欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

登录 图形验证码

程序员文章站 2022-05-01 18:37:37
[html]  <%@ page language="java" import="java.util.*" pageEnc...

[html] 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <base href="<%=basePath%>"> 
     
    <title>My JSP 'index.jsp' starting page</title> 
     
  </head> 
  <form action="./LoginServlet" method="post"> 
      <body>  
      员工信息录入<br/> 
       
                              用户名:<input type="text" value="" name="username" /> <br/> 
                              密码:<input type="password" name="psw"/> <br/> 
            验证码:<input name="checkcode" type="text" classs="big-input" maxlength="20" value="" /><br/> 
        <img src="${pageContext.request.contextPath }/ImageServlet" onclick="nextPic();" id="pic" title="看不清,再来一张" /><br/>                  
                              保存用户名和密码<input type="checkbox" name="save" value="yes" /><br/> 
  
             
            <input type="submit" value="保存" /> 
       </body> 
        
       <script type="text/javascript"> 
       function nextPic(){ 
         var picStr=document.getElementById("pic"); 
         //使用随机数 
        // picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Math.random(); 
                
        //使用时间戳       
        picStr.src="${pageContext.request.contextPath }/ImageServlet?"+Date.parse(new Date()); 
        
       } 
        
       </script> 
  </form> 
</html> 
ImageServlet:
[java] 
package aaa; 
 
 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.io.PrintWriter; 
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; 
import javax.servlet.http.HttpSession; 
 
public class ImageServlet extends HttpServlet { 
 
    private int width=85; 
    private int height=30; 
    //定义验证码的使用的字母和数字 
    private char[] charSeq="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); 
    private int codeCount=4; 
    //定义验证码的基线 
    private int codeX; 
    private int codeY; 
    public ImageServlet() { 
        super(); 
    } 
 
    public void destroy() { 
        super.destroy(); // Just puts "destroy" string in log 
        // Put your code here 
    } 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doPost(request,response); 
    } 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        request.setCharacterEncoding("utf-8"); 
        response.setContentType("text/html;charset=utf-8"); 
        //PrintWriter out = response.getWriter(); 
        HttpSession session=request.getSession(); 
        /*
         * BufferedImage描述具有可以访问图像缓冲区的image
         * Graphics类是所有图形上下文
         */ 
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 
        Graphics g=image.getGraphics(); 
        //将此图形上下文的当前颜色设置为指定颜色 
        g.setColor(Color.white); 
        g.fillRect(0, 0, width, height); 
        //设置边框的yans 
        g.setColor(Color.black); 
        g.drawRect(0, 0, width-1, height-1); 
        //生成验证码 
        Random random=new Random(); 
        String randstr=""; 
        for(int i=1;i<=codeCount;i++){ 
            //随即获取验证码 
            String rand=String.valueOf(charSeq[random.nextInt(charSeq.length)]); 
            System.out.println("rand=  "+rand); 
            randstr=randstr+rand; 
             
            g.setColor(Color.blue); 
            g.setFont(new Font("宋体",Font.BOLD,30)); 
             
            g.drawString(rand, codeX*i, codeY); 
        } 
         
        System.out.println("randstr=   "+randstr); 
        //把randstr放置到session中 
        session.setAttribute("checkKey", randstr); 
         
        //设置干扰点 
        for(int i=0;i<155;i++){ 
            int x=random.nextInt(width); 
            int y=random.nextInt(height); 
             
            g.setColor(Color.red); 
            g.drawOval(x, y, 0, 0); 
        } 
        //释放资源 
        g.dispose(); 
         
        HttpServletResponse res=(HttpServletResponse)response;   
        //设定网页的到期时间,一旦过期则必须到服务器上重新调用   
        res.setDateHeader("Expires", -1);   
        //Cache-Control 指定请求和响应应遵循的缓存机制   no-cache指示请求或响应消息是不能缓存的   
        res.setHeader("Cache-Control", "no-cache");   
        //用于设定禁止浏览器从本地缓存中调用页面内容,设定后一旦离开页面就无法从Cache中再调出   
        res.setHeader("Pragma", "no-cache");   
         
        ImageIO.write(image, "jpeg", response.getOutputStream()); 
         
    } 
 
    public void init() throws ServletException { 
        this.codeX=width/(codeCount+2); 
        this.codeY=height; 
    } 
 

LoginServlet:
[java] 
package aaa; 
 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletContext; 
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 LoginServlet extends HttpServlet { 
 
    public LoginServlet() { 
        super(); 
    } 
 
    public void destroy() { 
        super.destroy(); // Just puts "destroy" string in log 
    } 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doPost(request,response); 
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        //request.setCharacterEncoding("utf-8"); 
        response.setContentType("text/html;charset=utf-8"); 
        PrintWriter out = response.getWriter(); 
        HttpSession session=request.getSession(); 
         
        String username=request.getParameter("username"); 
        String checkcode=request.getParameter("checkcode"); 
        String checkKey=(String)session.getAttribute("checkKey"); 
        if(checkcode.equals(checkKey)){ 
            System.out.println("允许登录"); 
        }else{ 
            System.out.println("重新登录"); 
        } 
         
         
         
         
         
         
         
        System.out.println("username=  "+username); 
         
        request.getRequestDispatcher("/purview.jsp").forward(request, response); 
     
    } 
 
    public void init() throws ServletException { 
    }