一、JSP过滤器&监听器
过滤器
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)arg0;
req.setCharacterEncoding("utf-8");
String path=req.getServletPath();
System.out.println("请求路径:"+path);
System.out.println("经过了字符编码过滤器~~我得意的笑");
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
监听器
private ServletContext application=null;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
application=arg0.getServletContext();
Integer count = 0;
application.setAttribute("count", count);
}
@Override
public void sessionCreated(HttpSessionEvent arg0) {
Integer count=(Integer)application.getAttribute("count");
count++;
application.setAttribute("count", count);
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
Integer count=(Integer)application.getAttribute("count");
count--;
application.setAttribute("count", count);
}
二、配置filter筛选web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>jsp-04</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>
<filter-name>建议填写该配置的类名</filter-name>
<filter-class>筛选完整路径(包名+类名)</filter-class>
</filter>
<filter-mapping>
<filter-name>建议填写该配置的类名</filter-name>
<url-pattern>