Java实现登录验证码

登录验证码

Servlet

/*

  • 从请求中获取数据,获取验证码的session的值转为String类型,
  • 销毁,防止返回后验证码不刷新,重新验证成功
  • 判断验证码是否相同(忽略大小写)
  • 相同:创建user对象调用service层的方法验证返回结果是否为空      
    * 为空:创建session:储存错误信息,转发,登录页面显示登录名或密码错误    
    * 不为空:创建session:储存用户名,转发,到登录成功页面
  • 不相同:创建session:储存错误信息,登录页面显示验证码错误(判断如果session为null不显示)
public class Servlet extends HttpServlet {  
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
Login login = new service.impl.Login();   
String username =request.getParameter("username");  
String password = request.getParameter("password");   
String code = request.getParameter("code");        
Object checkcode1 = request.getSession().getAttribute("checkcode");
String checkcode = (String) checkcode1;        
request.getSession().removeAttribute("checkcode");       
 if (checkcode!=null&&code.equalsIgnoreCase(checkcode)){      
User u=new User();            
u.setUsername(username);            
u.setPassword(password);    
User user = login.Login(u);   
if (user!=null){                request.getSession().setAttribute("username",username)         
request.getRequestDispatcher("Success.jsp").forward(request,response);      
}else{                request.getSession().setAttribute("userfail","用户名或密码错误");               
 request.getRequestDispatcher("index.jsp").forward(request,response);       
}        }else{            request.getSession().setAttribute("codefail","验证码错误");    
request.getRequestDispatcher("index.jsp").forward(request,response);        
}                   
}

CheckcodeServlet

public class CheckcodeServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
//定义验证码框的长宽      
int width = 100;     
int height = 50;    
//创建image对象       
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
//创建画笔对象    
Graphics graphics = image.getGraphics();     
//设置画笔颜色      
graphics.setColor(Color.white);       
//填充背景      
graphics.fillRect(0, 0, width, height);        
//重新设定画笔颜色        graphics.setColor(Color.BLUE);     
//画验证码的边框      
graphics.drawRect(0, 0, width - 1, height - 1);    
//将验证码所要显示的内容组成字符串       
String s = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";   
//创建随机数对象        
Random random = new Random();      
//创建颜色数组       
Color[] colors = {Color.red, Color.BLACK, Color.magenta, Color.YELLOW, Color.GREEN};   
//创建builder对象用于组合验证码       
StringBuilder builder = new StringBuilder();    
//for循环画验证码    
for (int i = 1; i <= 4; i++) {         
//每个字母换一个颜色            graphics.setColor(colors[new Random().nextInt(colors.length)]);     
//随机生成字符串下标          
int index = random.nextInt(s.length());  
//通过字符串下标拿到字符        
char c = s.charAt(index);       
//组合字符串          
builder.append(c);     
//设置验证码的字体       
graphics.setFont(new Font("Comic Sans MS", Font.BOLD, 20));       
//验证码所要摆放的位置     
graphics.drawString(c + "", width / 5 * i, height / 2);       
}       
//将验证码转为String类型      
String s1 = builder.toString();     
//存放在session中        request.getSession().setAttribute("checkcode", s1);        //for循环画干扰线  
for (int i = 0; i < 30; i++) {         
//设置干扰线颜色         
graphics.setColor(colors[new Random().nextInt(colors.length)]);   
//设置干扰线坐标           
int x = random.nextInt(width);    
int y = random.nextInt(height);     
int x1 = random.nextInt(30);       
int y1 = random.nextInt(30);     
int sin = random.nextBoolean() ? 1 : -1;      
int cos = random.nextBoolean() ? 1 : -1;            graphics.drawLine(x, y, x + x1 * sin, y + y1 * cos);        }      
//输出验证码框    
ImageIO.write(image, "jpg", response.getOutputStream());  
}
原文地址:https://www.cnblogs.com/JaminYe/p/10459453.html