java web(6) Filter和监听器
Filter
Filter:过滤器,用来过滤网站的数据;
处理中文乱码
登录验证
- 导包
- 编写过滤器 实现Filter接口,重写对应的方法即可
- import javax.servlet.*;
import java.io.IOException;
public class CharaterEncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
/*
filterChain:链
1.过滤所有的代码,在过滤特定的请求的时候都会执行
2.必须要让过滤器继续同行
*/
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执行后");
}
public void destroy() {
}
3.在web.xml中配置filter映射
<filter>
<filter-name>filter</filter-name>
<filter-class>com.luo.filter.CharaterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
监听器
实现一个监听器有n种
- 编写一个监听器(实现一个监听器的接口)
- import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* 统计网站在线人数:统计session
*/
public class OnlineCountListener implements HttpSessionListener {
//创建session监听,看你的一举一动
//一旦创建一个session就会触发一个这个事件
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount=new Integer(1);
}else {
int count=onlineCount.intValue();
onlineCount=new Integer(count+1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
//销毁session监听
//一旦销毁一个session就会触发一个这个事件
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount=new Integer(0);
}else {
int count=onlineCount.intValue();
onlineCount=new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
}
- web.xml中注册监听器
- <!--
注册监听器-->
<listener>
<listener-class>com.luo.listener.OnlineCountListener</listener-class>
</listener>
- 看情况是否使用
1. 过滤器和监听器的常见应用
用户登录之后才能进入主页!用户注销后就不能进入主页了
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if(request.getSession().getAttribute("USER_SESSION")==null){
response.sendRedirect("/error.jsp");
}
filterChain.doFilter(req,resp);
public class LoginoutServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest
req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session =
req.getSession().getAttribute(LoginSessionId.loginSessionId);
if (user_session!=null){
req.getSession().removeAttribute(LoginSessionId.loginSessionId);
resp.sendRedirect("/Login.jsp");
}else{
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest
req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class LoginoutServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest
req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session =
req.getSession().getAttribute(LoginSessionId.loginSessionId);
if (user_session!=null){
req.getSession().removeAttribute(LoginSessionId.loginSessionId);
resp.sendRedirect("/Login.jsp");
}else{
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest
req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?