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

Java实现登录验证码

程序员文章站 2022-04-28 11:41:55
登录验证码 Servlet / 从请求中获取数据,获取验证码的session的值转为String类型, 销毁,防止返回后验证码不刷新,重新验证成功 判断验证码是否相同(忽略大小写) 相同:创建user对象调用service层的方法验证返回结果是否为空 为空:创建session:储存错误信息,转发,登 ......

登录验证码

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());  
}