【问题标题】:Session created by TomcatTomcat 创建的会话
【发布时间】:2022-02-13 02:58:56
【问题描述】:

我正在学习使用 servlet 的会话,我在书中读到要创建一个我们需要调用的会话,如下所示。

HttpSession session = request.getSession()

这会导致 Web 容器创建一个会话 ID 并将其发送回客户端,以便客户端可以将其附加到服务器的每个后续请求中。当我在网络选项卡中的请求标头下打开 chrome 中的开发人员工具时,我确实看到了一个 cookie 标头。

Cookie: JSESSIONID=F92

以下是我在登录 servlet 中所做的操作

package shop;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class LoginServlet extends HttpServlet{
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String uid = request.getParameter("uid");
        String password = request.getParameter("pwd");
        
        if(uid.equals("admin") && password.equals("admin"))
        {
            HttpSession session = request.getSession();
            System.out.println(session.getId());
            response.sendRedirect("shopping");
        }
        else
            response.getWriter().println("Invalid Credentials");
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.doGet(request, response);
    }

}

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>shopping application</title>
</head>
<body style="background-color:cyan">
<div style="text-align:center">
<h3>Welcome to Mart</h3>
<form action="login" method="post" name="loginform">
    <div style="padding:2px;">
        <label for="uid">Username:</label>
        <input type="text" id="uid" name="uid">
    </div>
    <div style="padding:2px;">
        <label for="pwd">Password:</label>
        <input type="password" name="pwd" id="pwd">
    </div>

    <input type="submit" value="Login">
</form>
</div>
</body>
</html>

我的问题是,即使我删除了getSession() 调用,我仍然会在网络选项卡中看到 cookie。是否有与 tomcat 的每个请求关联的默认会话?

【问题讨论】:

  • 您是否启用了身份验证?
  • 我是新手,我该如何检查?我正在使用 eclipse 中的 tomcat 来运行我的项目
  • 启用身份验证后,您需要在访问页面时提供用户名和密码。
  • 我确实将用户名和密码作为 index.jsp 的输入

标签: java tomcat servlets jakarta-ee


【解决方案1】:

在需要时,Tomcat 会话会延迟建立。基本上有几种情况会创建会话:

  • 如果您调用request.getSession() 或request.getSession(true),则始终会建立会话,
  • 如果您根据 Tomcat 的用户数据库对用户进行身份验证,则可能会根据身份验证方法创建会话。最值得注意的是,如果您使用表单身份验证(请参阅 this tutorial),则始终会建立会话,
  • JSP 页面会创建会话,除非您添加 &lt;%page session="false"%&gt; 指令(请参阅 Why set a JSP page session = "false" directive?)。

浏览器会记住 cookie,因此JSESSIONID 的存在并不表示会话存在(它可能来自之前的测试)。要测试是否存在会话,请使用request.getSession(false)。例如:

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      final boolean establishSession = req.getParameter("withSession") != null;
      try (final PrintWriter writer = resp.getWriter()) {
         final String requestedSessionId = req.getRequestedSessionId();
         final HttpSession possibleSession = req.getSession(establishSession);
         writer.append("I requested a session with id: ")//
               .append(requestedSessionId)
               .append("\nI have a session with id: ")
               .append(possibleSession != null ? possibleSession.getId() : null)
               .println();
      }
   }

编辑:我添加了一个 JSP 页面创建会话的案例。

【讨论】:

  • 我完全没有在 web.xml 中进行任何更改来设置身份验证,仅仅是因为我使用的是表单吗?
  • 抱歉,我没有注意到您的 JSP 页面代码。所以是的,您的带有表单的 JSP 页面创建了会话,您需要将 session="false" 添加到 page 指令(请参阅修改后的答案)。
  • 感谢您的帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2016-06-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多