访问servlet外部的会话变量 [英] Accessing session variables outside servlet

查看:118
本文介绍了访问servlet外部的会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个java webapp,我需要根据用户登录ID访问数据库中的记录。我在成功登录后在会话变量中设置登录详细信息。

I'm working on a java webapp where I need to access records from database based on user login id.I set the login details in session variable after successful login.

我想这样做

从proj_recs中选择*其中user_id = user_id(来自会话)

Select * from proj_recs where user_id= user_id (from session)

现在我将用户名作为参数传递,但我认为这不是一个好习惯。有没有更好的方法来访问servlet之外的会话变量?

Right now I'm passing username as a parameter, but I believe it's not a good practice. Is there a better way of accessing session variables outside servlet ?

Servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        User user = (User) request.getSession().getAttribute("userInfo");

        System.out.println(user);

        if(user != null){
            Gson gson = new Gson();
            returnJsonResponse(response,gson.toJson(user));
            return;
        }
}

数据层包

public Accrual getAccruals(String accrualID,String userid) throws AccrualNotFoundException{

    String sql = Select * from db_acc where acc_id= accrualID and user_id=userid;

}

问题是我必须用userid修改我的所有方法。有没有办法可以将用户详细信息设置为某个静态类,并在不修改方法签名的情况下访问应用程序中我想要的详细信息?但我相信静态类在不同的用户请求之间共享。

The problem is I have to modify all my methods with userid. Is there a way I can set user details into some static class and access the details where ever i want in the application without modifying the method signature? But I believe static classes are share between different user requests.

推荐答案

您正在寻找的解决方案是Thread Local(google it )。它允许您使用静态方法访问特定于线程的数据。

The solution that you are looking for is Thread Local (google it). It allows you to access thread-specific data using static method.

您可以开始阅读 http://veerasundar.com/blog/2010/11/java-thread-local-how-to-使用 - 和 - 码采样/ 。使用那里的示例,您需要创建:

You can start reading http://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/. Using examples from there, you need to create:

public class MyThreadLocal {

    public static final ThreadLocal userThreadLocal = new ThreadLocal();

    public static void set(User user) {
        userThreadLocal.set(user);
    }

    public static void unset() {
        userThreadLocal.remove();
    }

    public static User get() {
        return userThreadLocal.get();
    }
}

在您的servlet中,执行以下操作:

in your servlet, do this:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    User user = (User) request.getSession().getAttribute("userInfo");
    MyThreadLocal.set(user);
    try {
       // call data layer
    } finally {
        MyThreadLocal.unset();
    }
}

在您的数据层中,您可以通过以下方式检索用户这样做:

in your data layer, you can retrieve the user by doing this:

public void dataLayerMethod(ExistingParameters parameters) {
    User user = MyThreadLocal.get();
}

请注意,您无需更改数据层的方法签名。

Notice that you don't need to change the method signature of the data layer.

线程本地起初有点令人困惑,但是一旦你阅读这篇文章,你会很快熟悉。

Thread Local is a bit confusing at first, but you will get familiar very quickly once you read the article.

这篇关于访问servlet外部的会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆