(此外,EL表达式与JSTL表达式也可以使用。)
1.使用xml文件管理程序文件对应关系
3.异常处理机制:通过配置来完成,更便于管理
采用 Servlet 和 Struts2 实现注册功能流程图
package filt_Pack_001; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import strus_Pack_001.MyUser; public class Ctrl_Filter_Cls_001 implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub // 获取 HttpServletRequest 对象 HttpServletRequest hsr = (HttpServletRequest)arg0; // 1 接收用户请求的地址 String url = hsr.getServletPath(); // 2 判断用户请求的地址,并执行相应的程序 // 在过滤器中编写跳转 需要有返回 return if(url.equals("/hsr.action")){ // 如果接收到的请求是注册的,就跳转到注册界面 hsr.getRequestDispatcher("register.jsp").forward(arg0, arg1);; return; }else if (url.equals("/saveUser.action")) { // 获取表单数据 String username = hsr.getParameter("username"); String pwd0 = hsr.getParameter("pw0"); String pwd1 = hsr.getParameter("pw1"); // 验证密码的一致性 if(pwd0.equals(pwd1)){ // 相等则封装到 user 对象之中去 MyUser us = new MyUser(); us.setUsernameString(username); us.setPassword(pwd1); // 执行 相关 DAO 持久化操作 // 注册成功就跳转到注册成功页面 hsr.getRequestDispatcher("end.jsp").forward(arg0, arg1); // 在过滤器中编写跳转 需要有返回 return return; } else { // 如果跳转不成功 就返回原来的注册页面 hsr.getRequestDispatcher("register.jsp").forward(arg0, arg1); return; } } // 3 跳转到相应的结果页面 ,让过滤器执行 以完成核心过滤器的执行 arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
package strus_Pack_001; public class MyUser { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsernameString() { return username; } public void setUsernameString(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>Ctrl_Filter_Cls_001</filter-name> <filter-class>filt_Pack_001.Ctrl_Filter_Cls_001</filter-class> </filter> <filter-mapping> <filter-name>Ctrl_Filter_Cls_001</filter-name> <!-- 配置过滤器 过滤掉所有的请求 --> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<%@ 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> </head> <body> <h3> 恭喜您注册成功! </h3> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" 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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> </head> <body> <!-- 链接 为 register。action --> <a href="hsr.action">注册</a> </body> </html>
<%@ 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> </head> <body> <!-- 编写表单数据 --> <form action="saveUser.action" method="post"> <input type="text" name="username"> <input type="password" name="pw0"> <input type="password" name="pw1"> <input type="submit" value="提交"> </form> </body> </html>