編程:編寫服務器端驗證的程序
要求:(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
[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="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>
<?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
[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