最近在学servlet jsp,用的林信良先生的 jsp&servlet 这本书,在第五章有道在线人数统计的课后题完成,做一次记录.
实际效果:
一:用户类:
package cc.openhome.controler; public class User {
private String name;
private String sessionId;
private String ip;
private String firstTime;
private String userAgent;
public User(){ } public String getName() {
return name;
} public String getIp() {
return ip;
} public String getSessionId() {
return sessionId;
} public String getFirstTime() {
return firstTime;
} public String getUserAgent() {
return userAgent;
} public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
} public void setName(String name) {
this.name = name;
} public void setFirstTime(String firstTime) {
this.firstTime = firstTime;
} public void setIp(String ip) {
this.ip = ip;
} public void setSessionId(String sessionId) {
this.sessionId = sessionId;
} }
public class User
二:登录页面:
package cc.openhome.controler; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*; @WebServlet("/login.do")
public class Login extends HttpServlet { private ArrayList<User> ulist;
private Map<String,String> users;
public Login(){
users = new HashMap<String,String>();
users.put("liming","123456");
users.put("小猪","123456");
users.put("小林","123456");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8");
String uname = req.getParameter("user");
String pssd = req.getParameter("pwd"); String page = "login.html"; if (users.containsKey(uname) && users.get(uname).equals(pssd)) {
req.getSession().setAttribute("uname", uname);
String sessionId = req.getSession().getId();
ArrayList<User> ulist = (ArrayList<User>) getServletContext().getAttribute("ulist");
if(ulist == null){
ulist = new ArrayList<User>();
getServletContext().setAttribute("ulist",ulist);
}
User u = new User();
u.setSessionId(sessionId);
u.setName(uname);
u.setIp(req.getRemoteAddr());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
u.setFirstTime(df.format(new Date()));
u.setUserAgent(req.getHeader("user-agent"));
ulist.add(u);
page = "welcome.view";
}
resp.sendRedirect(page);
}
}
三:统计在线人数:
package cc.openhome.controler; import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.List; @WebListener
public class OnlineUserCounter implements HttpSessionListener {
private static int counter; public static int getCounter(){
return counter;
} @Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("新建session: "+se.getSession().getId()); } @Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
ServletContext application = session.getServletContext();
String username = (String) session.getAttribute("username"); List ulist = (List)
application.getAttribute("ulist");
ulist.remove(username);
System.out.println(username+"已经退出!"); }
}
四:登录成功页面显示
package cc.openhome.view; import cc.openhome.controler.User;
import cc.openhome.controler.OnlineUserCounter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; @WebServlet("/welcome.view")
public class Welcome extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession(false);
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01'" +
"Transitional//EN");
out.println("<html>");
out.println("<head>");
out.println("<title>欢迎</title>");
out.println("</head>");
out.println("<body>"); ArrayList<User> ul = (ArrayList <User>) getServletContext().getAttribute("ulist");
out.println("<h1> 目前在线人数 "+ ul.size() +"人</h1>");
Iterator<User> it = ul.iterator();
while (it.hasNext()){
User u = it.next();
out.println("<h4>id: "+ u.getSessionId() +" </h4>");
out.println("<h4>用户: "+ u.getName()+" </h4>");
out.println("<h4>ip: "+ u.getIp() +" </h4>");
out.println("<h4>访问时间: "+ u.getFirstTime() +" </h4>");
out.println("<h4>浏览器信息: "+ u.getUserAgent() +" </h4>");
out.println("<br>");
} out.println("</body>");
out.println("</html>");
out.close(); }
}
五:登录的前端页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="login.do" method="post">
用户名: <input type="text" name="user"><br>
密码: <input type="password" name="pwd"><br>
提交: <input type= "submit">
</form>
</body>
</html>
longin.html
六:实际效果:
七:总结
1.httpsessionListener 工作机制
2,ArrayList 使用
八:不足之处:
1页面展示用的是:在servlet中用的html,还没有学习jsp.
2.已知问题:同一个用户同时登录,重新显示为一新用户,后面再改
参考资料:
http://www.cnblogs.com/shencheng/archive/2011/01/07/1930227.html
https://blog.csdn.net/su20145104009/article/details/53217201
本人菜鸟一枚,如有问题请大神不吝赐教!