验证中...
片段 1 片段 2 片段 3 片段 4 片段 5 片段 6 片段 7 片段 8
FileAction.java
原始数据 复制代码
public class FilterAction implements Filter { // 实现Filter接口并复写它的生命周期方法
// 过滤器销毁执行该方法
public void destroy() {
System.out.println("过滤器销毁啦!");
}
// 过滤请求
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
// 使用该对象获取客户端请求
HttpServletRequest req = (HttpServletRequest) request;
// url请求地址字符串
String servletPath = req.getServletPath();
// 转发页面的path路径
String path = null;
// 判断客户端请求地址是否是userAdd
if ("/userAdd.action".equals(servletPath)) {
path = "/WEB-INF/pages/userAdd.jsp";
}
// 判断是否添加数据
if ("/save.action".equals(servletPath)) {
// 抓取数据
String userName = req.getParameter("userName");
String userDesc = req.getParameter("userDesc");
String userAge = req.getParameter("userAge");
// 封装数据
User user = new User(10086, userName, userDesc, userAge);
// 执行保存略
// 存储到request域中取出
request.setAttribute("user", user);
path = "/WEB-INF/pages/userShow.jsp";
}
// 如果path路径不为null转发页面
if (path != null) {
req.getRequestDispatcher(path).forward(req, response);
// 结束
return;
}
chain.doFilter(req, response);
}
// 过滤器初始化执行该方法
public void init(FilterConfig filterConfig) {
System.out.println("过滤器初始化啦!");
}
}
User.class
原始数据 复制代码
public class User {
private Integer userId;
private String userName;
private String userDesc;
private String userAge;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserDesc() {
return userDesc;
}
public void setUserDesc(String userDesc) {
this.userDesc = userDesc;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", userDesc=" + userDesc + ", userAge=" + userAge
+ "]";
}
public User(Integer userId, String userName, String userDesc, String userAge) {
super();
this.userId = userId;
this.userName = userName;
this.userDesc = userDesc;
this.userAge = userAge;
}
}
web.xml
原始数据 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FilterDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>filterAction</filter-name>
<filter-class>cn.zy.filter.action.FilterAction</filter-class>
</filter>
<filter-mapping>
<filter-name>filterAction</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
userAdd.jsp
原始数据 复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="save.action" method="post">
userName:<input type="text" name="userName"><br>
userDesc:<input type="text" name="userDesc"><br>
userAge:<input type="text" name="userAge"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
addShow.jsp
原始数据 复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
userId:${requestScope.user.userId }
<br>
<br> userName:${requestScope.user.userName }
<br>
<br> userDesc:${requestScope.user.userDesc }
<br>
<br> userAge:${requestScope.user.userAge }
</body>
</html>
页面跳转.png
add.png
result.png

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close