当前位置: 移动技术网 > IT编程>开发语言>Jsp > servlet服务器端验证

servlet服务器端验证

2018年11月23日  | 移动技术网IT编程  | 我要评论
:编写服务器端验证的程序 要求:(1)编写表单界面如下图: (2)编写servlet程序对用户所输入的内容进行验证,要求用户名不能为空,密码与确认密码不能为空且长度在4-10之间,密码与确认密码相

:编写服务器端验证的程序

要求:(1)编写表单界面如下图:


(2)编写servlet程序对用户所输入的内容进行验证,要求用户名不能为空,密码与确认密码不能为空且长度在4-10之间,密码与确认密码相同。如果用户输入的条件符合要求,则请求转发到success.,否则请求转发到error.jsp


(3)success.jsp用于输出用户所输入的用户名及密码。


(4)error.jsp用于输出用户输入时出错的信息。

 


├─.myeclipse
├─.settings
├─src
│  └─com
│      └─mars
└─webroot
    ├─meta-inf
    └─web-inf
        ├─classes
        │  └─com
        │      └─mars
        └─lib

 

 

登录login.jsp

[html]
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> 
<% 
    string path = request.getcontextpath(); 
    string basepath = request.getscheme() + "://" 
            + request.getservername() + ":" + request.getserverport() 
            + path + "/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
    <head> 
        <base href="<%=basepath%>"> 
 
        <title>login</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> 
        <center> 
            <h1> 
                服务器端验证的程序 
            </h1> 
            <form action="login" method="get"> 
                <table border="1"> 
                    <tr> 
                        <td> 
                            用 户 名: 
                        </td> 
                        <td> 
                            <input type="text" name="username"> 
                        </td> 
                    </tr> 
 
                    <tr> 
                        <td> 
                            密    码: 
                        </td> 
                        <td> 
                            <input type="password" name="password1"> 
                        </td> 
                    </tr> 
 
                    <tr> 
                        <td> 
                            确认密码: 
                        </td> 
                        <td> 
                            <input type="password" name="password2"> 
                        </td> 
                    </tr> 
 
                    <tr> 
                        <td colspan="2"> 
                            <input type="submit" value="提交"> 
                        </td> 
                    </tr> 
 
                </table> 
            </form> 
        </center> 
    </body> 
</html> 
<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<%
 string path = request.getcontextpath();
 string basepath = request.getscheme() + "://"
   + request.getservername() + ":" + request.getserverport()
   + path + "/";
%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">

  <title>login</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>
  <center>
   <h1>
    服务器端验证的程序
   </h1>
   <form action="login" method="get">
    <table border="1">
     <tr>
      <td>
       用 户 名:
      </td>
      <td>
       <input type="text" name="username">
      </td>
     </tr>

     <tr>
      <td>
       密    码:
      </td>
      <td>
       <input type="password" name="password1">
      </td>
     </tr>

     <tr>
      <td>
       确认密码:
      </td>
      <td>
       <input type="password" name="password2">
      </td>
     </tr>

     <tr>
      <td colspan="2">
       <input type="submit" value="提交">
      </td>
     </tr>

    </table>
   </form>
  </center>
 </body>
</html>
success.jsp


[html]
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> 
<% 
    string path = request.getcontextpath(); 
    string basepath = request.getscheme() + "://" 
            + request.getservername() + ":" + request.getserverport() 
            + path + "/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
    <head> 
        <base href="<%=basepath%>"> 
 
        <title>success</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> 
        <center> 
            <h1> 
                登陆成功界面 
            </h1> 
            用户名:<%=request.getattribute("name")%><br> 
            密  码:<%=request.getattribute("password")%> 
        </center> 
    </body> 
 
</html> 
<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<%
 string path = request.getcontextpath();
 string basepath = request.getscheme() + "://"
   + request.getservername() + ":" + request.getserverport()
   + path + "/";
%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">

  <title>success</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>
  <center>
   <h1>
    登陆成功界面
   </h1>
   用户名:<%=request.getattribute("name")%><br>
   密  码:<%=request.getattribute("password")%>
  </center>
 </body>

</html>

error.jsp


[html]
<%@ page language="java" import="java.util.*" pageencoding="gbk"%> 
<% 
    string path = request.getcontextpath(); 
    string basepath = request.getscheme() + "://" 
            + request.getservername() + ":" + request.getserverport() 
            + path + "/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
    <head> 
        <base href="<%=basepath%>"> 
 
        <title>error</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> 
        <% 
            arraylist l = new arraylist(); 
            l = (arraylist) request.getattribute("error"); 
            iterator i = l.iterator(); 
            string str = ""; 
            while (i.hasnext()) { 
                str = (string) i.next(); 
                out.println("<table border=1 width=200><tr><td>" + str + "</td></tr></table>"); 
            } 
        %> 
    </body> 
</html> 
<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<%
 string path = request.getcontextpath();
 string basepath = request.getscheme() + "://"
   + request.getservername() + ":" + request.getserverport()
   + path + "/";
%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <base href="<%=basepath%>">

  <title>error</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>
  <%
   arraylist l = new arraylist();
   l = (arraylist) request.getattribute("error");
   iterator i = l.iterator();
   string str = "";
   while (i.hasnext()) {
    str = (string) i.next();
    out.println("<table border=1 width=200><tr><td>" + str + "</td></tr></table>");
   }
  %>
 </body>
</html>

web.xml


[html]
<?xml version="1.0" encoding="utf-8"?> 
<web-app version="2.4"  
    xmlns=""  
    xmlns:xsi=""  
    xsi:schemalocation="  
    "> 
  <servlet> 
    <description>this is the description of my j2ee component</description> 
    <display-name>this is the display name of my j2ee component</display-name> 
    <servlet-name>login</servlet-name> 
    <servlet-class>com.mars.login</servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>login</servlet-name> 
    <url-pattern>/login</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 
<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.4"
 xmlns=""
 xmlns:xsi=""
 xsi:schemalocation="
 ">
  <servlet>
    <description>this is the description of my j2ee component</description>
    <display-name>this is the display name of my j2ee component</display-name>
    <servlet-name>login</servlet-name>
    <servlet-class>com.mars.login</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
login.java
[java]
package com.mars; 
 
import java.io.ioexception; 
import java.util.arraylist; 
 
import javax.servlet.servletexception; 
import javax.servlet.http.httpservlet; 
import javax.servlet.http.httpservletrequest; 
import javax.servlet.http.httpservletresponse; 
 
public class login extends httpservlet { 
 
    public void doget(httpservletrequest request, httpservletresponse response) 
            throws servletexception, ioexception { 
        response.setcontenttype("text/html;charset=gbk"); 
        request.setcharacterencoding("gbk"); 
         
        //string name = request.getparameter("username");  
        string newname = new string(request.getparameter("username").getbytes("iso-8859-1"),"gbk"); 
        string password1 = request.getparameter("password1"); 
        string password2 = request.getparameter("password2"); 
         
        arraylist l = new arraylist(); 
        if (newname.equals("") || newname.equals(null)) { 
            l.add("用户不为空"); 
 
        } 
        if (password1== null || password1.length() < 4 || password1.length() > 10) { 
            l.add("密码的长度4-10位"); 
 
        } 
        if (password2 == null || password2.length() < 4 || password2.length() > 10) { 
            l.add("确认密码的长度4-10位"); 
 
        } 
        if (password1 != null && password2 != null 
                && !password1.equals(password2)) { 
            l.add("密码和确认密码应该一致"); 
 
        } 
        if (l.isempty()) { 
            request.setattribute("name", newname); 
            request.setattribute("password", password1); 
            request.getrequestdispatcher("success.jsp").forward(request, response); 
 
        } else { 
            request.setattribute("error", l); 
            request.getrequestdispatcher("error.jsp").forward(request, response); 
 
        } 
    } 
 
    public void dopost(httpservletrequest request, httpservletresponse response) 
            throws servletexception, ioexception { 
        system.out.println("调用dopost"); 
        doget(request, response); 
    } 




摘自 mars学it
 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网