过滤器和监听器
过滤器:
对过滤的所有文件设置字符编码
package Filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import javax.servlet.annotation.WebFilter; //要过滤的文件路径 @WebFilter("/*") public class CharacterEncodeing implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { //初始化过滤器,由jvm自动加载 } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; //设置字符编码 req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); //设置响应时的数据格式 resp.setContentType("text/html;charset=utf-8"); filterChain.doFilter(req,resp); } @Override public void destroy() { //销毁过滤器,由jvm自动加载 } }
过滤目录下的jsp页面是否已经登录,否则跳转会登录页面
package Filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import javax.servlet.annotation.WebFilter; @WebFilter("/newspages/*") public class LoginFilter implements Filter {//过滤器 @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse resp = (HttpServletResponse) servletResponse; Object uname = req.getSession().getAttribute("uname"); if(uname!=null){ filterChain.doFilter(req,resp); }else{ resp.sendRedirect(req.getContextPath()+"/IndexServlet"); } } @Override public void destroy() { } }
监听器:
import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; /** * 监听session作用域变化的监听器 */ @WebListener public class AttributeListener implements HttpSessionAttributeListener{ @Override public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) { //判断哪个用户完成了登录操作 System.err.println("key:"+httpSessionBindingEvent.getName()+",用户:"+httpSessionBindingEvent.getValue()+"进行了登录操作"); } @Override public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) { //移除作用域中的值 System.err.println("key:"+httpSessionBindingEvent.getName()+",用户:"+httpSessionBindingEvent.getValue()+"进行了注销操作"); } @Override public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) { } }
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import java.util.Arrays; import java.util.List; @WebListener public class FirstListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent servletContextEvent) { //在servletContext对象初始化时执行的操作 List<String> strings = Arrays.asList("张三", "李四", "王二麻子"); //把lists集合存入到ServletContext作用域 servletContextEvent.getServletContext().setAttribute("lists",strings); } @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { System.out.println("ServletContext对象已销毁===="); } }