Filter 会根据url或者servlet-name拦截请求, app启动是调用init(), 关闭时调用destroy()方法:
package com.test;
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;
public class MyFilter implements Filter
{
FilterConfig filterConfig = null;
/**
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy()
{
System.out.println("this is destory");
}
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException
{
arg0.setAttribute("name", "Alex");
System.out.println("this is do filter");
HttpServletRequest req = (HttpServletRequest)arg0;
// ((HttpServletResponse)arg1).sendRedirect(req.getSession());
chain.doFilter(arg0, arg1) ;//转发到对应servlet/Action请求的url
}
/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig arg0) throws ServletException
{
System.out.println("this is init");
this.filterConfig = arg0;
}
}
servlet:
public class MyServlet extends HttpServlet
{
/** serialVersionUID */
private static final long serialVersionUID = 1L;
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
doPost(req, resp);
}
/**
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String name = (String) req.getAttribute("name");
System.out.println("name is :" + name);
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID"
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">
<display-name>IPTV Web Administration</display-name>
<!-- Filters -->
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.neulion.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/test</url-pattern> //拦截对应的servlet-name 或者url
</filter-mapping>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.test.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
输入:http://localhost:8084/myApp/test
输出:
this is do filter
name is :Alex