简体   繁体   English

关于java web session handeling如何工作的困惑。使用servlet api和HttpSession对象揭开Cookie和标头差异的神秘面纱

[英]Confusion about how java web session handeling works. Demystifying Cookies and Header differences using servlet api and HttpSession object

I am learning Spring security and Spring MVC, but I realized I needed to learn jsp Servlets first and general web programming in a java environment. 我正在学习Spring安全性和Spring MVC,但我意识到我需要首先学习jsp Servlets并在java环境中学习一般的Web编程。

I have confusions surrounding the HttpServletRequest and HttpServletResponse objects and how they can be used to add headers to the request and response objects and how they relate to sessions. 我有关于HttpServletRequest和HttpServletResponse对象的混淆,以及它们如何用于向请求和响应对象添加标头以及它们与会话的关系。

As far as I understand, a cookie is a type of header just like Content-type and Accept. 据我所知,cookie是一种类型的标题,就像Content-type和Accept一样。 The java servlet api just makes it easy to work with the header by using methods specific to the context in which the header is being used. java servlet api通过使用特定于使用标头的上下文的方法,可以轻松地使用标头。 For example: 例如:

response.setContentType(String mimeType)
response.setContentLength(int lengthInBytes)

My confusion starts here.. Cookie is not a String or int, its a object: 我的困惑从这里开始.. Cookie不是String或int,它是一个对象:

response.addCookie(Cookie cookie)
response.getCookies()

Since a cookie is a type of header, can't I just use something like this: 由于cookie是一种标题,我不能只使用这样的东西:

String cookieVal = response.getHeader("cookie")

I'm having difficulty understanding session management and how it relates to the HttpServletRequest and HttpServletResponse API.. What is the HttpSession object for? 我很难理解会话管理以及它与HttpServletRequest和HttpServletResponse API的关系。什么是HttpSession对象?

HttpSession.getAttribute() // What is this getting??
HttpSession.setAttribute("Bla Bla", "valuetoset") // What is this setting?

You can read the RFC describing Cookies and the related headers, Set-Cookie and Cookie to understand what they are. 您可以阅读描述CookieRFC和相关标题, Set-CookieCookie以了解它们是什么。

You can go through Chapter 7 of the Servlet Specification if you want to understand in detail how Cookies and Sessions are related. 如果要详细了解Cookie和会话的关联方式,可以查看Servlet规范的第7章

You first need to understand that HTTP is a stateless protocol . 您首先需要了解HTTP是无状态协议 This means that each request that a client makes has no relation to any previous or future requests. 这意味着客户端发出的每个请求与任何先前或将来的请求无关。 However, as users, we very much want some state when interacting with a web application. 但是,作为用户,我们在与Web应用程序交互时非常需要某种状态。 A bank application, for example, only wants you to be able to see and manage your transactions. 例如,银行应用程序只希望您能够查看和管理您的交易。 A music streaming website might want to recommend some good beats based on what you've already heard. 音乐流媒体网站可能希望根据您已经听过的内容推荐一些好的节拍。

To achieve this, the Cookie and Session concepts were introduced. 为此,引入了CookieSession概念。 Cookies are key-value pairs, but with a specific format (see the links). Cookie是键值对,但具有特定格式(请参阅链接)。 Sessions are server-side entities that store information (in memory or persisted) that spans multiple requests/responses between the server and the client. 会话是服务器端实体,用于存储跨服务器和客户端之间的多个请求/响应的信息(在内存中或持久存储)。

The Servlet HTTP session uses a cookie with the name JSESSIONID and a value that identifies the session. Servlet HTTP会话使用名为JSESSIONID的cookie和标识会话的值。

The Servlet container keeps a map (YMMV) of HttpSession objects and these identifiers. Servlet容器保存HttpSession对象的映射(YMMV)和这些标识符。 When a client first makes a request, the server creates an HttpSession object with a unique identifier and stores it in its map. 当客户端首先发出请求时,服务器会创建一个具有唯一标识符的HttpSession对象,并将其存储在其映射中。 It then adds a Set-Cookie header in the response. 然后在响应中添加Set-Cookie标头。 It sets the cookie's name to JSESSIONID and its value to the identifier it just created. 它将cookie的名称设置为JSESSIONID并将其值设置为刚刚创建的标识符。

This is the most basic Cookie that a server uses. 这是服务器使用的最基本的Cookie。 You can set any number of them with any information you wish. 您可以使用任何所需信息设置任意数量的数据。 The Servlet API makes that a little simpler for you with the HttpServletResponse#addCookie(Cookie) method but you could do it yourself with the HttpServletResponse#addHeader(String, String) method. Servlet API使用HttpServletResponse#addCookie(Cookie)方法使您更简单,但您可以使用HttpServletResponse#addHeader(String, String)方法HttpServletResponse#addHeader(String, String)

The client receives these cookies and can store them somewhere, typically in a text file. 客户端接收这些cookie并可以将它们存储在某个地方,通常是在文本文件中。 When sending a new request to the server, it can use that cookie in the request's Cookie header to notify the server that it might have done a previous request. 向服务器发送新请求时,它可以在请求的Cookie标头中使用该cookie来通知服务器它可能已经完成了先前的请求。

When the Servlet container receives the request, it extracts the Cookie header value and tries to retrieve an HttpSession object from its map by using the key in the JSESSIONID cookie. Servlet容器收到请求时,它会提取Cookie标头值并尝试使用JSESSIONID cookie中的密钥从其映射中检索HttpSession对象。 This HttpSession object is then attached to the HttpServletRequest object that the Servlet container creates and passes to your Servlet . 然后将此HttpSession对象附加到Servlet容器创建并传递给ServletHttpServletRequest对象。 You can use the setAttribute(String, Object) and getAttribute(String) methods to manage state. 您可以使用setAttribute(String, Object)getAttribute(String)方法来管理状态。

You are correct that cookies are managed using headers. 你是正确的,使用标头管理cookie。 There are TWO cookie management related headers: Cookie and Set-Cookie . 有两个与cookie管理相关的标题: CookieSet-Cookie

Cookie header is sent by the user agent (browser) and will be available in your HttpServletRequest object and the Set-Cookie header is appended to your HttpServletResponse object when you use methods such as addCookie(Cookie) . Cookie标头由用户代理(浏览器)发送,并且在您的HttpServletRequest对象中可用,并且当您使用addCookie(Cookie)等方法时, Set-Cookie标头将附加到您的HttpServletResponse对象。

In Java an HttpSession is established when the first request reaches your application. 在Java中,当第一个请求到达您的应用程序时,就会建立HttpSession。 The Servlet Spec implementation in your container (Jetty, Tomcat, WebSphere, etc) will create and manage the HttpSession. 容器中的Servlet Spec实现(Jetty,Tomcat,WebSphere等)将创建和管理HttpSession。 The browser will receive a JSESSIONID cookie which will identify this particular session in the future. 浏览器将收到一个JSESSIONID cookie,以便将来识别此特定会话。

Agreeing with the answers given above, I would like to conclude that Cookie and Session are two different entities in the world of web. 同意上面给出的答案,我想得出结论,Cookie和Session是Web世界中的两个不同实体。

Cookie 曲奇饼

Cookie represents some brief information that's generated by server and stored on client(browser). Cookie表示由服务器生成并存储在客户端(浏览器)上的一些简要信息。 According to HTTP mechanism, browser have to send all the cookies(that have not expired), that server had sent before to browser. 根据HTTP机制,浏览器必须发送服务器之前发送到浏览器的所有cookie(尚未过期)。

Session 会议

HTTP is a stateless protocol. HTTP是无状态协议。 Unlike FTP and other protocol, where connection state is preserved between multiple request-response transaction, in HTTP connection is established for one request and it's closed when response for that request is satisfied. 与FTP和其他协议不同,在多个请求 - 响应事务之间保持连接状态,在一个请求中建立HTTP连接,并在满足该请求的响应时关闭。 This flaw in HTTP is present, because it was designed in early days to serve static web pages only. HTTP中的这个缺陷是存在的,因为它是在早期设计的,仅用于提供静态网页。 But as web has expanded, it's now used to serve dynamic full-fledged webapps. 但随着网络的扩展,它现在用于提供动态的成熟网络应用程序。 Thus, it has become necessary to identify users. 因此,有必要识别用户。 Thus, for every request served by web-server, a labeling mechanism is required which can identify user of each request. 因此,对于由web服务器提供的每个请求,需要标记机制,其可以标识每个请求的用户。 This identification of user of request(whether the request has came from same user, same machine), sessions are used. 使用该请求的用户的标识(请求是否来自同一用户,同一台机器),会话。
Session can be successfully implemented only if web-server can receive any information about the user in the request. 仅当Web服务器可以在请求中接收有关用户的任何信息时,才能成功实现会话。 One way of making this information available to user is Cookie. 向用户提供此信息的一种方法是Cookie。 Others are URL rewriting, hidden fields, etc. 其他是URL重写,隐藏字段等。

session.setAttribute() will store information in current session on server side not on client side(browser). session.setAttribute()将信息存储在服务器端的当前会话中,而不是存储在客户端(浏览器)上。

Hope it may help you. 希望它可以帮到你。

Ok Looks like you want to see the difference between Cookies and Headers. 好看你想看看Cookies和Headers之间的区别。 They have different purpose. 他们有不同的目的。 Cookies are temporary storage of information on client side. Cookie是客户端的临时信息存储。 Server set the cookies(data) on the response and once set browser send these cookies(data) with each subsequent requests till the cookie expires . 服务器在响应上设置cookie(数据),一旦设置浏览器,每次后续请求都会发送这些cookie(数据),直到cookie过期 But headers are used as hints to browser and server. 但标题用作浏览器和服务器的提示。 For ex 对于前者

setHeader("Content-Type", "application/json");

will inform client to prepare to see a json response in the payload. 将通知客户端准备在有效负载中查看json响应。 Since it is a "one time" information there is not need the browser to send that information back to the server with each new requests like cookies. 由于它是“一次性”信息,因此不需要浏览器将每个新请求(如cookie)发送回服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM