public void loginRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); HttpSession session = request.getSession(); String username=request.getParameter("username"); //获得登录用户名 UserInfo user=UserInfo.getInstance(); //获得UserInfo类的对象 session.setMaxInactiveInterval(600); //设置Session的过期时间为10分钟 Vector vector=user.getList(); boolean flag=true; //标记是否登录的变量 //判断用户是否登录 System.out.println("vector的size:"+vector.size()); if(vector!=null&&vector.size()>0){ for(int i=0;i<vector.size();i++){ System.out.println("vector"+i+":"+vector.elementAt(i)+" user:"******"<script language='javascript'>alert('该用户已经登录');window.location.href='index.jsp';</script>"); } catch (IOException e) { e.printStackTrace(); } flag=false; break; } } } //保存用户信息 if(flag){ UserListener ul=new UserListener(); //创建UserListener的对象 ul.setUser(username); //添加用户 user.addUser(ul.getUser()); //添加用户到UserInfo类的对象中 session.setAttribute("user",ul); //将UserListener对象绑定到Session中 session.setAttribute("username",username); //保存当前登录的用户名 session.setAttribute("loginTime",new Date().toLocaleString()); //保存登录时间 ServletContext application=getServletContext(); String sourceMessage=""; if(null!=application.getAttribute("message")){ sourceMessage=application.getAttribute("message").toString(); } sourceMessage+="系统公告:<font color='gray'>" + username + "走进了聊天室!</font><br>"; application.setAttribute("message",sourceMessage); try { request.getRequestDispatcher("login_ok.jsp").forward(request, response); } catch (Exception ex) { Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex); } } }
/** Set the session timeout */ protected void setSessionTimeout(HttpSession session, long time) { session.setMaxInactiveInterval((int) (time / Constants.SECOND)); }