Java implementation login verification code

Login verification code



  • Get the data from the request and change the value of the session to String type
  • Destroy to prevent the verification code from not refreshing after returning. The re verification is successful
  • Determine whether the verification code is the same (ignore case)
  • Same: create user object to call the method of service layer to verify whether the return result is empty
    Blank: create session: save error message, forward, login page displays login name or password error
    Not empty: create session: save user name, forward to login success page
  • Different: create session: save the error information, the login page displays the verification code error (judge if the session is null, do not display)
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;        
 if (checkcode!=null&&code.equalsIgnoreCase(checkcode)){      
User u=new User();            
User user = login.Login(u);   
if (user!=null){                request.getSession().setAttribute("username",username)         
}else{                request.getSession().setAttribute("userfail","Wrong user name or password");               
}        }else{            request.getSession().setAttribute("codefail","Verification code error");    


public class CheckcodeServlet extends HttpServlet {    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
//Define the length and width of the verification code box
int width = 100;     
int height = 50;    
//Create image object
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
//Create brush object
Graphics graphics = image.getGraphics();     
//Set brush color
//Fill in background
graphics.fillRect(0, 0, width, height);        
//Reset brush color graphics.setColor(Color.BLUE)
//Draw verification code border
graphics.drawRect(0, 0, width - 1, height - 1);    
//String the content to be displayed by the verification code
String s = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890";   
//Create random number object 
Random random = new Random();      
//Create color array
Color[] colors = {, Color.BLACK, Color.magenta, Color.YELLOW, Color.GREEN};   
//Create a builder object for combining captcha
StringBuilder builder = new StringBuilder();    
//for loop drawing verification code
for (int i = 1; i <= 4; i++) {         
//Change each letter to another color (colors [new random(). Nextint (colors. Length)])
//Randomly generated string subscript
int index = random.nextInt(s.length());  
//Get the character through the string subscript
char c = s.charAt(index);       
//Combination string
//Set font of verification code
graphics.setFont(new Font("Comic Sans MS", Font.BOLD, 20));       
//Location of verification code
graphics.drawString(c + "", width / 5 * i, height / 2);       
//Change the verification code to String type
String s1 = builder.toString();     
//Store in session request.getSession().setAttribute("checkcode", s1); / / for loop drawing interference line
for (int i = 0; i < 30; i++) {         
//Set interference line color
graphics.setColor(colors[new Random().nextInt(colors.length)]);   
//Set interference line coordinates
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);        }      
//Output verification code box
ImageIO.write(image, "jpg", response.getOutputStream());  

Keywords: Java Session JSP

Added by Angus on Thu, 05 Dec 2019 06:00:50 +0200