过滤器
单个过滤器
单个过滤器流程:客户端→服务器→过滤器chain.doFilter(request, response)请求到达资源之前的代码
→要访问的资源→过滤器chain.doFilter(request, response)响应到达客户端之后的代码→客户端
多个过滤器
若是一个过滤器链:先配置先执行(请求时的执行顺序);响应时:以相反的顺序执行。
在 HttpServletRequest 到达 Servlet 之前,拦截客户的 HttpServletRequest 。根据需要检查
HttpServletRequest,也可以修改 HttpServletRequest 头和数据。
在HttpServletResponse 到达客户端之前,拦截 HttpServletResponse。根据需要检查
HttpServletResponse,也可以修改 HttpServletResponse 头和数据。
实现
package com.mage.filter;
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;
public class Filter01 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter01初始化了。。。");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("Filter01请求到达资源前执行的code");
// **放行**
chain.doFilter(request, response);
System.out.println("Filter01响应到达客户端前执行的code");
}
@Override
public void destroy() {
System.out.println("Filter01销毁了。。。");
}
}
<filter>
<filter-name>filter01</filter-name><!-- 给服务器看的 -->
<filter-class>com.mage.filter.Filter01</filter-class><!-- 设置资源路径 -->
</filter>
<filter-mapping>
<filter-name>filter01</filter-name><!-- 给服务器看的 -->
<url-pattern>/s01</url-pattern><!-- 设置要拦截的路径 -->
</filter-mapping>
过滤器处理字符乱码
package com.mage.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class EncodeFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
// 基于Http
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
// 设置POST请求编码以及响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取服务器版本
String serverInfo = request.getServletContext().getServerInfo();
// Apache Tomcat/8.0.52
System.out.println(serverInfo);
// 得到版本号 **左闭右开**
String info = serverInfo.substring(serverInfo.indexOf("/")+1,serverInfo.indexOf("/")+2);
// 判断Tomcat版本是否是7及以下
if(info != null && Integer.parseInt(info) <= 7){
// 判断请求方式
String method = request.getMethod();
System.out.println(method);
if("get".equalsIgnoreCase(method)){
HttpServletRequest myWrapper = new MyWrapper(request);
// 放行
chain.doFilter(myWrapper, response);
return;
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
}
/**
* 定义内部类MyWrapper继承HttpServletRequestWrapper
* 目的:重写getParameter方法
* @author Cushier
*
*/
class MyWrapper extends HttpServletRequestWrapper{
HttpServletRequest request;
public MyWrapper(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
String value = null;
try {
if(request.getParameter(name)!=null){
value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}
}
监听器
package com.mage.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class Listener01 implements HttpSessionListener {
int onlinePeople = 0;
/**
* 监听SESSION创建
*/
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session创建了。。。");
}
/**
* 监听SESSION销毁
* 正常关闭服务器不会销毁,因为会自动序列化到本地
*/
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("session销毁了。。。");
}
}
<listener>
<listener-class>com.mage.listener.Listener01</listener-class>
</listener>
Servlet3.0 注解
@WebServlet
@WebServlet("/s01")
@WebServlet(name = "servlet01",value = "/s01")
@WebServlet(name = "servlet01",urlPatterns = {"/s01","/s001"})
@WebServlet(loadOnStartup=1,urlPatterns={"/s001","/s003"})
@WebFilter
@WebFilter(filterName = "filter01",urlPatterns = "/*")
@WebListener
Servlet3.0 提供 @WebListener 注解将一个实现了特定监听器接口的类定义为监听器。比如将实现
了 ServletSessionListener 接口的类标注为监听器。
@WebListener
@MultipartConfig
@WebServlet("/upload")
@MultipartConfig