简单的filter
1.创建index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="product_input.action">to product_input</a>
</body>
</html>
2.创建filter
package com.hgh.strust1.filtet;
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
/**
* Servlet Filter implementation class FilterDispach
*/
//@WebFilter("/FilterDispach")
public class FilterDispach implements Filter {
/**
* Default constructor.
*/
public FilterDispach() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
HttpServletRequest req = (HttpServletRequest) request;
String path = req.getServletPath();
System.out.println(path);
String url="";
if (path.equals("/product_input.action")) {
url = "/WEB-INF/views/input.jsp";
req.getRequestDispatcher(url).forward(req, response);
return;
}
if (path.equals("/product_creat.action")) {
url="/WEB-INF/views/details.jsp";
String productName = req.getParameter("productName");
String productPrice = req.getParameter("productPrice");
String productDest = req.getParameter("productDest");
Product product = new Product(12, productName, Double.parseDouble(productPrice), productDest);
req.setAttribute("product", product);
System.out.println(product.toString());
req.getRequestDispatcher(url).forward(req, response);
return;
}
// pass the request along the filter chain
//Cannot call sendError() after the response has been committed
//chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
3.在配置文件中配置filter
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>struts1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<display-name>FilterDispach</display-name>
<filter-name>FilterDispach</filter-name>
<filter-class>com.hgh.strust1.filtet.FilterDispach</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterDispach</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
4,编写输入页面和回显页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="product_creat.action" method="post">
productName:<input type="text" name="productName">
<br>
productPrice:<input type="text" name="productPrice">
<br>
productDest<input type="text" name="productDest">
<br>
<input type="submit" value="submit">
</form>
input page
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
productid:${requestScope.product.id}
<br/>
productName:${requestScope.product.productName}
<br/>
productPrice:${requestScope.product.productPrice}
<br/>
productDest:${requestScope.product.productDest}
<br/>
details
</body>
</html>
5,Pojo
package com.hgh.strust1.filtet;
public class Product {
private int id;
private String productName;
private double productPrice;
private String productDest;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public String getProductDest() {
return productDest;
}
public void setProductDest(String productDest) {
this.productDest = productDest;
}
public Product(int id, String productName, double productPrice,
String productDest) {
super();
this.id = id;
this.productName = productName;
this.productPrice = productPrice;
this.productDest = productDest;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Product [id=" + id + ", productName=" + productName
+ ", productPrice=" + productPrice + ", productDest="
+ productDest + "]";
}
}