文字说明: 一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向。

实现方式 response.sendRedirect(“/welcome.html”) 实现原理: 302状态码和location头即可实现重定向

列如:

java

	// 取得提交的参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String checkCode = request.getParameter("checkCode");
		
		
		System.out.println("用户名:"+username);
		System.out.println("密码"+password);
		System.out.println("验证码:"+checkCode);
		
		//重定向到Message.html
		//这是/ 表示webapps目录
		response.sendRedirect("/day04/success.html");

login.html

<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
   <form action='/day04/LoginServlet' method='post'>
       <table border="1" align="center">
           <caption>用户登陆【mvc】</caption>
           <tr>
               <th>用户</th>
               <td><input type="text" name="username"></td>
           </tr>
           <tr>
               <th>密码</th>
               <td><input type="password" name="password"></td>
           </tr>
           <tr>
              <th>验证码</th>
              <td>
              <input type="text" name="checkCode"/>
              </td>
              <td>
                <img alt="" src="/day04/Yanzm01">
              </td>             
           </tr>
           <tr>
           <td colspan="2" align="center">
              <input type="submit" value="提交">         
           </td>
           </tr>
       </table>
   
   
   </form>  
  </body>
</html>

验证码:


import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



//生成随机图片
public class Yanzm01 extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//设置浏览器不能缓存
		response.setHeader("expires","-1"); //ie浏览器
		response.setHeader("cache-control","no-cache");
		response.setHeader("pragma","no-cache");
		
		//在内存中构造一副图片
		BufferedImage image = new BufferedImage(100,25,BufferedImage.TYPE_INT_RGB);

		//取得画笔
		Graphics g =  image.getGraphics();
		
		//设置字体大小和颜色
		g.setColor(Color.YELLOW);
		g.setFont(new Font("黑体",Font.BOLD,22));
		//在图片中显示字符串 1azb
		g.drawString(getStringfor(),20,20);
		
		ImageIO.write(image, "jpg", response.getOutputStream());
		


	}
	
	/**产生一个随机数*/
	private String getString()
	{
		
		return UUID.randomUUID().toString();
	}
	
	/**产生一个4位随机数第一个数字,第二个字母,第三个数字,第四个字母*/
	private String getStringfor()
	{
		String str = "";
		String numberAndLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		int length = numberAndLetter.length(); //取得全部长度
		for(int i=1;i<=4;i++){  //验证码长度
			if(i==1 || i==3){//如果1、3、位数字
				while(true){
					Random random = new Random();
					int index = random.nextInt(length);
					//substring(int beginIndex, int endIndex)
					//返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。
					String value = numberAndLetter.substring(index,index+1);  
					if(value.matches("[0-9]")){ //正则判断
						str += value;
						break;
					}
				}
			}else if(i==2 || i==4){//如果2、4位大小字母
				while(true){
					Random random = new Random();
					int index = random.nextInt(length);
					String value = numberAndLetter.substring(index,index+1);
					if(value.matches("[A-Za-z]")){
						str += value;
						break;
					}
				}
			}
		}
		return str;
			
	}


}