简体   繁体   English

Java会话-最佳实践?

[英]Session in java - Any best practises?

I came across a code where the session object is obtained in two different ways (or rather wrote in two different ways). 我遇到了一个代码,其中以两种不同的方式(或者以两种不同的方式编写)获取了会话对象。

using HttpServletRequest 使用HttpServletRequest

 someMethod(HttpServletRequest request){
 HttpSession session = request.getSession();
 //getAttribute from session
 }

And using HttpSession 并使用HttpSession

anotherMethod(HttpSession session){
//getAttribute from session
}

I went through this article and a question on SO. 我浏览了这篇文章 ,并对SO 提出了一个问题 But i am still have some doubts. 但是我仍然有一些疑问。

Can someone help me understand what is the difference between these? 有人可以帮助我了解两者之间的区别吗?

UPDATE 更新

Both of these are methods in a spring controller and these are mapped to different ajax calls. 这两个都是spring控制器中的方法,它们被映射到不同的ajax调用。 I understand that there is a session associated with every request object but when you pass an HttpSession object where does it find the current session object(load all the attributes) or how is it obtained? 我知道每个请求对象都有一个会话,但是当您传递HttpSession对象时,它将在哪里找到当前会话对象(加载所有属性)或如何获取? When I call the method from javascript, I don't pass anything at all. 当我从javascript调用方法时,我什么都不传递。

someMethod(HttpServletRequest request)

In this you are passing the current request object, from which you can obtain your current session and then you can get attributes from it.. You can get the current session object from your request object by using : - 在此过程中,您传递了当前request对象,可以从中获取current session ,然后从中获取属性。您可以使用以下命令从请求对象中获取当前会话对象:

request.getSession(false)

*NOTE: - We pass false as a parameter to getSession(false) to get any existing session.. If no session exist it will return null .. whereas, request.getSession() will always create a new session, so you won't get any prevoius attribute store in other session.. *注意:-我们将false作为参数传递给getSession(false)以获取任何现有会话。如果不存在任何会话,它将返回null ..而request.getSession()将始终创建一个新会话,因此您不会无法在其他会话中获取任何prevoius属性存储。

anotherMethod(HttpSession session)

Here you are passing the session object itself from somewhere.. Might be because, your session object contains many attributes, and you don't want to many parameters in the method.. 在这里,您是从某个地方传递session对象本身的。。可能是因为,您的session对象包含许多属性,并且您不想在该方法中使用许多参数。

But you should do all this session related task in your Servlet and pass the attribute to the methods of other class.. 但是您应该在Servlet完成所有与session相关的任务,并将attribute传递给其他类的方法。

You don't need to float session objects if you have single attribute. 如果您具有单个属性,则无需浮动会话对象。 Just simply access it using session object. 只需使用会话对象即可访问它。

HttpSession session = request.getSession(true);
session.getAttribute(name);

Now only sensible case where you can float session objects is you have large number of attributes and you want each method to access its own set of attributes. 现在,您可以浮动session对象的唯一明智的情况是,您拥有大量的属性,并且希望每种方法都可以访问其自己的属性集。 In any case the method depends on session passed to it so it should not care how it was obtained. 无论如何,该方法取决于传递给它的session ,因此它不必关心如何获得它。

There is no huge difference between these two, the second method may be used if called multiple times to eliminate one extra method call request.getSession() by keeping session as somewhat like a local cache (with ignorable performance improvement unless called 100s of times). 两者之间没有太大的区别,如果多次调用可以通过将会话保持为类似于本地缓存的方式来消除一个额外的方法调用request.getSession() ,则可以使用第二种方法(除非调用了100次,否则性能会明显提高) 。

eg,. 例如,。

HttpSession session=request.getSession();
getFirstAttr(session);
getSecondAttr(session);
....
getHundredthAttr(session);

If you use the first method, then all the times that method is called one extra request.getSession() is called. 如果您使用第一种方法,则始终将该方法称为一个额外的request.getSession()

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

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