JavaWeb -- Session应用实例 -- 随机中文验证码 检验
注册页面 login.html
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Insert title here</title> <script type="text/javascript"> function changeImage(img) { img.src = img.src + "?" + new Date().getTime(); } </script> </head> <body> <form action="/WebTest3/Demo9" method="get"> <pre> User :<input type="text" name="username"/> <br /> Passwd :<input type="password" name="passwd"/> <br /> CheckCode:<input type="text" name="checkcode"/> <br /> <img src="/WebTest3/Demo8" alt="ChangeOne" onclick="changeImage(this)" style="cursor: hand"/> <br /> Submit<input type="submit" name="submit" value="submit"/> </pre> </form> </body> </html>
随机中文图片生成Servlet
public class Demo8 extends HttpServlet { private static final long serialVersionUID = 1L; private static final int width = 120; private static final int height = 40; /** * @see HttpServlet#HttpServlet() */ public Demo8() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); setBackGround(g); setBorder(g); drawRandomLine(g); String random = drawRandomNum((Graphics2D)g); 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 drawRandomNum(Graphics2D g) { // TODO Auto-generated method stub //[\u4e00 ~ \u9fa5] g.setColor(Color.BLUE); g.setFont(new Font(null, Font.BOLD, 20)); String base = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765"; int x = 5; StringBuffer sb = new StringBuffer(); for(int i=0; i<4; i++) { String ch = base.charAt(new Random().nextInt(base.length())) + ""; //System.out.println(ch); sb.append(ch); int degreen = new Random().nextInt()%30; g.rotate(degreen*Math.PI/180, x, 25); g.drawString(ch, x, 25); g.rotate(-degreen*Math.PI/180, x, 25); x += 30; } return sb.toString(); } private void drawRandomLine(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.green); for(int i=0; i<8; 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 setBorder(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.blue); g.drawRect(1, 1, width-2, height-2); } private void setBackGround(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
检验验证码正确性Servlet
public class Demo9 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Demo9() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String c_checkcode_temp = request.getParameter("checkcode"); String c_checkcode = new String(c_checkcode_temp.getBytes("iso8859-1"), "UTF-8"); //防乱码 String s_checkcode = (String) request.getSession().getAttribute("checkcode"); out.println("c_code: " + c_checkcode + "s_code: " + s_checkcode); if(c_checkcode!=null && s_checkcode!=null && c_checkcode.equals(s_checkcode)) { out.println("checkcode is right"); } else { out.println("checkcode is wrong"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }