• 《Head First Servlets & JSP》-6-会话管理-listener etc. demo


    工程结构

    上下文参数示例

    示例程序展示了如何从上下文读取参数,并在上下文监听器中生成属性对象和在上下文中设置属性。

    • 建立一个简单的JavaBean对象作为属性:Dog.java
    1. package com.example.entities;
    2. /**
    3. * Created by LTN on 2016/11/2.
    4. */
    5. public class Dog {
    6. private String breed;
    7. public Dog(String breed) {
    8. this.breed = breed;
    9. }
    10. public String getBreed() {
    11. return breed;
    12. }
    13. }
    • 建立一个servlet上下文监听器DogServletContextListener.java
    1. package com.example.listener;
    2. import com.example.entities.Dog;
    3. import javax.servlet.ServletContext;
    4. import javax.servlet.ServletContextEvent;
    5. /**
    6. * Created by LTN on 2016/11/2.
    7. */
    8. public class DogServletContextListener implements javax.servlet.ServletContextListener {
    9. public void contextInitialized(ServletContextEvent event) {
    10. ServletContext sc = event.getServletContext();
    11. String dogBreed = sc.getInitParameter("breed");
    12. Dog d = new Dog(dogBreed);
    13. sc.setAttribute("dog", d);
    14. }
    15. public void contextDestroyed(ServletContextEvent event) {
    16. //do nothing.
    17. }
    18. }
    • 建立Servlet进行测试ContextListenerTester.java
    1. package com.example.servlet;
    2. import com.example.entities.Dog;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.io.PrintWriter;
    9. /**
    10. * Created by LTN on 2016/11/2.
    11. */
    12. public class ContextListenerTester extends HttpServlet {
    13. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    14. response.setContentType("text/html");
    15. PrintWriter out = response.getWriter();
    16. out.println("test context attributes set by listener<br>");
    17. out.println("<br>");
    18. Dog dog = (Dog) getServletContext().getAttribute("dog");
    19. out.println("Dog's breed is: " + dog.getBreed());
    20. }
    21. }
    • 在DD(web.xml)中配置上下文参数、监听器和servlet映射关系
    1. <web-app>
    2. <display-name>Archetype Created Web Application</display-name>
    3. <context-param>
    4. <param-name>breed</param-name>
    5. <param-value>Great Dane</param-value>
    6. </context-param>
    7. <servlet>
    8. <servlet-name>ListenerTester</servlet-name>
    9. <servlet-class>com.example.servlet.ContextListenerTester</servlet-class>
    10. </servlet>
    11. <servlet-mapping>
    12. <servlet-name>ListenerTester</servlet-name>
    13. <url-pattern>/ListenTest.do</url-pattern>
    14. </servlet-mapping>
    15. <listener>
    16. <listener-class>com.example.listener.DogServletContextListener</listener-class>
    17. </listener>
    18. </web-app>
    1. test context attributes set by listener
    2. Dog's breed is: Great Dane

    Session属性监听器

    使用Session属性监听器对Session中属性进行监听,当Session属性发生增加、删除或改变时,会被该监听器捕捉。

    • 创建一个Session属性监听类DogSessionAttributeListener.java
    1. package com.example.listener;
    2. import javax.servlet.http.HttpSessionAttributeListener;
    3. import javax.servlet.http.HttpSessionBindingEvent;
    4. /**
    5. * Created by LTN on 2016/11/2.
    6. */
    7. public class DogSessionAttributeListener implements HttpSessionAttributeListener {
    8. public void attributeAdded(HttpSessionBindingEvent event) {
    9. String name = event.getName();
    10. Object value = event.getValue();
    11. System.out.println("Attribute added: " + name + ": " + value);
    12. }
    13. public void attributeRemoved(HttpSessionBindingEvent event) {
    14. String name = event.getName();
    15. Object value = event.getValue();
    16. System.out.println("Attribute removed: " + name + ": " + value);
    17. }
    18. public void attributeReplaced(HttpSessionBindingEvent event) {
    19. String name = event.getName();
    20. Object value = event.getValue();
    21. System.out.println("Attribute replaced: " + name + ": " + value);
    22. }
    23. }
    • 建立一个Servlet测试类AttributeListenerTester.java
    1. package com.example.servlet;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import javax.servlet.http.HttpSession;
    7. import java.io.IOException;
    8. /**
    9. * Created by LTN on 2016/11/2.
    10. */
    11. public class AttributeListenerTester extends HttpServlet {
    12. private static int labelCounter = 0;
    13. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    14. HttpSession session = request.getSession();
    15. session.setAttribute("label", "label" + labelCounter++);
    16. }
    17. }
    • 在DD中配置servlet和listener
    1. <servlet>
    2. <servlet-name>AttributeListenTester</servlet-name>
    3. <servlet-class>com.example.servlet.AttributeListenerTester</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <servlet-name>AttributeListenTester</servlet-name>
    7. <url-pattern>/AttributeListenerTest.do</url-pattern>
    8. </servlet-mapping>
    9. <listener>
    10. <listener-class>com.example.listener.DogSessionAttributeListener</listener-class>
    11. </listener>
    1. Attribute added: label: label0

    然后连续三次刷新,后台依次打印:

    1. Attribute replaced: label: label0
    2. Attribute replaced: label: label1
    3. Attribute replaced: label: label2

    注意,event.getName()和event.getValue()都是获取该事件之前的session属性的值。

    统计全局活动的对话

    使用HttpSessionListener进行全局活动会话的统计,类似统计在线人数功能。

    • 建立全局活动会话监听器
    1. package com.example.listener;
    2. import javax.servlet.http.HttpSessionEvent;
    3. import javax.servlet.http.HttpSessionListener;
    4. /**
    5. * Created by LTN on 2016/11/2.
    6. */
    7. public class GlobalCounterListener implements HttpSessionListener {
    8. static private int activeSessions;
    9. public static int getActiveSessions() {
    10. return activeSessions;
    11. }
    12. public void sessionCreated(HttpSessionEvent event) {
    13. activeSessions++;
    14. }
    15. public void sessionDestroyed(HttpSessionEvent event) {
    16. activeSessions--;
    17. }
    18. }
    • 建立servlet测试类GlobalCounterTester.java
    1. package com.example.servlet;
    2. import com.example.listener.GlobalCounterListener;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import javax.servlet.http.HttpSession;
    8. import java.io.IOException;
    9. import java.io.PrintWriter;
    10. /**
    11. * Created by LTN on 2016/11/2.
    12. */
    13. public class GlobalCounterTester extends HttpServlet {
    14. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    15. PrintWriter out = response.getWriter();
    16. HttpSession session = request.getSession();
    17. session.setAttribute("name","counter");
    18. String name=(String)session.getAttribute("name");
    19. int activeSessions= GlobalCounterListener.getActiveSessions();
    20. out.println("name: "+name);
    21. out.println("activeSessions: "+activeSessions);
    22. }
    23. }
    • 在DD中配置servlet和listener
    1. <servlet>
    2. <servlet-name>SessionCounterTester</servlet-name>
    3. <servlet-class>com.example.servlet.GlobalCounterTester</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <servlet-name>SessionCounterTester</servlet-name>
    7. <url-pattern>/CounterTest.do</url-pattern>
    8. </servlet-mapping>
    9. <listener>
    10. <listener-class>com.example.listener.GlobalCounterListener</listener-class>
    11. </listener>
    1. name: counter
    2. activeSessions: 1

    换一个浏览器,输入地址,会显示activeSessions数值递增。

    注意,同一个浏览器开新标签不能使得session个数增加;关闭当前浏览器再接着启动,也不能。

    属性绑定监听器

    当属性对象被加入session或从session删除使,对象本身可以知道。

    • 建立Session binding监听器Dog.java,该类继承了HttpSessionBindingListener,并实现valueBound和valueUnbound方法。
    1. package com.example.entities;
    2. import javax.servlet.http.HttpSessionBindingEvent;
    3. import javax.servlet.http.HttpSessionBindingListener;
    4. /**
    5. * Created by LTN on 2016/11/2.
    6. */
    7. public class Dog implements HttpSessionBindingListener {
    8. private String breed;
    9. public Dog(String breed) {
    10. this.breed = breed;
    11. }
    12. public String getBreed() {
    13. return breed;
    14. }
    15. public void valueBound(HttpSessionBindingEvent event) {
    16. System.out.println("I am a dog, i know i am in a conversation now.");
    17. }
    18. public void valueUnbound(HttpSessionBindingEvent event) {
    19. System.out.println("I am a dog, i know i am leaving a conversation now.");
    20. }
    21. }
    • 建立servlet测试类EntityBindingTester.java
    1. package com.example.servlet;
    2. import com.example.entities.Dog;
    3. import javax.servlet.ServletException;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import javax.servlet.http.HttpSession;
    8. import java.io.IOException;
    9. /**
    10. * Created by LTN on 2016/11/2.
    11. */
    12. public class EntityBindingTester extends HttpServlet {
    13. public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException {
    14. Dog dog = (Dog) getServletContext().getAttribute("dog");
    15. HttpSession session = request.getSession();
    16. session.setAttribute("dog-binding",dog);
    17. session.invalidate();
    18. }
    19. }
    • 在DD中配置servlet,注意,属性Dog不用/能在DD中配置
    1. <servlet>
    2. <servlet-name>ObjectBindingTester</servlet-name>
    3. <servlet-class>com.example.servlet.EntityBindingTester</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <servlet-name>ObjectBindingTester</servlet-name>
    7. <url-pattern>/ObjectBindingTest.do</url-pattern>
    8. </servlet-mapping>
    1. I am a dog, i know i am in a conversation now.
    2. Attribute added: dog-binding: com.example.entities.Dog@6dbba86e
    3. I am a dog, i know i am leaving a conversation now.
    4. Attribute removed: dog-binding: com.example.entities.Dog@6dbba86e

    附件列表

    • 相关阅读:
      【习题整理】分块+莫队(未完待续)
      【bzoj4198】【Noi2015】荷马史诗
      【bzoj2006】【NOI2015】超级钢琴
      【bzoj1029】【JSOI2007】建筑抢修
      【bzoj1483】【HNOI2009】梦幻布丁
      【bzoj4195】【NOI2015】程序自动分析
      Rearrangement inequality
      hdu1047
      hdu1046
      hdu1045
    • 原文地址:https://www.cnblogs.com/myitroad/p/6192526.html
    Copyright © 2020-2023  润新知