Java 如何创建会话对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3647909/
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-08-14 03:08:24  来源:igfitidea点击:

How to create a session object?

javasessionservletsdwr

提问by

I am creating a login page for my web application. I want to create a session object whenever a new user logged in. I know the concept of sessions, but i didnt used that before. Can i do it with a simple class. Or, i have to move to servlet. If i shall do it with a simple class means, how to create a session object.

我正在为我的 Web 应用程序创建一个登录页面。我想在新用户登录时创建一个会话对象。我知道会话的概念,但我以前没有使用过。我可以用一个简单的类来做到这一点。或者,我必须转移到 servlet。如果我用一个简单的类方法来做,如何创建一个会话对象。



This is my scenario...

这是我的场景...

The HTML CODE:

HTML代码:

<table>
<tr>
<td>User Name: </td><td><input id="uName"  class="required" type="text" 
    size="5" /></td>
</tr>
<tr>
<td>Password: </td><td><input id="pwd"  class="required" type="text" size="5"
    onclick="login()"/></td>
</tr>
</table>

The JS Code:

JS代码:

function login(){
var userDetails = { uName : null, pwd : null };
dwr.util.getValues(userDetails);//Yes, i am using DWR.
LoginAuthentication.doLogin(userDetails, loginResult);
}

 function loginResult(nextPage){
window.location.href = nextPage;
}

The Java Code:

Java代码:

public class LoginAuthentication
{
public String doLogin(User user) throws SQLException, ClassNotFoundException{
    String userName = user.getUserName();
    boolean loginResult = verifyUser(user);//Another method that verifies user details with the DB.
    if (loginResult == true){
        /* Here I have to create session object,
          and i want to add the current username in that object. How to do it.*/

        return "MainPage.html";
    }
    else{

        return "loginRetryPage.html";

    }
   }


The concept that was given to me about session is pretty simple and clear. I have to create a session object after a valid user input & add the user name to that object, Destroy the object when logout was clicked. But i didnt worked on sessions before. I mean, i dont know the syntax to create a session variable.

给我的关于会话的概念非常简单明了。我必须在有效的用户输入后创建一个会话对象并将用户名添加到该对象,单击注销时销毁该对象。但我之前没有参加过会议。我的意思是,我不知道创建会话变量的语法。

How shall i create a session Object here?

我将如何在这里创建会话对象?

Any suggestions would be more appreciative!!!

任何建议将更感激!!!

Thanks in Advance!!!

提前致谢!!!

采纳答案by Bozho

In a servlet a session is obtained with the following line:

在 servlet 中,使用以下行获取会话:

Session session = request.getSession();

And to get the requestobject with DWR, you do (see here):

request使用 DWR获取对象,您可以这样做(请参阅此处):

WebContext ctx = WebContextFactory.get();
HttpServletRequest request = ctx.getHttpServletRequest();

(The HttpServletRequestcontains all data about the HTTP request that has been sent by the browser to the server)

HttpServletRequest包含浏览器发送到服务器的所有HTTP请求数据)

回答by Dead Programmer

It is better to always use request.getSession(false); after successful login.

最好总是使用 request.getSession(false); 登录成功后。