我目前正在第一次配置 Tomcat。
目前,我认为会话复制(JSESSIONID)正在起作用,但我无法知道。
请注意,我从未学习过用 Java 编写代码,而且我也不打算学习。
这是我想要的:
A简单的 .war
文件会打印当前的 JSESSIONID 号码,并在网页每次重新加载时将其递增。
有人知道我可以在哪里找到这个吗?
答案1
- 运行 Eclipse
- CTRL+N(动态 Web 项目)并命名测试
- 右键点击项目>新建>Servlet,输入 页面点击计数器在里面班级名称:框 > 下一步 > 下一步,取消选择 doPost并完成
- 复制此代码并替换
// TODO Auto-generated method stub
位于做获取方法 - 执行
CTRL+SHIFT+O
导入缺失的库,并将 res 和 req 分别重命名为 response 和 request - 右键单击项目并运行它
1 Run on Server
- 如果预期页面未加载,请导航至
http://localhost:8080/test/SessionTracker
- 重新加载网页会增加数量
- 如果正常运行,请右键单击项目并
Export...
>>Web
WAR file
- 将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 ∧ 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.
}
}