jsp實現線上使用者列表顯示
阿新 • • 發佈:2019-02-01
在登陸後顯示還有哪些使用者線上,用一個列表顯示出來,算是完善了之前寫的程式碼的一個小功能吧,以後還會繼續。
一個是在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裡。