servlet服务器端验证

时间:2023-01-21 17:26:30

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

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

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

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

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


├─.myeclipse
├─.settings
├─src
│  └─com
│      └─mars
└─WebRoot
    ├─META-INF
    └─WEB-INF
        ├─classes
        │  └─com
        │      └─mars
        └─lib


登录login.jsp

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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
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);
}
}