[JSP] 利用Servlet对用户名和密码进行服务器端验证

2023-07-06 16:59:15 浏览数 (2)

代码语言:javascript复制
package com.shy;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass ProcessServlet extends HttpServlet {    protectedvoid Process(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.setCharacterEncoding("utf-8");        response.setContentType("text/html;charset=UTF-8");                String username = request.getParameter("username");        String password = request.getParameter("password");                boolean flag =true;       
 //将出借的信息写入 errorList中        
 List<String> errorList =new ArrayList<String>();        
//判断用户名不能为空
if(null==username ||"".equals(username))        {            errorList.add("用户名不能为空");        }        
//判断用户不用小于4位   
elseif(username.length()<4)        {            errorList.add("用户名不能小于4位");        }       
 //判断用户名第一位不能为数字
 elseif(Character.isDigit(username.charAt(0)))        {            errorList.add("用户名首字母不能为数字");        }        
 //判断用户名只能为字母数字以及(.) 英文逗点
 "for(int i=0;i<username.length();i  )              
	 {                    char c = username.charAt(i);  
 if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(Character.isDigit(c))||(c=='.'))                   
	 {                            continue;                    }   
 else                    {                        errorList.add("用户名只能为字母数字以及(.) 英文逗点");                                            }                                   }                
 //判断密码不能小于6位
 if(password.length() <6)        {            errorList.add("密码长度不能小于6位");                            }        
 //判断密码不能由全部重复的字符组成        
 for(int j=0;j<password.length()-1;j  )       
	 {                                if(password.charAt(j)!=password.charAt(j 1))          
		 {                continue;                                    }            
	 else            {                errorList.add("密码不能由全部重复字符组成");            }              }       
	 //判断密码是不是全由数字组成
	 for(int i=0;i<password.length();i  )       
		 {            char c = password.charAt(i);            
	 if(Character.isDigit(c))            {                continue;            }          
	 else            {                flag=false;            }        }                      
	 //如果密码全由数字组成,判断他是否为连续字符
	 if(flag)        {            for(int k=0;k<password.length()-1;k  )          
		 {                if((int)password.charAt(k)  1!= (int)password.charAt(k 1))               
			 {                     continue;                }                                           
		 else                {                    errorList.add("不能由连续的数字组成(123456)");                                                }            }        }       
		 //如果密码全由数字组成,判断他是否为连续字符
		 if(flag)        {            for(int k=0;k< password.length()-1;k  )           
			 {                if((int)password.charAt(k)-1!= (int)password.charAt(k 1))                {                    continue;                }                                            
		 else                {                    errorList.add("不能由连续的数字组成(123456)");                                                }            }        }             
		 if(errorList.isEmpty())       
			 {            request.setAttribute("username", username);         
		 request.setAttribute("password", password);            
		 request.getRequestDispatcher("javaservletsuccess.jsp").forward(request,response);        }      
		 else        {            
		 request.setAttribute("error", errorList);        
		 request.getRequestDispatcher("javaservleterror.jsp").forward(request,response);        }           }    
		 @Override    
		 protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        Process(req,resp);    }    
		 @Override   
		 protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        Process(req,resp);    }   }

利用Servlet对用户名和密码进行服务器端验证

校验规则:

1. 用户名只能为数字/字母/英文点号(.), 且不能由数字开始, 至少4位

2. 密码不能由全部重复的字符组成, 不能由连续的数字组成(123456/654321), 至少6位

代码如下:

代码语言:javascript复制
package com.shy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


publicclass ProcessServlet extends HttpServlet {

    protectedvoid Process(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        boolean flag =true;
        //将出借的信息写入 errorList中
        List<String> errorList =new ArrayList<String>();
        //判断用户名不能为空
if(null==username ||"".equals(username))
        {
            errorList.add("用户名不能为空");
        }
        //判断用户不用小于4位   
elseif(username.length()<4)
        {
            errorList.add("用户名不能小于4位");
        }
        //判断用户名第一位不能为数字
elseif(Character.isDigit(username.charAt(0)))
        {
            errorList.add("用户名首字母不能为数字");
        }
        //判断用户名只能为字母数字以及(.) 英文逗点"
for(int i=0;i<username.length();i  )
                {
                    char c = username.charAt(i);                    
                    if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(Character.isDigit(c))||(c=='.'))
                    {
                            continue;
                    }
                    else
                    {
                        errorList.add("用户名只能为字母数字以及(.) 英文逗点");                        
                    }                   
                }
        
        //判断密码不能小于6位
if(password.length() <6)
        {
            errorList.add("密码长度不能小于6位");                    
        }
        //判断密码不能由全部重复的字符组成        
for(int j=0;j<password.length()-1;j  )
        {                    
            if(password.charAt(j)!=password.charAt(j 1))
            {
                continue;                        
            }
            else
            {
                errorList.add("密码不能由全部重复字符组成");
            }      
        }
        //判断密码是不是全由数字组成
for(int i=0;i<password.length();i  )
        {
            char c = password.charAt(i);
            if(Character.isDigit(c))
            {
                continue;
            }
            else
            {
                flag=false;
            }
        }              
        //如果密码全由数字组成,判断他是否为连续字符
if(flag)
        {
            for(int k=0;k<password.length()-1;k  )
            {
                if((int)password.charAt(k)  1!= (int)password.charAt(k 1))
                {
                     continue;
                }                            
                else
                {
                    errorList.add("不能由连续的数字组成(123456)");
                                
                }
            }
        }
        //如果密码全由数字组成,判断他是否为连续字符
if(flag)
        {
            for(int k=0;k< password.length()-1;k  )
            {
                if((int)password.charAt(k)-1!= (int)password.charAt(k 1))
                {
                    continue;
                }                            
                else
                {
                    errorList.add("不能由连续的数字组成(123456)");                                
                }
            }
        }
        
       if(errorList.isEmpty())
        {
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            request.getRequestDispatcher("javaservletsuccess.jsp").forward(request,response);
        }
        else
        {
            request.setAttribute("error", errorList);
            request.getRequestDispatcher("javaservleterror.jsp").forward(request,response);
        }       
    }

    @Override
    protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Process(req,resp);
    }

    @Override
    protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Process(req,resp);
    }   
}

0 人点赞