Tomcat WAR 文件在重新加载时增加 JSESSIONID 以测试会话复制

Tomcat WAR 文件在重新加载时增加 JSESSIONID 以测试会话复制

我目前正在第一次配置 Tomcat。

目前,我认为会话复制(JSESSIONID)正在起作用,但我无法知道。

请注意,我从未学习过用 Java 编写代码,而且我也不打算学习。

这是我想要的:

A简单的 .war文件会打印当前的 JSESSIONID 号码,并在网页每次重新加载时将其递增。

有人知道我可以在哪里找到这个吗?

答案1

  1. 运行 Eclipse
  2. CTRL+N(动态 Web 项目)并命名测试
  3. 右键点击项目>新建>Servlet,输入 页面点击计数器在里面班级名称:框 > 下一步 > 下一步,取消选择 doPost并完成
  4. 复制此代码并替换// TODO Auto-generated method stub位于做获取方法
  5. 执行CTRL+SHIFT+O导入缺失的库,并将 res 和 req 分别重命名为 response 和 request
  6. 右键单击项目并运行它1 Run on Server
  7. 如果预期页面未加载,请导航至 http://localhost:8080/test/SessionTracker
  8. 重新加载网页会增加数量
  9. 如果正常运行,请右键单击项目并Export...>>WebWAR file
  10. 将war文件复制到webapps文件夹,然后重启tomcat

在此处输入图片描述

Java 代码如果关联不再存在:

import java.io.IOException;
import java.io.PrintWriter;

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("/SessionTracker")
public class SessionTracker extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public SessionTracker() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);

        Integer count = (Integer) session.getValue("tracker.count");
        if (count == null)
            count = new Integer(1);
        else
            count = new Integer(count.intValue() + 1);
        session.putValue("tracker.count", count);

        out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
        out.println("<BODY><H1>Session Tracking Demo</H1>");

        out.println("You've visited this page " + count
                + ((count.intValue() == 1) ? " time." : " times."));

        out.println("<P>");

        out.println("<H2>Here is your session data:</H2>");
        String[] names = session.getValueNames();
        for (int i = 0; i < names.length; i++) {
            out.println(names[i] + ": " + session.getValue(names[i]) + "<BR>");
        }
        out.println("</BODY></HTML>");
    }
}

答案2

我修改了@utrecht 提供的代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PageHitCounter extends HttpServlet{
/**
     * 
     */
    private static final long serialVersionUID = -9119068346424522270L;
private String sessionId; 

  public void init() 
  {
     sessionId = "Not Set";
  } 

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");
      // This method executes whenever the servlet is hit 
      PrintWriter out = response.getWriter();

      String title = "JSESSIONID &and; count";
      HttpSession session = request.getSession();
      // Boolean isNew = session.isNew();
      sessionId = session.getId();

      Integer counter = (Integer)session.getAttribute("counter");
      if ( counter == null ){
          counter = new Integer(1);
      } else {
          counter = new Integer(counter.intValue() + 1);
      }
      session.setAttribute("counter", counter);
      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\">" + sessionId + "</h2>\n" +
        "<h2 align=\"center\">" + counter + "</h2>\n" +
        "</body></html>");

  }
  public void destroy() 
  { 
      // This is optional step but if you like you
      // can write hitCount value in your database.
  } 
} 

相关内容