Java 如何创建会话对象?

Java 如何创建会话对象?,java,session,servlets,dwr,Java,Session,Servlets,Dwr,我正在为我的web应用程序创建登录页。我想随时创建一个会话对象 新用户已登录。我知道会话的概念,但我以前没用过。我可以用一个简单的类来完成它吗。或者,我必须转到servlet。 如果我要用一个简单的类来实现它,那就是如何创建会话对象 这是我的剧本。。。 HTML代码: <table> <tr> <td>User Name: </td><td><input id="uName" class="required" type="te

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

    }
   }
Java代码:

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

    }
   }

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

如何在此处创建会话对象?

任何建议都将更加感激


提前感谢

在servlet中,通过以下行获得会话:

Session session = request.getSession();
要使用DWR获取
请求
对象,您需要():


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

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

什么是“java代码”?它在几班?是servlet吗?@skaffman:哦,对不起。我现在要更新我的问题了。那真的没用。。。什么是
LoginAuthentication
,它与其他东西有什么关系?@skaffman:它只是一个类名。我删除了其他方法。也许我不知道如何根据一个类的用途来命名它,“请求”是什么?它的目的是什么?谢谢你的即时更新。我浏览了你的链接,似乎对我有用。但是,你能解释一下什么是WebContext吗。它能做什么?如果我加上这两行会发生什么?WebContext ctx=WebContextFactory.get();req=ctx.getHttpServletRequest();是的,会的。以上三行将为您提供会话对象,您可以在其中添加内容和检索内容。成功登录后的含义是什么。它是关于用DB检查用户名和密码或创建会话对象。