过滤器及监听器
过滤器
Filter即过滤,用于对请求进行预处理
使用Filter的完整流程: Filter对用户请求进行预处理->接着将请求交给Servlet进行处理并生成响应->最后Filter再对服务器响应做处理
使用
通过实现javax.servlet.Filter接口来实现一个过滤器。
步骤:
- 编写java类实现Filter接口,并实现其doFilter方法。
- 配置web.xml方法
public class Filter01 implements Filter{
//初始化,服务器启动时直接初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("filter执行了,请求到达资源之前处理的代码...");
//放行
chain.doFilter(request, response);
System.out.println("filter执行了,请求到达资源之后处理的代码...");
}
//销毁,正常关闭服务器
@Override
public void destroy() {
}
}
<filter>
<filter-name>f01</filter-name>
<filter-class>com.mage.filter.Filter01</filter-class>
</filter>
<filter-mapping>
<filter-name>f01</filter-name>
<url-pattern>/s01</url-pattern><!--需要过滤的资源名 -->
</filter-mapping>
处理字符乱码
/**
* GET:
请求:
Tomcat8及以上,不需要处理
Tomcat7及以下,需要处理乱码
new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
响应:
response.setContentType("text/html;charset=UTF-8");
POST:
请求:
Tomcat8及以上,需要处理乱码,request.setCharacterEncoding("UTF-8");
Tomcat7及以下,需要处理乱码,request.setCharacterEncoding("UTF-8");
响应:
response.setContentType("text/html;charset=UTF-8");
*/
public class EncodeFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//基于HttpServlet
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp =(HttpServletResponse)response;
//设置post方式请求和响应编码
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
//获取服务器版本
String serverInfo = req.getServletContext().getServerInfo();
// Apache Tomcat/8.0.52
//得到tomcat版本号
String Info = serverInfo.substring(serverInfo.indexOf("/")+1, serverInfo.indexOf("/")+2);
//判断tomcat版本是否是7及以下
if(Info != null && Integer.parseInt(Info) <= 7) {
//判断请求方式
String method = req.getMethod();
if("get".equalsIgnoreCase(method)) {
MyWrapper myWrapper = new MyWrapper(req);
chain.doFilter(myWrapper, resp);
}
}
chain.doFilter(req, resp);
}
public void init(FilterConfig fConfig) throws ServletException {
}
/**
* 定义内部类MyWrapper继承HttpServletRequestWrapper
* 目的:重写getParameter方法
*/
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 {
value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
}
}
监听器
web 监听器是一种 Servlet 中的特殊的类,它们能帮助开发者监听 web 中的特定事件,比如
ServletContext,HttpSession,ServletRequest 的创建和销毁;变量的创建、销毁和修改等。可以在某些
动作前后增加处理,实现监控。例如可以用来统计在线人数等。
使用
@WebListener
public class Listener implements HttpSessionListener {
int online = 0;
public void sessionCreated(HttpSessionEvent se) {
online ++;
se.getSession().getServletContext().setAttribute("online", online);
}
public void sessionDestroyed(HttpSessionEvent se) {
online --;
se.getSession().getServletContext().setAttribute("online", online);
}
}
<listener>
<listener-class>com.mage.Listener01</listener-class>
</listener>
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int online = (int)request.getSession().getServletContext().getAttribute("online");
response.getWriter().write("<h1>"+online+"</h1>");
}