日期:2014-05-17  浏览次数:21294 次

请指教~~如何用jsp取得tomcat中存的sessionID
望各位前辈能给新手我一个详细解说的代码!谢谢!

------解决方案--------------------
HttpSession session=request.getSession();//得到一个session对象或创建一个

session.getId()//得到sessionID数
------解决方案--------------------
用HttpSessionListener来实现

http://hi.baidu.com/lael80/blog/item/74fa6ec6576d641b9d163dfd.html
看看源代码


/////////////////
//bbs.rar\bbs\src\javabean\Listener.java

package javabean;

import javax.servlet.http.*;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

public class Listener extends HttpServlet implements HttpSessionListener, HttpSessionAttributeListener {
private static Map map=new HashMap();
private String user_name;

public void sessionCreated(HttpSessionEvent event){
}

public void sessionDestroyed(HttpSessionEvent event){
}

public void attributeAdded(HttpSessionBindingEvent event){
HttpSession session=event.getSession();
user_name=(String)session.getAttribute( "u_name ");
map.put(session,user_name);
}

public void attributeRemoved(HttpSessionBindingEvent event){
}

public void attributeReplaced(HttpSessionBindingEvent event){
HttpSession session=event.getSession();
user_name=(String)session.getAttribute( "u_name ");
if(user_name.trim().equals( ", ")){map.remove(session);}else map.put(session,user_name);
}

public static int getOnLineGuest(){
int OnLineGuest=0;
for(Iterator it=map.keySet().iterator();it.hasNext();){//entrySet()
Object key=it.next();
if(map.get(key).equals( " ")){
OnLineGuest++;
}
}
return OnLineGuest;
}

public static Map getOnLineUser(){
Map map2=new HashMap();
for(Iterator it=map.keySet().iterator();it.hasNext();){//entrySet()
Object key=it.next();
if(!map.get(key).equals( " ")&&!map.get(key).equals( ", ")){
map2.put(map.get(key),map.get(key));
}
}
return map2;
}
}


//////////////////////////////
//bbs.rar\bbs\bbs\index.jsp

<%@ page import= "javabean.Listener "%>
<%
Map map=new HashMap();
map=Listener.getOnLineUser();

out.println( " <a href= 'javascript:ShowUserList(); '> <img id= 'btn ' src= 'images/add.gif '> </a> "+
"目前论坛总共有 "+(Listener.getOnLineGuest()+map.size())+ " 人在线,其中注册用户 "+map.size()+ " 人,游客 "+
Listener.getOnLineGuest()+ " 人 ");

out.println( " <div id= 'list ' style= 'display:none; '> <br> ");
int i=0;
for(Iterator it=map.keySet().iterator();it.hasNext();){//entrySet()
Object key=it.next();
i++;
out.println( "&nbsp;&nbsp;&nbsp; <a href= 'userinfo.jsp?name= "+workFC.UnitoGB((String)map.get(key))+ " '> "+
workFC.UnitoGB((String)map.get(key))+ " </a> ");
if(i%10==0)out.println( " <br> <br> ");
}
out.println( " <br> <br> </div> ");
%>