首页 > 程序开发 > Web开发 > Jsp >

JSP+servlet生成验证码并验证

2015-08-03

生成验证码的基本过程是:



生成验证码的基本过程是:
这里写图片描述

<%@ page language=java import=java.util.* pageEncoding=UTF-8%>
<%@ page import=java.awt.*,java.awt.image.BufferedImage,javax.imageio.ImageIO %>
     <%
      response.setHeader(Cache-Control, no-cache);
      int width=60,height=20;
      
      BufferedImage image=new BufferedImage( width,height,BufferedImage.TYPE_INT_RGB);
      //由bufferdImage对象获得画笔g
      Graphics g=image.getGraphics();
      //画笔填充矩形
      g.setColor(new Color(200,200,200));
      g.fillRect(0, 0, width, height);
      //生成随机数
      Random rd=new Random();
      int rdNum=rd.nextInt(8999)+1000;
      String rdstr=String.valueOf(rdNum);
      //把随机数放入session中
      session.setAttribute(rdStr, rdstr);
      //画笔g画随机数
      g.setColor(Color.BLUE);
      g.setFont(new Font(,Font.PLAIN,20));
      g.drawString(rdstr, 10, 20);//这里的坐标是一个字符的左下角基线   
      //画100个随机点
      g.setColor(Color.green);
      for(int i=0;i<100;i++){
           int x=rd.nextInt(width);
           int y=rd.nextInt(height);
           g.drawOval(x, y, 1, 1);
      }
      //用ImageIO的静态函数write输出图像
      ImageIO.write(image, JPEG, response.getOutputStream());
      out.clear();
      out=pageContext.popBody(); 
     %>

画图的基本思想是:
这里写图片描述


<%@ page language=java import=java.util.* pageEncoding=UTF-8%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;
%>

> <script type=text/javascript> function refresh(){ document.loginform.imgcode.src=http://blog.csdn.net/tuke_tuke/article/details/codes.jsp; } </script>

欢迎登陆


请输入账号:
请输入密码:
请输入验证码:

这里写图片描述


package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class codeServlet extends HttpServlet {
    /**
     * The doPost method of the servlet. 

     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取表单的验证码
        String code=request.getParameter(yanzhengma);
          //获取session中的验证码
          HttpSession session=request.getSession();
          String randstr=(String) session.getAttribute(rdStr);
          response.setCharacterEncoding(gb2312);//respons设为中文编码

          PrintWriter out=response.getWriter();//获得输出写出器
           if(code.equals(randstr)){//输入的验证码和session保存的验证码相比较
                    out.println(恭喜你,验证码正确!);         
           }
           else{                
               //response.sendRedirect(/code/back.jsp);
               out.println(验证码错误!!);
           }          
    }
}

输入结果是:
这里写图片描述
这里写图片描述

相关文章
最新文章
热点推荐