利用Servlet和jsp实现客户端与服务器端的用户登录信息验证

首先编写登录页面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

<%@ 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">



 

 

转载于:https://www.cnblogs.com/lcqBlogs/archive/2011/12/03/2392374.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值