用JSTL实现类似Struts1的表单验证

java 代码
 
  1. <%@ page language="java" import="java.util.*" pageEncoding=""%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head> 
  7.     <title>Login</title>  
  8.       
  9.     <meta http-equiv="pragma" content="no-cache"
  10.     <meta http-equiv="cache-control" content="no-cache">  
  11.     <meta http-equiv="expires" content="0">     
  12.       
  13.   </head>  
  14.     
  15.   <body>  
  16.     <form method="post" action="<c:url value='/servlet/LoginServlet'/>">  
  17.         your name : <input type="text" name="userName" value="${requestScope.userName}"/>   
  18.             ${requestScope.errors["userName"]}<br>  
  19.         your password : <input type="password" name="password" value="${requestScope.password}"/>   
  20.             ${requestScope.errors["password"]} <br>  
  21.         <input type="submit" name="submit" value="submit"/>  
  22.         <input type="reset" name="reset" value="reset"/>  
  23.     </form>  
  24.   </body>  
  25. </html>  

java 代码
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.       
  8.     <title>Login</title>  
  9.       
  10.     <meta http-equiv="pragma" content="no-cache">  
  11.     <meta http-equiv="cache-control" content="no-cache">  
  12.     <meta http-equiv="expires" content="0">      
  13.   </head>  
  14.     
  15.   <body>  
  16.     your name is ${requestScope.userName} <br>  
  17.     your password is ${requestScope.password}  
  18.   </body>  
  19. </html>  
java 代码
 
  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  3.   
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  5. <html>  
  6.   <head>  
  7.       
  8.     <title>Login</title>  
  9.       
  10.     <meta http-equiv="pragma" content="no-cache">  
  11.     <meta http-equiv="cache-control" content="no-cache">  
  12.     <meta http-equiv="expires" content="0">      
  13.   </head>  
  14.     
  15.   <body>  
  16.     Errors message: <br>  
  17.     ${requestScope.errorMsg}  
  18.   </body>  
  19. </html>  
java 代码
 
  1. package app;  
  2.   
  3. import java.io.*;  
  4.   
  5. import javax.servlet.*;  
  6. import javax.servlet.http.*;  
  7.   
  8. import java.util.*;  
  9.   
  10. public class LoginServlet extends HttpServlet {  
  11.   
  12.     /** 
  13.      * Constructor of the object. 
  14.      */  
  15.     public LoginServlet() {  
  16.         super();  
  17.     }  
  18.   
  19.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  20.             throws ServletException, IOException {  
  21.           
  22.         if(!invalidate(request, response)) {  
  23.             RequestDispatcher dispatcher = request.getRequestDispatcher("/Login.jsp");  
  24.             dispatcher.forward(request, response);  
  25.               
  26.             return;  
  27.         }  
  28.           
  29.         if(userName.equals("xuxiaolei") && password.equals("123")) {  
  30.               
  31.             request.setAttribute("userName", userName);  
  32.             request.setAttribute("password", password);  
  33.             RequestDispatcher dispatcher = request.getRequestDispatcher("/ShowLogin.jsp");  
  34.             dispatcher.forward(request, response);  
  35.         } else {  
  36.             request.setAttribute("errorMsg""userName or password error");  
  37.             RequestDispatcher dispatcher = request.getRequestDispatcher("/ErrorMsg.jsp");  
  38.             dispatcher.forward(request, response);  
  39.         }  
  40.           
  41.           
  42.               
  43.     }  
  44.   
  45.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  46.             throws ServletException, IOException {  
  47.   
  48.         doGet(request, response);  
  49.     }  
  50.       
  51.     public boolean invalidate(HttpServletRequest request, HttpServletResponse response) {  
  52.       
  53.         boolean result = true;  
  54.         HashMap errors = new HashMap();  
  55.         setUserName(request);  
  56.         setPassword(request);  
  57.         if(userName == null || userName.length() < 1) {  
  58.             userName = "";  
  59.             errors.put("userName""User is required");  
  60.         }  
  61.           
  62.         if(password == null || password.length() < 1) {  
  63.             password = "";  
  64.             errors.put("password""Password required");  
  65.         }  
  66.           
  67.         if(errors.size() > 0) {  
  68.             request.setAttribute("userName", userName);  
  69.             request.setAttribute("password", password);  
  70.             request.setAttribute("errors", errors);  
  71.             result = false;  
  72.         }  
  73.           
  74.         return result;  
  75.     }  
  76.       
  77.     public void setUserName(HttpServletRequest request) {  
  78.         userName = (String) request.getParameter("userName");  
  79.     }  
  80.       
  81.     public void setPassword(HttpServletRequest request) {  
  82.         password = (String) request.getParameter("password");  
  83.     }  
  84.   
  85.       
  86.       
  87.       
  88.   
  89.     private String userName;  
  90.     private String password;  
  91.       
  92. }  
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.       
  8.   <servlet>  
  9.     <description>This is the description of my J2EE component</description>  
  10.     <display-name>This is the display name of my J2EE component</display-name>  
  11.     <servlet-name>LoginServlet</servlet-name>  
  12.     <servlet-class>app.LoginServlet</servlet-class>  
  13.   </servlet>  
  14.   <servlet>  
  15.     <description>This is the description of my J2EE component</description>  
  16.     <display-name>This is the display name of my J2EE component</display-name>  
  17.     <servlet-name>PreStudentView</servlet-name>  
  18.     <servlet-class>app.PreStudentView</servlet-class>  
  19.   </servlet>  
  20.   
  21.   
  22.   <servlet-mapping>  
  23.     <servlet-name>LoginServlet</servlet-name>  
  24.     <url-pattern>/servlet/LoginServlet</url-pattern>  
  25.   </servlet-mapping>  
  26.   <servlet-mapping>  
  27.     <servlet-name>PreStudentView</servlet-name>  
  28.     <url-pattern>/servlet/PreStudentView</url-pattern>  
  29.   </servlet-mapping>  
  30.       
  31. </web-app>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值