如何在 Java 中创建会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16662720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:35:38  来源:igfitidea点击:

How to make session in Java?

javaweb-applications

提问by Kliver Max

I need make sessions in Java web application.
I found that sesstion makes in servlet calass by method getSession().
But i have a question about session parameters. For example i send to server login/pass and save it into session atributes. Okey. Next time i do something on client and send new params to server. Where i gonna send it? In another or same and i gonna use if elsefor handle params like this?

我需要在 Java Web 应用程序中创建会话。
我发现 Session 通过 method 在 servlet calass 中进行getSession()
但我有一个关于会话参数的问题。例如,我发送到服务器登录/通过并将其保存到会话属性中。好的。下次我在客户端上做一些事情并将新的参数发送到服务器。我要把它寄到哪里?在另一个或相同的情况下,我将if else用于处理这样的参数?

enter image description hereAnother question: How to use params which i put in session(login/pass) in another classes?

在此处输入图片说明另一个问题:如何使用我在其他课程中放入会话(登录/通过)的参数?

UPDATE

更新

I read about sessions. And have new question. How to use session params in enother class. I mean after login i send new params on server, read it in servlet and want to take a login/pass from session and send it with new params into another class.

我阅读了关于session 的信息。并有新问题。如何在另一个类中使用会话参数。我的意思是登录后,我在服务器上发送新的参数,在 servlet 中读取它并希望从会话中获取登录/传递并将其与新参数一起发送到另一个类中。

回答by Juned Ahsan

As part of your request handling in a doGet or doPost method, here is how you can get session and use it to get and set variables.

作为在 doGet 或 doPost 方法中处理请求的一部分,以下是获取会话并使用它来获取和设置变量的方法。

//Obtain the session object, create a new session if doesn't exist
HttpSession session = request.getSession(true);

//set a string session attribute
session.setAttribute("MySessionVariable", "MySessionAtrValue");

//get a string sessson attribute
String strParam = session.getAttribute("MySessionVariable");


//get an integer sessioin attribute
Integer param = (Integer) session.getAttribute("MySessionVariable");

//set an integer session attribute
session.setAttribute("MySessionVariable", new Integer(param.intValue() + 1));

回答by amicngh

Sessionis associated with each request . Now it depends whether client join the session or not there are three overloaded methods of getSession()to get more about them please go through the documents. Now if session is aleady associated with the request get existing session set the attribute in session and vice-versa if not create new session and do the same.

Session与每个请求相关联。现在取决于客户端是否加入会话,有三种重载方法getSession()可以了解更多关于它们的信息,请查看文档。现在,如果会话与请求相关联,则获取现有会话在会话中设置属性,反之亦然,如果不创建新会话并执行相同操作。

if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new on each request.

I hope this helped.

我希望这有帮助。