利用Servlet和jsp實現客戶端與服務器端的用戶登錄信息驗證 – JAVA編程語言程序開發技術文章

首先編寫登錄頁面mylogin.jsp
 編寫表單:
[java]
<form onsubmit="return valdate()"> 
   
  username:  <input type="text" name = "username" id="username"><br/> 
  password:  <input type="password" name = "password" id="password"><br/> 
  repassword:  <input type="password" name = "repassword" id="repassword"><br/> 
 
  <input type="submit" value="submit"/> 
  <input type="reset" value="reset"/>   
   
   
  </form> 
第一種驗證的方式:利用javascript在客戶端實現驗證
[java]
<script type="text/javascript"> 
  
 function valdate(){ 
 //var username = document.getElementById("username"); 
 //var password = document.getElementById("password"); 
 //var repassword = document.getElementById("repassword"); 
  
  var username = document.getElementsByName("username")[0]; 
 var password = document.getElementsByName("password")[0]; 
 var repassword = document.getElementsByName("repassword")[0]; 
  
 if(username.value.length == 0){ 
   
  alert("username can not be blank"); 
  return false; 
 } 
  
 if(password.value.length < 6 || password.value.length > 11){ 
  
   alert("length of password is invalid"); 
  return false; 
  
 } 
 if(password.value != repassword.value> 11){ 
  
   alert("password is not the same with repassword"); 
  return false; 
  
 } 
  return true; 
    
 } 
  
  
 </script> 
如上面的代碼所示,在表單中調用javascript的函數實現客戶端驗證。
第二種驗證的方式:用Servlet實現驗證
編寫ValidateServlet.java類
[java]
package com.lcq.servlet; 
 
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; 
 
@SuppressWarnings("serial") 
public class ValidateServlet extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        String username = request.getParameter("username"); 
        String password = request.getParameter("password"); 
        String repassword = request.getParameter("repassword"); 
        List<String> list = new ArrayList<String>(); 
        if(null == username || "".equals(username)){ 
            list.add("username can't be blank!"); 
        } 
        if(password == null ||password.length() < 6 ||password.length() > 10){ 
            list.add("length of password should be between 6 and 10"); 
        } 
        if(repassword == null ||repassword.length() < 6 || repassword.length() > 10){ 
            list.add("length of repassword should be between 6 and 10"); 
        } 
        if(password != null && repassword != null && !password.equals(repassword)){ 
            list.add("password and repassword not the same!"); 
        } 
         
         
        if(list.isEmpty()){ 
            request.setAttribute("username", username); 
            request.setAttribute("password", password); 
            request.getRequestDispatcher("sucess.jsp").forward(request, response); 
        }else{ 
            request.setAttribute("error",list); 
            request.getRequestDispatcher("error.jsp").forward(request, response);; 
        } 
        
         
    } 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
         doGet(request, response); 
     
    } 
 

在Servlet中處理完成後轉到sucess.jsp或者error.jsp
以下是sucess.jsp
[java]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
   
     
    <title>My JSP 'sucess.jsp' starting page</title> 
     
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <!– 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    –> 
 
  </head> 
   
  <body> 
   
   
  username :<%=request.getAttribute("username") %><br> 
  password :<%=request.getAttribute("password") %> 
  
  </body> 
</html> 

以下是error.jsp
[java]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
     
    <title>My JSP 'error.jsp' starting page</title> 
     
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <!– 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    –> 
 
  </head> 
   
  <body> 
 
  <h1>login failed:</h1> 
  <% 
  List <String> list = (List<String>)request.getAttribute("error"); 
  for(String str : list ){ 
  out.println(str + "<br>"); 
  
  } 
   %> 
     
 
 
 
  </body> 
</html> 
用第二種方式時將mylogin.jsp中的表單修改為
  <form  action="ValidateServlet">

摘自 liuchangqing123

發佈留言