jsp实现在线用户列表显示

在登陆后显示还有哪些用户在线,用一个列表显示出来,算是完善了之前写的代码的一个小功能吧,以后还会继续。

一个是在LoginCheck的servlet里面,把登陆成功的用户用List的形式存进application里

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.apple.eawt.Application;

import pub.GetDataBaseConnection;
import userPOJO.UserPOJO;

public class LoginCheck extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//取得out对象
        String username = request.getParameter("username");
        String password = request.getParameter("password");
          boolean flag = false;
          Connection conn = null;
          PreparedStatement pstate = null;
          ResultSet res = null;
          try{
            conn = GetDataBaseConnection.getConnection();
            String sql = "select user_id,headicon from usert where username = ? and password = ?";
            pstate = conn.prepareStatement(sql);
            pstate.setString(1,username);
            pstate.setString(2,password);
            res = pstate.executeQuery();
            while(res.next()){  
                    Cookie c[] = request.getCookies();
                    if(null == c || c.length < 2){//如果cookie不存在或者小于2就添加用户名和密码的cookie用于一分钟内免登陆
                    Cookie cook_name = new Cookie("username",username);
                    cook_name.setMaxAge(60);
                    Cookie cook_pass = new Cookie("password",password);
                    cook_pass.setMaxAge(60);
                    response.addCookie(cook_name);
                    response.addCookie(cook_pass);

                    int userId = res.getInt(1);
                    String headicon = res.getString(2);
                    UserPOJO userpojo = new UserPOJO(userId,username,password,headicon);
                    HttpSession session = request.getSession(true);
                    session.setAttribute("userpojo", userpojo);//设置用户属性范围
                    out.print("<SCRIPT language=javascript>alert('登录成功');window.location='jsp/Main.jsp';</script>"); 
                    flag = true; 

                    String userName = request.getParameter("username"); 
                    ServletContext application = request.getServletContext(); 
                    ArrayList loginList = (ArrayList)application.getAttribute("loginlist"); 
                    if(loginList == null){  
                      loginList = new ArrayList(); 
                      application.setAttribute("loginlist",loginList); 
                    }  
                    loginList.add(userName); 
                    request.getRequestDispatcher("/jsp/Main.jsp").forward(request,response); 
                    }
            }
            if(!flag){
            out.print("<SCRIPT language=javascript>alert('用户名或密码不正确,请重新输入!'); window.location='jsp/Login.jsp';</script>");
            }
            }catch(Exception e){
              e.printStackTrace();
          }finally{
              try{
                  res.close();
                  pstate.close();
                  conn.close();
              }catch(Exception e){
                  e.printStackTrace();
              }
          }
    }

}

然后就是在main.jsp里面把list给迭代出来了

<%@page contentType="text/html; charset=utf-8" %>
<%@page import="java.util.*" %>
<%Cookie c[] = request.getCookies(); if (null != c) { } %>
<html>
<head><title><h1>主界面</h1></title></head>
<body>
    <img src="<%=request.getContextPath() %>/headicon/${userpojo.headicon}" name="headicon" alt="头像" />
    <h1>${userpojo.userName},你好</h1>
    <h3><a href="<%=request.getContextPath() %>/jsp/Logout.jsp" onClick="{if(confirm('确定要注销吗?')){return true;}return false;}">注销</a></h3>
    <a href="<%=request.getContextPath() %>/jsp/UpdateInfo.jsp">修改资料</a>
    <h1>在线人员列表</h1>
<hr/>
<% ArrayList<String> list = (ArrayList<String>)application.getAttribute("loginlist"); Iterator<String> it = list.iterator(); while(it.hasNext()){ %>
    <li><%=it.next()%></li>

<% } %>
</body>
</html>

在完善过程中,发现要把项目部署进tomcat里面的server.xml里面,不然上传的图片不能保存进项目里,而是保存在了tomcat里面的webapps里。

你可能感兴趣的:(jsp,servlet,应用)