• 监听器与过滤器练习


    监听器与过滤器练习

    题目:

    实现某电商系统的字符过滤器,要求对所有页面进行字符过滤功能。

    选取合适的监听器实现统计商品列表页的访问人数(注:同一用户多次访问商品列表页时,计数为1次)。

    提交代码要求如下:

    1)提交商品列表页的关键代码;DealServlet

    显示商品列表页的访问人数

    2)提交字符过滤器的关键代码;

    3) 提交相关监听器的关键代码;

    代码如下:

    login.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    <h1>商品列表</h1>
    <a href="d1">点我查看网页访问量</a>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ContextListener

    package listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * Application Lifecycle Listener implementation class ContextListener
     *
     */
    @WebListener
    public class ContextListener implements ServletContextListener {
    
        /**
         * Default constructor. 
         */
        public ContextListener() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
         * @see ServletContextListener#contextDestroyed(ServletContextEvent)
         */
        public void contextDestroyed(ServletContextEvent sce)  { 
             // TODO Auto-generated method stub
        }
    
    	/**
         * @see ServletContextListener#contextInitialized(ServletContextEvent)
         */
        public void contextInitialized(ServletContextEvent sce)  { 
             // TODO Auto-generated method stub
        	ServletContext context=sce.getServletContext();
        	context.setAttribute("userNum", 0);
        }
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    PageListener

    package listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.annotation.WebListener;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * Application Lifecycle Listener implementation class PageListener
     *
     */
    @WebListener
    public class PageListener implements HttpSessionListener {
    
        /**
         * Default constructor. 
         */
        public PageListener() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
         * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
         */
        public void sessionCreated(HttpSessionEvent se)  { 
             // TODO Auto-generated method stub
        	System.out.println("Session创建-------------------");
        	ServletContext context=se.getSession().getServletContext();
        	int count=(int)context.getAttribute("userNum");
        	count++;
        	context.setAttribute("userNum", count);
        	
        }
    
    	/**
         * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
         */
        public void sessionDestroyed(HttpSessionEvent se)  { 
             // TODO Auto-generated method stub
    //    	System.out.println("Session销毁-------------------");
    //    	ServletContext context=se.getSession().getServletContext();
    //    	int count=(int)context.getAttribute("userNum");
    //    	count--;
    //    	context.setAttribute("userNum", count);
        }
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    DealServlet

    package servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class DealServlet
     */
    @WebServlet("/d1")
    public class DealServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public DealServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		request.setCharacterEncoding("utf-8");
    		response.setContentType("text/html;charset=utf-8");
    		
    		PrintWriter writer=response.getWriter();
    		HttpSession session=request.getSession();
    		ServletContext context=getServletContext();
    		int count = (int)context.getAttribute("userNum");
    		writer.write("<h1>访问人数为:</h1>"+count);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    CharacterFilter

    package filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    
    /**
     * Servlet Filter implementation class CharacterFilter
     */
    @WebFilter("/*")
    public class CharacterFilter implements Filter {
    
        /**
         * Default constructor. 
         */
        public CharacterFilter() {
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see Filter#destroy()
    	 */
    	public void destroy() {
    		// TODO Auto-generated method stub
    	}
    
    	/**
    	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
    	 */
    	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    		// TODO Auto-generated method stub
    		// place your code here
    
    		request.setCharacterEncoding("utf-8");
    		
    		response.setContentType("text/html;charset=utf-8");
    		// pass the request along the filter chain
    		chain.doFilter(request, response);
    	}
    
    	/**
    	 * @see Filter#init(FilterConfig)
    	 */
    	public void init(FilterConfig fConfig) throws ServletException {
    		// TODO Auto-generated method stub
    		System.out.println("Filter is initing!");
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    python基础05 循环 变量 函数组合案例
    重生之我是一名程序员 34
    【计算机网络】如何让客户端构造一个HTTP请求
    深入理解Java虚拟机之【方法区】
    部署第三方Jar包到Nexus私服
    一篇文章带你用动态规划解决打家劫舍问题
    亚马逊云代码AI助手CodeWhisperer使用教程
    child_process exec 不是内部或外部命令,也不是可运行的程序或批处理文件。
    【Linux】Linux命令大全——解压、目录、文件、搜索等
    redisson有几种分布式算法
  • 原文地址:https://blog.csdn.net/huayu8086/article/details/125561015