Servlet验证码

//login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${msg } <form action="/day11/login" method="post"> <table border="1" width="70%"> <tr> <td>输入姓名</td> <td> <input type="text" name="username" /> </td> </tr> <tr> <td>输入密码</td> <td> <input type="password" name="password" /> </td> </tr> <tr> <td>验证码</td> <td> <input type="text" name="code" /> <img id="imgId" src="/day11/checkcode"> <a href="#" onclick="run()">看不清,换一张</a> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="登陆"/> </td> </tr> </table> </form> </body> <script type="text/javascript"> // 看不清,换一张,时间戳 function run(){ // 获取图片 var image = document.getElementById("imgId"); image.src = "/day11/checkcode?"+new Date().getTime(); } </script> </html>

  

package CheckCode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

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 check extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		final int width=140;
		final int height=30;
		BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
		Graphics2D g=(Graphics2D)image.getGraphics();
		
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.BLUE);
		g.drawRect(0, 0, width-1, height-1);
		
		String words="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
		Random random=new Random();

		g.setColor(Color.WHITE);
		g.setFont(new Font("楷书",Font.PLAIN,18));
		
		int x=20;
		int y=20;
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<5;i++){
			int jiaodu=random.nextInt(60)-30;
			double hudu=jiaodu*Math.PI/180;
			g.rotate(hudu,x,y);
			int index=random.nextInt(words.length());
			char ch=words.charAt(index);
			
			sb.append(ch);
			g.drawString(" "+ch, x, y);
			g.rotate(-hudu,x,y);
			x+=18;
		}
		request.getSession().setAttribute("code", sb.toString());
		g.setColor(Color.BLUE);
		int x1;
		int x2;
		int y1;
		int y2;
		for(int i=0;i<4;i++){
			x1=random.nextInt(width);
			y1=random.nextInt(height);
			x2=random.nextInt(width);
			y2=random.nextInt(height);
			g.drawLine(x1, y1, x2, y2);
		}
		
		ImageIO.write(image, "jpg", response.getOutputStream());
	}

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

}

  

package CheckCode;

import java.io.IOException;

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

public class loginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String code1=(String)request.getSession().getAttribute("code");
		System.out.println(code1);
		String code2=request.getParameter("code");
		if(code2!=null&&code1.equalsIgnoreCase(code2)){
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().write("成功");
		}else{
			request.setAttribute("msg","验证码错误");
			request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
		}
	}

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

		doGet(request, response);

	}

}

  

posted @ 2017-04-12 21:19  wqkant  阅读(114)  评论(0编辑  收藏  举报