一。web监听器:监听特殊事件的发生
1.监听实现步骤 a。写一个java类,实现特定的接口,重写相关方法 b。在web.xml中,牌配置 <listener> <listener-class>com.listener.MyContextLisenter</listener-class> </listener> c.注解方式:在java类上写一个@WebLisenter2.可以在web.xml中可以配置servetContext初始化的参数 <context-param> <param-name>basePath</param-name> <param-value>http://localhost:8888/StudentManager/</param-value> </context-param> 在程序中获取方式:sce.getServletContext().getInitParameter("basePath");3.三种监听:请求监听(ServletRequestListener),会话监听(HttpSessionListener),上下文监听(ServletContextListener)
二。过滤器
1、特点:在服务器中第一个被执行,可以拦截任意指定的路径请求2.用于项目中的非法访问,字符集设置等等3.实现步骤: a.写一个java程序,实现Filter接口,重写方法 b.在web.xml中配置 <filter> <filter-name>charfilter</filter-name> <filter-class>com.filter.CharSetFilter</filter-class> </filter> <filter-mapping> <filter-name>charfilter</filter-name> <!-- 拦截根目录下的所有请求 --> <url-pattern>/*</url-pattern> </filter-mapping> c.dofilter方法里面的最后一句,应该将请求继续放行china.doFilter(req,resp);
web.xml配置文件
javaweb06 basePath http://localhost:8888/StudentManager/ charset com.filter.CharSetFilter charset /* loginservlet com.control.LoginServlet loginservlet /login loginservlet2 com.control.LoginServlet2 loginservlet2 /login2 index.html index.htm index.jsp default.html default.htm default.jsp
实例1:
package com.listener;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;public class MyRequestListener implements ServletRequestListener{ @Override public void requestInitialized(ServletRequestEvent sre) { sre.getServletContext(); sre.getServletRequest(); System.out.println("请求开始"); } @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("请求结束"); }}
实例2:
package com.listener;import javax.servlet.ServletContext;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;@WebListenerpublic class MySessionListener implements HttpSessionListener{ @Override public void sessionCreated(HttpSessionEvent se) { String path = se.getSession().getServletContext().getInitParameter("basePath"); System.out.println("初始化参数为:"+path); ServletContext app = se.getSession().getServletContext(); Integer online = (Integer)app.getAttribute("online"); if(online==null){ online = 0; } System.out.println("当前在线人数是:"+ ++online); app.setAttribute("online", online); } @Override public void sessionDestroyed(HttpSessionEvent se) { ServletContext app = se.getSession().getServletContext(); Integer online = (Integer)app.getAttribute("online"); System.out.println("当前在线人数为:"+ --online); app.setAttribute("online", online); }}
实例3:
package com.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebFilter("/student/*")public class IsLoginFilter implements Filter{ @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2) throws IOException, ServletException { //1.从session中获取用户信息 HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; String username = (String) request.getSession().getAttribute("username"); System.out.println(request.getRequestURI()); if(request.getRequestURI().endsWith("/login.jsp")||request.getRequestURI().endsWith("/login3")){ arg2.doFilter(req, resp); return; } if(username==null){//非法登录 response.sendRedirect("login.jsp"); }else{ arg2.doFilter(req, resp); } }}
实例4:
package com.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.annotation.WebFilter;/** * 将所有请求的字符集统一修改 * @author allen */@WebFilter("/*")public class CharSetFilter implements Filter{ @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2) throws IOException, ServletException { System.out.println("请求字符集修改为utf-8"); req.setCharacterEncoding("utf-8"); arg2.doFilter(req,resp);//将请求继续往下放行 }}