Java 中的会话是什么?
到目前为止我了解了Java中的HttpSession概念。
HttpSession ses = req.getSession(true);
将根据请求创建一个会话对象。
setAttribute("String", object);
将,将“字符串”和值与会话对象绑定。
getAttribute("String");
将返回与指定字符串关联的对象。
我无法理解的是:我正在创建一个会话对象,例如 HttpSession ses = req.getSession(true);
并通过调用 setAttribute("String", object); 为其设置名称。 在这里,此代码驻留在服务器内部。对于每个人来说,当他尝试登录时,服务器中的相同代码将被执行。 setAttribute("String", object);
在此方法中,字符串值是常量 1。因此,创建的每个会话对象都将由我提供的相同字符串绑定。当我尝试检索字符串以验证他的会话时或在注销操作时采取 getAttribute("String");
将返回相同的常量字符串值(我对吗!!?实际上我不'不知道,我只是在考虑它的执行逻辑)。那我怎么才能失效呢。
我在网上的所有教程中都看到过这种类型的插图。这是设置该属性的实际方法吗?或者,真正的应用程序开发人员会在“String”字段中给出一个变量来动态设置它
(即session.setAttribut(userName,userName); //Setting the String Dynamically..我不知道它是否正确。 )
我的最后一个问题是
WebContext ctx = WebContextFactory.get();
request = ctx.getHttpServletRequest();
上面两行的作用是什么? ctx 和 ctx 中将存储什么?要求? HttpSession ses = req.getSession(true);
将创建新的会话方法。 ses 中存储了什么值。
So far I understand Httpsession concepts in Java.
HttpSession ses = req.getSession(true);
will create a session object, according to the request.
setAttribute("String", object);
will, bind the 'String', and value with the Session object.
getAttribute("String");
will return an object associated with the string, specified.
What I am not able to understand is: I am creating a session object likeHttpSession ses = req.getSession(true);
and setting a name for it by calling setAttribute("String", object);
.
Here, This code resides inside the server. For every person, when he tries to login the same code in the server will be executed. setAttribute("String", object);
in this method the string value is a constant one. So, each session object created will be binded by the same string which I have provided. When I try to retrieve the string to validate his session or while logout action taken the getAttribute("String");
ll return the same constant string value(Am I right!!?? Actually I don't know, I'm just thinking of its logic of execution). Then, how can I be able to invalidate.
I saw this type of illustration in all of the tutorials on the WEB. Is it the actual way to set that attribute? Or, real application developers will give a variable in the "String" field to set it dynamically
(ie. session.setAttribut(userName, userName); //Setting the String Dynamically.. I dono is it right or not.)
And my final question is
WebContext ctx = WebContextFactory.get();
request = ctx.getHttpServletRequest();
What do the two lines above do? What will be stored in ctx & request?HttpSession ses = req.getSession(true);
will creates new session means. What value stored in ses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些[随机]精度:
request.getSession(true)
时,都会检查HttpRequest
对象,以便查找编码在 cookie 中或/和 URL 路径中的会话 ID参数(分号后面的内容)。如果找不到会话 ID,servlet 容器(即服务器)将创建一个新会话。response.encodeURL()
方法修改 HTML 文档中的链接。如果未找到会话 ID 或会话 ID 引用无效会话,则调用request.getSession(false)
或简单地调用request.getSession()
将返回 null。web.xml
文件中配置。invalidate()
方法显式使给定会话失效。Some [random] precisions:
request.getSession(true)
, theHttpRequest
object will be inspected in order to find a session ID encoded either in a cookie OR/AND in the URL path parameter (what's following a semi-colon). If the session ID cannot be found, a new session will be created by the servlet container (i.e. the server).response.encodeURL()
method. Callingrequest.getSession(false)
or simplyrequest.getSession()
will return null in the event the session ID is not found or the session ID refers to an invalid session.web.xml
file.invalidate()
method.JSESSIONID
, they are referring to the standard name of the HTTP cookie used to do session-tracking in Java.我建议您阅读教程 关于 Java 会话。每个用户根据 Java Web 服务器发送到浏览器的 JSESSIONID 请求/响应参数获取不同的 HttpSession 对象。因此,每个用户都可以拥有一个同名的属性,并且为该属性存储的值对于所有用户来说都是不同的。
此外,WebContextFactory 和 WebContext 是 DWR 类,它们提供了一种获取 servlet 参数的简单方法。
I suggest you read a tutorial on Java sessions. Each user gets a different HttpSession object, based on a JSESSIONID request/response parameter that the Java web server sends to the browser. So every user can have an attribute with the same name, and the value stored for this attribute will be different for all users.
Also, WebContextFactory and WebContext are DWR classes that provide an easy way to get the servlet parameters.
据我了解,您担心的是在 HttpSession 中存储内容时不同用户的分离。
Servlet 容器(例如 Tomcat)利用其 JSESSIONID 来处理此问题。
故事是这样的:
用户的浏览器,存储一个唯一的
jsessionId。
网站,JSESSIONID cookie 是
送回来了。
跟踪谁是谁。
数据的分离。每一个
用户有自己的存储桶
唯一标识的对象
JSSessionID。
希望这(至少部分)回答了您的问题。
干杯
As I understand it, your concerns are about separation of the different users when storing things in the HttpSession.
The servlet container (for example Tomcat) takes care of this utilizing its JSESSIONID.
The story goes like this :
the user's browser, storing a UNIQUE
jsessionId.
website, the JSESSIONID cookie is
sent back.
keep track of who is who.
of the separation of data. Every
user has their own bucket of
objects uniquely identified by the
JSESSIONID.
Hopefully that (at least partially) answers your question.
Cheers
您的基本 servlet 将如下所示
无需为已完成的会话设置任何属性名称。正如其他人在其他答案中建议的那样,使用 cookie 或 URL 重写来为您存储 sessionID。
当您处理 DWR WebContext 时,它只是执行与上面相同的操作,只是通常不会将 Request 对象传递到方法中,因此您使用 WebContext 来获取该请求
Your basic servlet is going to look like
There is no need to set any attribute names for your session that is already done. As others have suggested in other answers, use cookies or URL re-writing to store the sessionID for you.
When you are dealing with the DWR WebContext, it is simply doing the same thing as above, just normally the Request object isn't passed into the method, so you use the WebContext to get that request for you