再线人名统计 错误
来源:百度知道 编辑:UC知道 时间:2024/05/31 19:57:17
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{ ServletContext application = null;
HttpSession s=request.getSession();
application =s.getServletContext();
//取得SESSION NAME
String name = s.getAttribute("name").toString()+"\r\n";
//全部用户名称
String allname=null;
//判断是否有人登陆,如果有人登陆
if(application.getAttribute("name")!=null)
{
//取得登陆人的名字
allname = application.getAttribute("name").toString();
//判断所有登陆人里是否包含本人,如果包含
if(allname.indexOf(name)>0)
{
}
//如果不包含,将本人的名字放到最后
else
{
allname.concat(name);
application.setAttribute("name", allname);
}
}
//如果没人登陆创建一个直接复制
else
{
application.setAttribute("name", name);
}
allname.concat(name); 这个方法你用错了,他是把name连接在字符串后面,然后返回一个新的字符串.
所以应该这样:
allname = allname.concat(name);
OK! 咯
做的这么复杂 曾经做过
直接在servlet里获得输入jsp页面的内容 然后在把获得值传到你想传到的jsp页面就行了
楼主这个想法貌似有点····
要实现在线人数的统计·需要用到监听器!
新建一个 listener监听session类:
public class SessionListener implements HttpSessionAttributeListener {
private static Vector onlineList = new Vector();
String name = "login_name"; //sessionl里面的属性
public void attributeAdded(HttpSessionBindingEvent event) {
if (name.equals(event.getName())) {
onlineList.add(event.getValue());
}
}
public void attributeRemoved(HttpSessionBindingEvent event) {
if (name.equals(event.getName())) {
onlineList.remove(event.getValue());
}
}
public void attributeReplaced(HttpSessionBindingEvent event) {
this.attributeAdded(event