如何创建会话对象?

0 java session servlets dwr

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


这是我的情景......

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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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";

    }
   }
Run Code Online (Sandbox Code Playgroud)

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

我该如何在这里创建一个会话对象?

任何建议都会更加赞赏!

提前致谢!!!

Boz*_*zho 5

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

Session session = request.getSession();
Run Code Online (Sandbox Code Playgroud)

request使用DWR 获取对象,请执行操作(请参阅此处):

WebContext ctx = WebContextFactory.get();
HttpServletRequest request = ctx.getHttpServletRequest();
Run Code Online (Sandbox Code Playgroud)

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