标签:dem lob cti 结构 bind bsp his date aced
示例程序展示了如何从上下文读取参数,并在上下文监听器中生成属性对象和在上下文中设置属性。
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;
}
}
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.
}
}
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());
}
}
<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>
test context attributes set by listener
Dog‘s breed is: Great Dane
使用Session属性监听器对Session中属性进行监听,当Session属性发生增加、删除或改变时,会被该监听器捕捉。
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);
}
}
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++);
}
}
<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>
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--;
}
}
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);
}
}
<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>
name: counter
activeSessions: 1
换一个浏览器,输入地址,会显示activeSessions数值递增。
注意,同一个浏览器开新标签不能使得session个数增加;关闭当前浏览器再接着启动,也不能。
当属性对象被加入session或从session删除使,对象本身可以知道。
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.");
}
}
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();
}
}
<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>
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
标签:dem lob cti 结构 bind bsp his date aced
原文地址:http://www.cnblogs.com/myitroad/p/6192526.html