高分!!!在JavaBean里怎样可以取得用户Session的值

love_lanchong 2008-12-22 12:05:26
我用一个JavaBean来监听是否有用户打开浏览器,如果有用户打开浏览器的话:
根据Session或者Cookie如果不为空,就是正式用户,就把当前时间写入数据库,到用户关闭浏览器,再取到时间,相减得到一个数,再写入数据库来做一个用户在线时间的统计。

我现在的问题是在JavaBean文件里取不到用户Session

请大家给出提示代码,解决必奖高分。
...全文
532 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
cry_liu 2011-09-05
  • 打赏
  • 举报
回复
对了,麻烦再问一下,除了那个javabean之外还需要在web.xml或者什么地方配置东东吗?谢谢
cry_liu 2011-09-05
  • 打赏
  • 举报
回复
不懂,你解决的办法可以说清楚点不咯?谢谢
love_lanchong 2008-12-25
  • 打赏
  • 举报
回复
谢谢大家的帮忙,小弟的问题已经解决
我在sessionDestroyed这个方法里,也就是Session销毁的时候,用
HttpSession session = se.getSession();
String userName = (String)session.getAttribute("username");
这样就可以取到Session了,然后再进行操作就可以了
hero_shaoshuai 2008-12-24
  • 打赏
  • 举报
回复
关注
耶律火柴 2008-12-24
  • 打赏
  • 举报
回复
在过滤器里调用 javabean的方法传session进去
ujnharry 2008-12-24
  • 打赏
  • 举报
回复
为什么要这样得数据呢
haobaofufob 2008-12-24
  • 打赏
  • 举报
回复
呵呵,不管怎么实现,就是要想办法将HttpServletRequest对象保存住,这样就能取得session了。
lihan6415151528 2008-12-23
  • 打赏
  • 举报
回复

import org.directwebremoting.WebContext;

import org.directwebremoting.WebContextFactory;

public class JavaBeanGetSessionValueSimple{

public void getSessionValue(){

WebContext ctx = WebContextFactory.get();

session = ctx.getHttpServletRequest().getSession();

String name = session.getAttribute("name");

System.out.println("name:" + name);

}


DWR确实是不错选择
wndsc 2008-12-23
  • 打赏
  • 举报
回复
o(∩_∩)o... 不会
tiger868686 2008-12-23
  • 打赏
  • 举报
回复
用dwr可轻松解决:

import org.directwebremoting.WebContext;

import org.directwebremoting.WebContextFactory;

public class JavaBeanGetSessionValueSimple{

public void getSessionValue(){

WebContext ctx = WebContextFactory.get();

session = ctx.getHttpServletRequest().getSession();

String name = session.getAttribute("name");

System.out.println("name:" + name);

}

}
love_lanchong 2008-12-23
  • 打赏
  • 举报
回复
自己顶啊,救命啊
pengxiancan 2008-12-23
  • 打赏
  • 举报
回复
会不会和你的浏览器的第三方Cookies是关闭的!
其他的不知道!顶!
love_lanchong 2008-12-23
  • 打赏
  • 举报
回复
我在Servlet文件里已经定义好了的Cookie,在Jsp页面里也可以读取,但是在.java文件里怎么读取不了?是什么问题呢?

public void sessionCreated(HttpSessionEvent se) {
activeSessions++;
System.out.println("aaaaaaa"); //只能打印出这一行
Cookie cs[]=request.getCookies(); //程序到这一行就不进行下去了
System.out.println("bbbbbb");
if(cs!=null && cs.length>0){
System.out.println("ccccccc");
for(int n=0;n<cs.length;n++){
System.out.println("ddddddd");
if("cookiename".equals(cs[n].getName())) {
String username = java.net.URLDecoder.decode(cs[n].getValue());
}
}
}
guobingyou 2008-12-23
  • 打赏
  • 举报
回复
关注 中。。。
laitaogood 2008-12-23
  • 打赏
  • 举报
回复
获取上下文,然后从上下文里获得session
kukufly 2008-12-23
  • 打赏
  • 举报
回复
action或者servlet就是属于一个javabean,楼主可以重写一下HttpServert方法,调用里面request和response即可
blueram 2008-12-23
  • 打赏
  • 举报
回复
package com.bjsxt.drp.util.listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class UserCountListener implements HttpSessionListener,HttpSessionAttributeListener {

private static int count = 0;

public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("UserCountListener.sessionCreated()");
}

public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("UserCountListener.sessionDestroyed()");
}

public void attributeAdded(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub


System.out.println("name=" + event.getName());
System.out.println("name=" + event.getValue());
if (event.getName().equals("user")) {
count ++;
}
System.out.println("online count: " + count);
}

public void attributeRemoved(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub

}

public void attributeReplaced(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub

}

}[code=Java]
[/code]

在attributeAdded里面处理登陆时的记录,在attributeRemoved处理session为空的情况
fys249931556 2008-12-22
  • 打赏
  • 举报
回复
实现Listener相关接口。
你javaBean中如果要通过session实现其他功能,可以通过Listener的对象来取得。
在javaBean 的构造方法中接收Session。
sunnylyy 2008-12-22
  • 打赏
  • 举报
回复
利用threadlocal, 把session的内容存储在threadlocal变量中去。
lanzhengwu 2008-12-22
  • 打赏
  • 举报
回复
顶LS 的..这东西用监听器来做就好了 ..你JAVABEAN中有HttpSession对象么??怎么得到SESSION..
况且要得到浏览器的状态要监听的..
加载更多回复(8)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧