jsp实现在线用户列表显示

该博客介绍了如何在用户登录后通过Servlet和JSP展示在线用户列表。登录成功后,用户信息被存储为List并放入应用程序上下文中。在main.jsp中遍历列表以显示在线用户。此外,还提到为了正确保存上传的图片,需要将项目配置到Tomcat的server.xml中。

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

一个是在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里。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值