编写类:
package com.neusoft.web;
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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TransNameFilter extends HttpServlet
implements Filter
{
public void init(FilterConfig arg0)
throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest hreq = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
res.setHeader("P3P", "CP=CAO PSA OUR");
if (chain != null)
chain.doFilter(request, response);
}
public void destroy()
{
}
}
添加监听
web.xml
<filter>
<filter-name>transNameFilter</filter-name>
<filter-class>com.neusoft.web.TransNameFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>transNameFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>