java_利用session校验图片认证码

RegisterServlet:检验server,client验证码是否一致

ImageServlet: 产生验证码

 

 

<!DOCTYPE html>
<html>
<head>
<title>Register.html</title>

<script type="text/javascript">
		function ChangeImage(img){
			img.src=img.src+"?"+new Date().getTime();
			
		}
	</script>
</head>

<body>
	<form action="/NANA/servlet/RegisterServlet">
		用户名:<input type="text" ><br /> 
		密码:<input type="text"><br /> 
		认证码:<input type="text" name="checkcode"><img alt="" src="/NANA/servlet/ImageServlet" onclick="ChangeImage(this)" alt="change" style="cursor:pointer"><br/>
			<input type="submit" value="submit">
	</form>
</body>
</html>


RegisterServlet:

 

public class RegisterServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String c_checkcode = request.getParameter("checkcode");
		String s_checkcode = (String) request.getSession().getAttribute("checkcode");
		
		if(c_checkcode!=null&&s_checkcode!=null&&c_checkcode.equals(s_checkcode)){
			System.out.println("处理请求");
		}else{
			
			System.out.println("认证码错误");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}


 

 

ImageServlet

 

public class ImageServlet extends HttpServlet {
	private static final int WIDTH = 120;
	private static final int HEIGHT = 25;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
				BufferedImage.TYPE_INT_RGB);

		Graphics g = image.getGraphics();

		setBackGround(g);

		setBorder(g);

		drawRandomLine(g);

		String random = drawRandmNumber((Graphics2D) g);

		// add into server checkcode
		request.getSession().setAttribute("checkcode", random);

		response.setContentType("image/jpeg");
		response.setDateHeader("expries", -1);
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ImageIO.write(image, "jpg", response.getOutputStream());
	}

	private String drawRandmNumber(Graphics2D g) {
		// TODO Auto-generated method stub

		g.setColor(Color.RED);
		g.setFont(new Font("宋体", Font.BOLD, 20));

		StringBuffer sb = new StringBuffer();
		int x = 5;
		for (int i = 0; i < 4; i++) {

			int degree = new Random().nextInt() % 30;

			String num = (new Random().nextInt(10)) + "";
			sb.append(num);
			g.rotate(degree * Math.PI / 180, x, 20);

			g.drawString(num, x, 20);

			g.rotate(-degree * Math.PI / 180, x, 20);

			x += 30;

		}
		return sb.toString();

	}

	private void drawRandomLine(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.GREEN);
		for (int i = 0; i < 5; i++) {
			int x1 = new Random().nextInt(WIDTH);
			int y1 = new Random().nextInt(HEIGHT);

			int x2 = new Random().nextInt(WIDTH);
			int y2 = new Random().nextInt(HEIGHT);

			g.drawLine(x1, y1, x2, y2);
		}
	}

	private void setBackGround(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
	}

	private void setBorder(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.BLUE);
		g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2013-12-21 22:43  MrMrCash  阅读(171)  评论(0编辑  收藏  举报