zoukankan      html  css  js  c++  java
  • 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);
    	}
    
    }
    


     

  • 相关阅读:
    [NOI2015]软件包管理器(树链剖分)
    [题解][luogu p1273]有线电视网
    [题解][洛谷P1373 小a和uim之大逃离]
    《LoadRunner 没有告诉你的》之五——无所不在的性能测试 (已完稿)
    《LoadRunner没有告诉你的》之六——获取有效的性能需求
    《LoadRunner 没有告诉你的》之一——描述性统计与性能结果分析
    《LoadRunner 没有告诉你的》之四——理解性能
    《LoadRunner 没有告诉你的》之三——理发店模型
    人活着就是要学会持续的奔跑!【二】
    人活着就是要学会持续的奔跑!【一】
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720428.html
Copyright © 2011-2022 走看看