1. Overview
1.概述
In this quick tutorial, we’ll see the difference between calling HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean).
在这个快速教程中,我们将看到调用HttpServletRequest#getSession()与HttpServletRequest#getSession(boolean)之间的区别。
2. What’s the Difference?
2.有什么区别?
The methods getSession() and getSession(boolean) are very similar. There’s a small difference, though. The difference is whether the session should be created if it doesn’t exist already.
getSession() 和getSession(boolean) 的方法非常相似。不过有一个小小的区别。区别在于,如果会话不存在,是否应该被创建。
Calling getSession() and getSession(true) are functionally the same: retrieve the current session, and if one doesn’t exist yet, create it.
调用getSession()和getSession(true)在功能上是一样的:检索当前会话,如果还没有会话,则创建它。
Calling getSession(false), though, retrieves the current session, and if one doesn’t exist yet, returns null. Among other things, this is handy when we want to ask if the session exists.
调用getSession(false),会检索当前的会话,如果一个会话还不存在,则返回null。除此之外,当我们想询问会话是否存在时,这很方便。
3. Example
3.例子
In this example, we are considering this scenario:
在这个例子中,我们考虑的是这种情况。
- the user enters the user id and logs in to the application
- the user then enters the user name and age and wants to update these details for the logged-in user
We’ll store the user values in the session to understand the usage of HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean).
我们将在会话中存储用户值,以了解HttpServletRequest#getSession() 和HttpServletRequest#getSession(boolean)的用法。
First, let’s create a servlet where we’re using HttpServletRequest#getSession() in its doGet() method:
首先,让我们创建一个servlet,在其doGet()方法中使用HttpServletRequest#getSession() 。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("userId", request.getParameter("userId"));
}
At this point, the servlet will retrieve the existing session or create a new one for the logged-in user, if it doesn’t exist.
在这一点上,servlet将检索现有的会话,如果不存在,则为登录的用户创建一个新的会话。
Next, we’ll set the userName attribute in the session.
接下来,我们将在会话中设置userName属性。
As we want to update the details of the user for the respective user id, we want the same session and do not want to create a new session to store the user name.
由于我们想更新各自用户ID的详细信息,我们希望使用同一个会话,而不想创建一个新的会话来存储用户名。
So now, we will use HttpServletRequest#getSession(boolean) with false value:
所以现在,我们将使用HttpServletRequest#getSession(boolean),使用false值:。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session != null) {
session.setAttribute("userName", request.getParameter("userName"));
}
}
This will result in setting the userName attribute on the same session that the userId was previously set.
这将导致在之前设置userId的同一会话上设置userName属性。
4. Conclusion
4.总结
In this tutorial, we’ve explained the difference between HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean) methods.
在本教程中,我们已经解释了HttpServletRequest#getSession()和HttpServletRequest#getSession(boolean)方法之间的区别。
The complete example is available over on GitHub.
完整的例子可在GitHub上找到。