在容器里的类都是单态的,如Servlet容器,只被实例化一次。
HttpSession-ID->作用于客户端、服务器请求之间?
ScriptSession-ID->用户访问了哪些页面?
以下是具体代码演示:
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="lgManager!login">
<s:textfield name="usName"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="TestManager" extends="struts-default">
<action name="lgManager" class="com.zuxia.yc42.action.LoginAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
com.zuxia.yc42.action
public class LoginAction extends ActionSupport:
package com.zuxia.yc42.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String usName;
public String login()
{
Map<String,Object> session = ActionContext.getContext().getSession();
session.put("user", usName);
return SUCCESS;
}
public String getUsName() {
return usName;
}
public void setUsName(String usName) {
this.usName = usName;
}
}
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=path%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=path%>/dwr/util.js"></script>
<!-- dwrDemo.js来自dwr.xml配置文件中指向的java类 -->
<script type="text/javascript" src="<%=path%>/dwr/interface/dwrDemo.js"></script>
<script type="text/javascript">
window.οnlοad=function()
{
dwr.engine.setActiveReverseAjax(true);
dwrDemo.showUsers();
};
function showUsers(data)
{
dwr.util.removeAllOptions("usNames");
dwr.util.addOptions("usNames",data);
}
</script>
</head>
<body>
<s:select list="#{'':'--清选择--' }" id="usNames" name="usNames"></s:select>
</body>
</html>
WebRoot/WEB-INF/dwr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="dwrDemo">
<param name="class" value="com.zuxia.yc42.dwr.DwrDemo" />
</create>
<convert converter="bean" match="java.lang.StackTraceElement" />
</allow>
</dwr>
WebRoot/WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- Session监听器 -->
<listener>
<listener-class>com.zuxia.yc42.listener.JsSessionListener</listener-class>
</listener>
<!-- DWR配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 调试模式 -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- 设置是否允许使用dwr推送技术 -->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>-1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
com.zuxia.yc42.dwr
public class DwrDemo :
package com.zuxia.yc42.dwr;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.directwebremoting.Browser;
import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;
public class DwrDemo
{
public void showUsers()
{
Browser.withCurrentPage(new Runnable() {
@Override
public void run() {
//得到上下文对象
ServletContext context = ServerContextFactory.get().getServletContext();
Map<String,ScriptSession> map=(Map<String,ScriptSession>) context.getAttribute("ScriptSessionMap");
Collection<ScriptSession> ss = map.values();
Map<Integer,String> users = new HashMap<Integer,String>();
int key=0;
for(ScriptSession session :ss)
{
users.put(key++, session.getAttribute("user").toString());
}
for(ScriptSession session :ss)
{
ScriptBuffer buffer = new ScriptBuffer();
buffer.appendScript("showUsers(");
buffer.appendData(users);
buffer.appendScript(");");
session.addScript(buffer);
}
}
});
}
}
com.zuxia.yc42.listener
public class JsSessionListener implements HttpSessionListener:
package com.zuxia.yc42.listener;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.directwebremoting.Container;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;
public class JsSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event)
{
//得到容器Container container
Container container = ServerContextFactory.get().getContainer();
//如果容器里有ScriptSessionManager对象,则赋值创建ScriptSessionManager 实例化对象
ScriptSessionManager ssManager = (ScriptSessionManager)container.getBean(ScriptSessionManager.class);
//添加一个监听
ssManager.addScriptSessionListener(new ScriptSessionListener() {
@Override
public void sessionDestroyed(ScriptSessionEvent event)
{
System.out.println("有ScriptSession被销毁了");
}
@Override
public void sessionCreated(ScriptSessionEvent event) {
//得到上下文对象ServletContext context
ServletContext context = ServerContextFactory.get().getServletContext();
Object obj = context.getAttribute("ScriptSessionMap");
if(obj == null)
{
//如果是第一次登录,ScriptSessionMap是没有值得,那么就创建一个
context.setAttribute("ScriptSessionMap", new HashMap<String,ScriptSession>());
}
Map<String,ScriptSession> map = (Map<String,ScriptSession>)context.getAttribute("ScriptSessionMap");
//获取KEY
HttpSession session = WebContextFactory.get().getSession();
//得到HttpSession对象中的key值user
Object user = session.getAttribute("user");
//然后把HttpSession中的user添加ScriptSession中
event.getSession().setAttribute("user", user);
//把HttpSession中session的ID和ScriptSession以key-value形式添加到map集合中
map.put(session.getId(), event.getSession());
System.out.println("有ScriptSession被创建了");
}
});
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
}