首页 > 代码库 > 《Head First Servlets & JSP》-6-会话管理-listener etc. demo
《Head First Servlets & JSP》-6-会话管理-listener etc. demo
工程结构
上下文参数示例
示例程序展示了如何从上下文读取参数,并在上下文监听器中生成属性对象和在上下文中设置属性。
- 建立一个简单的JavaBean对象作为属性:Dog.java
package com.example.entities;
/**
* Created by LTN on 2016/11/2.
*/
public class Dog {
private String breed;
public Dog(String breed) {
this.breed = breed;
}
public String getBreed() {
return breed;
}
}
- 建立一个servlet上下文监听器DogServletContextListener.java
package com.example.listener;
import com.example.entities.Dog;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
/**
* Created by LTN on 2016/11/2.
*/
public class DogServletContextListener implements javax.servlet.ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);
}
public void contextDestroyed(ServletContextEvent event) {
//do nothing.
}
}
- 建立Servlet进行测试ContextListenerTester.java
package com.example.servlet;
import com.example.entities.Dog;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by LTN on 2016/11/2.
*/
public class ContextListenerTester extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("test context attributes set by listener<br>");
out.println("<br>");
Dog dog = (Dog) getServletContext().getAttribute("dog");
out.println("Dog‘s breed is: " + dog.getBreed());
}
}
- 在DD(web.xml)中配置上下文参数、监听器和servlet映射关系
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>
<servlet>
<servlet-name>ListenerTester</servlet-name>
<servlet-class>com.example.servlet.ContextListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenTest.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.listener.DogServletContextListener</listener-class>
</listener>
</web-app>
- 效果
启动项目,在浏览器输入http://localhost:8080/servlet-listener/ListenTest.do
显示:
test context attributes set by listener
Dog‘s breed is: Great Dane
Session属性监听器
使用Session属性监听器对Session中属性进行监听,当Session属性发生增加、删除或改变时,会被该监听器捕捉。
- 创建一个Session属性监听类DogSessionAttributeListener.java
package com.example.listener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
* Created by LTN on 2016/11/2.
*/
public class DogSessionAttributeListener implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent event) {
String name = event.getName();
Object value = event.getValue();
System.out.println("Attribute added: " + name + ": " + value);
}
public void attributeRemoved(HttpSessionBindingEvent event) {
String name = event.getName();
Object value = event.getValue();
System.out.println("Attribute removed: " + name + ": " + value);
}
public void attributeReplaced(HttpSessionBindingEvent event) {
String name = event.getName();
Object value = event.getValue();
System.out.println("Attribute replaced: " + name + ": " + value);
}
}
- 建立一个Servlet测试类AttributeListenerTester.java
package com.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* Created by LTN on 2016/11/2.
*/
public class AttributeListenerTester extends HttpServlet {
private static int labelCounter = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession session = request.getSession();
session.setAttribute("label", "label" + labelCounter++);
}
}
- 在DD中配置servlet和listener
<servlet>
<servlet-name>AttributeListenTester</servlet-name>
<servlet-class>com.example.servlet.AttributeListenerTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AttributeListenTester</servlet-name>
<url-pattern>/AttributeListenerTest.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.listener.DogSessionAttributeListener</listener-class>
</listener>
- 测试效果
启动tomcat,在浏览器中输入URL:http://localhost:8080/servlet-listener/AttributeListenerTest.do
第一次回车后,后台打印
Attribute added: label: label0
然后连续三次刷新,后台依次打印:
Attribute replaced: label: label0
Attribute replaced: label: label1
Attribute replaced: label: label2
注意,event.getName()和event.getValue()都是获取该事件之前的session属性的值。
统计全局活动的对话
使用HttpSessionListener进行全局活动会话的统计,类似统计在线人数功能。
- 建立全局活动会话监听器
package com.example.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Created by LTN on 2016/11/2.
*/
public class GlobalCounterListener implements HttpSessionListener {
static private int activeSessions;
public static int getActiveSessions() {
return activeSessions;
}
public void sessionCreated(HttpSessionEvent event) {
activeSessions++;
}
public void sessionDestroyed(HttpSessionEvent event) {
activeSessions--;
}
}
- 建立servlet测试类GlobalCounterTester.java
package com.example.servlet;
import com.example.listener.GlobalCounterListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by LTN on 2016/11/2.
*/
public class GlobalCounterTester extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("name","counter");
String name=(String)session.getAttribute("name");
int activeSessions= GlobalCounterListener.getActiveSessions();
out.println("name: "+name);
out.println("activeSessions: "+activeSessions);
}
}
- 在DD中配置servlet和listener
<servlet>
<servlet-name>SessionCounterTester</servlet-name>
<servlet-class>com.example.servlet.GlobalCounterTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionCounterTester</servlet-name>
<url-pattern>/CounterTest.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.example.listener.GlobalCounterListener</listener-class>
</listener>
- 测试效果
在浏览器输入URL:http://localhost:8080/servlet-listener/CounterTest.do
页面显示:
name: counter
activeSessions: 1
换一个浏览器,输入地址,会显示activeSessions数值递增。
注意,同一个浏览器开新标签不能使得session个数增加;关闭当前浏览器再接着启动,也不能。
属性绑定监听器
当属性对象被加入session或从session删除使,对象本身可以知道。
- 建立Session binding监听器Dog.java,该类继承了HttpSessionBindingListener,并实现valueBound和valueUnbound方法。
package com.example.entities;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
* Created by LTN on 2016/11/2.
*/
public class Dog implements HttpSessionBindingListener {
private String breed;
public Dog(String breed) {
this.breed = breed;
}
public String getBreed() {
return breed;
}
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("I am a dog, i know i am in a conversation now.");
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("I am a dog, i know i am leaving a conversation now.");
}
}
- 建立servlet测试类EntityBindingTester.java
package com.example.servlet;
import com.example.entities.Dog;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* Created by LTN on 2016/11/2.
*/
public class EntityBindingTester extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException {
Dog dog = (Dog) getServletContext().getAttribute("dog");
HttpSession session = request.getSession();
session.setAttribute("dog-binding",dog);
session.invalidate();
}
}
- 在DD中配置servlet,注意,属性Dog不用/能在DD中配置
<servlet>
<servlet-name>ObjectBindingTester</servlet-name>
<servlet-class>com.example.servlet.EntityBindingTester</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ObjectBindingTester</servlet-name>
<url-pattern>/ObjectBindingTest.do</url-pattern>
</servlet-mapping>
- 效果测试
启动tomcat,输入URL:http://localhost:8080/servlet-listener/ObjectBindingTest.do
后台打印:
I am a dog, i know i am in a conversation now.
Attribute added: dog-binding: com.example.entities.Dog@6dbba86e
I am a dog, i know i am leaving a conversation now.
Attribute removed: dog-binding: com.example.entities.Dog@6dbba86e
附件列表
《Head First Servlets & JSP》-6-会话管理-listener etc. demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。