Struts实现注册功能

 

ControlFilter.java



1 package com.jikexueyuan.filter;
2
3 import java.io.IOException;
4
5 import javax.servlet.Filter;
6 import javax.servlet.FilterChain;
7 import javax.servlet.FilterConfig;
8 import javax.servlet.ServletException;
9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.HttpServletRequest;
12
13 import com.jikexueyuan.pojo.users;
14
15
16
17 public class ControlFilter implements Filter {
18
19 public void destroy() {
20 // TODO Auto-generated method stub
21
22 }
23
24 public void doFilter(ServletRequest arg0, ServletResponse arg1,
25 FilterChain arg2) throws IOException, ServletException {
26 HttpServletRequest request = (HttpServletRequest) arg0;
27 //1接收用户请求的地址
28 String url = request.getServletPath();
29 //2判断用户请求的地址,并执行相应的程序
30 if(url.equals("/register.action")){
31 request.getRequestDispatcher("register.jsp").forward(arg0, arg1);
32 return;
33 }else if(url.equals("/saveUser.action")){
34 String username = request.getParameter("username");
35 String password = request.getParameter("password");
36 String password1 = request.getParameter("password1");
37 if(password.equals(password1)){
38 users us = new users();
39 us.setName(username);
40 us.setPassword(password);
41 //执行相关DAO持久化操作
42 request.getRequestDispatcher("end.jsp").forward(arg0, arg1);
43 return;
44 }else{
45 request.getRequestDispatcher("register.jsp").forward(arg0, arg1);
46 return;
47 }
48 }
49 //3跳转到相应的结果页面
50 arg2.doFilter(arg0, arg1);
51 }
52
53 public void init(FilterConfig arg0) throws ServletException {
54 // TODO Auto-generated method stub
55
56 }
57
58 }


 

 

users.java



1 package com.jikexueyuan.pojo;
2
3 public class users {
4
5 private int id;
6 private String name;
7 private String password;
8 public int getId() {
9 return id;
10 }
11 public void setId(int id) {
12 this.id = id;
13 }
14 public String getName() {
15 return name;
16 }
17 public void setName(String name) {
18 this.name = name;
19 }
20 public String getPassword() {
21 return password;
22 }
23 public void setPassword(String password) {
24 this.password = password;
25 }
26
27 }


 

index.jsp



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.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>
<a href="register.action">注册</a>
</body>
</html>


register.jsp



1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'register.jsp' starting page</title>
13
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22
23 </head>
24
25 <body>
26
27 <form action="saveUser.action" method="post">
28 <input type="text" name="username"/><br>
29 <input type="password" name="password"/><br>
30 <input type="password" name="password1"/><br>
31 <input type="submit" value="提交">
32 </form>
33
34 </body>
35 </html>


end.jsp



1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'end.jsp' starting page</title>
13
14 <meta http-equiv="pragma" content="no-cache">
15 <meta http-equiv="cache-control" content="no-cache">
16 <meta http-equiv="expires" content="0">
17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18 <meta http-equiv="description" content="This is my page">
19 <!--
20 <link rel="stylesheet" type="text/css" href="styles.css">
21 -->
22
23 </head>
24
25 <body>
26 <h3>注册成功<h3>
27 </body>
28 </html>


 

 

web.xml 配置



1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7 <welcome-file-list>
8 <welcome-file>index.jsp</welcome-file>
9 </welcome-file-list>
10
11 <filter>
12 <filter-name>ControlFilter</filter-name>
13 <filter-class>com.jikexueyuan.filter.ControlFilter</filter-class>
14 </filter>
15 <filter-mapping>
16 <filter-name>ControlFilter</filter-name>
17 <url-pattern>/*</url-pattern>
18 </filter-mapping>
19
20 </web-app>