过滤器 Filter
当访问服务器的资源时,过滤器可以将请求拦截下来,完成开发者所达成的效果。
eg: 处理中文乱码、登录验证…
开发步骤
编写过滤器
a.实现接口 implements Filter(import javax.servlet.Filter; !!!!)
b.实现方法
c.配置拦截路径
eg:
解决中文乱码
Filter.class
public class FilterDemo implements Filter {
//初始化
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化...");
}
//Chain : 链
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;chatset=UTF-8");
//让请求继续走,若不写则程序到此结束-->放行
filterChain.doFilter(servletRequest,servletResponse);
}
//销毁
public void destroy() {
System.out.println("销毁...");
}
}
Servler.class
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//未加setCharact...方法回乱码
resp.getWriter().write("你好哇!世界!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.rz.servlet.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!--filter的设置-->
<filter>
<filter-name>CharacterFilter</filter-name>
<filter-class>com.rz.servlet.FilterDemo</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
监听器
实现监听器接口
Listen.class
//统计在线人数=统计session
public class ListenDemo implements HttpSessionListener {
//创建session监听
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer num = (Integer) servletContext.getAttribute("num");
if (num == null){
num = new Integer(1);
}else {
int count = num.intValue();
num = new Integer(count+1);
}
servletContext.setAttribute("num",num);
}
//销毁session监听
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer num = (Integer) servletContext.getAttribute("num");
if (num == null){
num = new Integer(0);
}else {
int count = num.intValue();
num = new Integer(count-1);
}
servletContext.setAttribute("num",num);
}
}
注册监听器
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<!--注册监听器-->
<listener>
<listener-class>com.rz.servlet.ListenDemo</listener-class>
</listener>
</web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP</title>
</head>
<body>
<p>当前有 <i><%=this.getServletConfig().getServletContext().getAttribute("num")%></i> 人在线</p>
</body>
</html>