首先编写登录页面mylogin.jsp
编写表单:
<form οnsubmit="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在客户端实现验证
<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类
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
<%@ 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
用第二种方式时将mylogin.jsp中的表单修改为
<form action="ValidateServlet">