如何创建会话对象?

发布于 2024-09-18 12:24:33 字数 1630 浏览 7 评论 0原文

我正在为我的网络应用程序创建一个登录页面。我想每当 新用户登录。我知道会话的概念,但我以前没有使用过。我可以通过一个简单的课程来完成吗?或者,我必须转向 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";

    }
   }

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

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

任何建议将更加感激!

提前致谢!!!

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.


This is my scenario...

The HTML CODE:

<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:

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:

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!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-09-25 12:24:33

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

Session session = request.getSession();

要使用 DWR 获取 request 对象,您需要执行 (参见此处):(

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

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

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

Session session = request.getSession();

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

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

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

帅气尐潴 2024-09-25 12:24:33

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文