在input.jsp页面输入表单信息,details.jsp中显示表单信息。Filter类进行逻辑处理,根据servletPath来执行不同的操作。
input.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'input.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>
<form action="product-save.action" method="post">
ProductName :<input type="text" name="productName"/><br>
ProductDesc: <input type=" text" name="productDesc"/><br>
ProductPrice: <input type=" text" name="productPrice"/><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
details.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'details.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>
ProductName :${requestScope.product.productName} <br>
ProductId :${requestScope.product.productId} <br>
ProductDesc :${requestScope.product.productDesc} <br>
ProductPrice :${requestScope.product.productPrice} <br>
</body>
</html>
Filter类:
public class FilterDispatcher implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
//1.获取servlet的Path
String serlvetPath=req.getServletPath();
System.out.println(serlvetPath);
String path=null;
//2.判断servletPath,若等于“product-input.action”则转发到input.jsp
if("/product-input.action".equals(serlvetPath)){
path="/WEB-INF/pages/input.jsp";
}
//3.若等于save则
if("/product-save.action".equals(serlvetPath)){
String productName= request.getParameter("productName");
String productDesc= request.getParameter("productDesc");
String productPrice= request.getParameter("productPrice");
Product product = new Product(null,productName,productDesc,Double.parseDouble(productPrice));
System.out.println("Save:"+ product.getProductName());
product.setProductId(Integer.valueOf(1001));
//将Product保存到 request中
request.setAttribute("product", product);
path="/WEB-INF/pages/details.jsp";
}
if (path != null) {
request.getRequestDispatcher(path).forward(request, response);
return;
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>FilterDispatcher</display-name>
<filter-name>FilterDispatcher</filter-name>
<filter-class>com.hcx.struts2.helloWorld.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDispatcher</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
Servlet VS Filter
1.servlet 能做的Filter都能完成
2.Filter 独有FilterChain,这个API在Servlet中不存在,filter能进行拦截