0

我正在为我的 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>

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

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

    }
   }

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

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

任何建议将更加感激!

提前致谢!!!

4

2 回答 2

5

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

Session session = request.getSession();

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

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

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

于 2010-09-05T21:49:03.643 回答
2

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

于 2010-09-06T06:43:54.043 回答