过滤器:用来过滤网站的数据
- 处理中文乱码
- 登录验证
- etc…
开发步骤:
-
导包,创建实现类,实现Filter接口(java.servlet包下的filter)
-
编写过滤器,重写三个方法,实现业务逻辑
package com.ola.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * ClassName:ShowServlet Package:com.ola.servlet * * @author morningj * @date 2021/1/9 11:57 */ public class ShowServlet extends HttpServlet { private static final long serialVersionUID = -3255769486004657840L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("utf-8"); resp.getWriter().write("hello!你好世界!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
package com.ola.filter; import javax.servlet.*; import java.io.IOException; /** * ClassName:CharacterEncodingFilter Package:com.ola.filter * * @author morningj * @date 2021/1/9 11:52 */ public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("已执行初始化操作!"); } @Override public void destroy() { System.out.println("已执行销毁操作!"); } @Override public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); servletResponse.setContentType("text/html;charset=UTF-8"); System.out.println("CharacterEncodingFilter执行前......"); filterChain.doFilter(servletRequest, servletResponse); System.out.println("CharacterEncodingFilter执行后......"); } }
-
web.xml中配置Filter
注意:
-
doFilter中的所有代码,在过滤特定请求的时候都会执行
-
必须要让过滤器继续通行,使用:
filterChain.doFilter(servletRequest, servletResponse);
监听器The listener
package com.ola.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* ClassName:OnlineCountListener Package:com.ola.listener
* description:
* 实现监听在线用户人数,通过session会话的数量判断;
* @author morningj
* @date 2021/1/9 15:50
*/
public class OnlineCountListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
if (onlineCount == null) {
onlineCount = new Integer(1);
} else {
int count = onlineCount.intValue();
onlineCount = new Integer(count + 1);
}
servletContext.setAttribute("OnlineCount", onlineCount);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
if (onlineCount == null) {
onlineCount = new Integer(1);
} else {
int count = onlineCount.intValue();
onlineCount = new Integer(count - 1);
}
servletContext.setAttribute("OnlineCount", onlineCount);
}
}
<listener>
<listener-class>com.ola.listener.OnlineCountListener</listener-class>
</listener>