Servlet 操作Session示例分享

Java-教程王 Servlet 发布时间:2021-08-13 22:57:48 阅读数:4632 1
下文是笔者借助HttpSession对象创建一个记录当前客户端访问服务器的次数的示例,
代码中运用了HttpSession的一些方法对Session进行操作,如下所示:
例:
本示例是基于Servlet HelloWorld扩展开发

/** ServletSessionTest.java **/
 


package servlet01;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

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;

@WebServlet("/SessionTest")
public class ServletSessionTest extends HttpServlet {

	private static final long serialVersionUID = 99L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 当不存在 session 会话,则创建一个 session 对象
		HttpSession session = req.getSession(true);
		// 获取session 创建时间
		Date createTime = new Date(session.getCreationTime());
		// 获取该网页的最后一次访问时间
		Date lastAccessTime = new Date(session.getLastAccessedTime());

		// 设置日期输出的格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String title = "Java265.com-Servlet Session Sample test";
		Integer visitCount = 0;
		String visitCountKey = new String("参观人数");
		String userIdKey = new String("userId");
		String userId = new String("Java265.com");
		if (session.getAttribute(visitCountKey) == null) {
			session.setAttribute(visitCountKey, 0);
		}

		// 检查网页上是否有新的访问者
		if (session.isNew()) {
			title = "Java265.com-Servlet Session Sample test";
			session.setAttribute(userIdKey, userId);
		} else {
			visitCount = (Integer) session.getAttribute(visitCountKey);
			visitCount = visitCount + 1;
			userId = (String) session.getAttribute(userIdKey);
		}
		session.setAttribute(visitCountKey, visitCount);

		// 避免中文乱码
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();

		String docType = "<!DOCTYPE html>\n";
		out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"
				+ "<body bgcolor=\"#f0f0f0\">\n" + "<h5 align=\"center\">" + title + "</h5>\n"
				+ "<h6 align=\"center\">Session 信息</h6>\n" + "<table border=\"1\" align=\"center\">\n"
				+ "<tr bgcolor=\"#949494\">\n" + "  <th>Session 信息</th><th>值</th></tr>\n" + "<tr>\n"
				+ "  <td>SessionId</td>\n" + "  <td>" + session.getId() + "</td></tr>\n" + "<tr>\n"
				+ "  <td>第一次访问时间</td>\n" + "  <td>" + df.format(createTime) + "  </td></tr>\n" + "<tr>\n"
				+ "  <td>最后访问时间</td>\n" + "  <td>" + df.format(lastAccessTime) + "  </td></tr>\n" + "<tr>\n"
				+ "  <td>用户信息</td>\n" + "  <td>" + userId + "  </td></tr>\n" + "<tr>\n" + "  <td>访问总次数:</td>\n"
				+ "  <td>" + visitCount + "</td></tr>\n" + "</table>\n" + "</body></html>");

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		doGet(req, resp);
	}

}
Servlet操作HttpSession对象的示例分享
Servlet操作HttpSession的示例分享
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/Servlet/202108/198.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者