同步操作将从 yswift/LearnJava 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package javaweb;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.Date;@WebServlet(name = "SessionTrack", urlPatterns = "/sessionTrack")public class SessionTrack extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// 如果不存在 session 会话,则创建一个 session 对象HttpSession session = request.getSession(true);// 获取 session 创建时间Date createTime = new Date(session.getCreationTime());// 获取该网页的最后一次访问时间Date lastAccessTime = new Date(session.getLastAccessedTime());String title = "欢迎回到我的网站";Integer visitCount = 0;String visitCountKey = "visitCount";String userIDKey = "userID";String userID = "ABCD";session.setAttribute(visitCountKey, visitCount);// 检查网页上是否有新的访问者if (session.isNew()) {title = "欢迎来到我的网站";session.setAttribute(userIDKey, userID);} else {visitCount = (Integer) session.getAttribute(visitCountKey);visitCount = visitCount + 1;userID = (String) session.getAttribute(userIDKey);}session.setAttribute(visitCountKey, visitCount);// 设置响应内容类型response.setContentType("text/html");PrintWriter out = response.getWriter();String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<h2 align=\"center\">Session 信息</h2>\n" +"<table border=\"1\" align=\"center\">\n" +"<tr bgcolor=\"#949494\">\n" +" <th>Session 信息</th><th>值</th></tr>\n" +"<tr>\n" +" <td>id</td>\n" +" <td>" + session.getId() + "</td></tr>\n" +"<tr>\n" +" <td>Creation Time</td>\n" +" <td>" + createTime +" </td></tr>\n" +"<tr>\n" +" <td>Time of Last Access</td>\n" +" <td>" + lastAccessTime +" </td></tr>\n" +"<tr>\n" +" <td>User ID</td>\n" +" <td>" + userID +" </td></tr>\n" +"<tr>\n" +" <td>Number of visits</td>\n" +" <td>" + visitCount + "</td></tr>\n" +"</table>\n" +"</body></html>");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。