如何从 JSESSIONID 加载 Java HttpSession? [英] How can i load Java HttpSession from JSESSIONID?

查看:33
本文介绍了如何从 JSESSIONID 加载 Java HttpSession?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 JSESSIONID 获取 Java HttpSession.是否可以?如果是,如何?

I want to get Java HttpSession by JSESSIONID. Is it possible? If yes, how?

推荐答案

您需要将它们全部收集到 Map 使用 HttpSessionListener 自己.

You need to collect them all in a Map using a HttpSessionListener yourself.

public class HttpSessionCollector implements HttpSessionListener {
    private static final Map<String, HttpSession> sessions = new HashMap<String, HttpSession>();

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        sessions.put(session.getId(), session);
    }


    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        sessions.remove(event.getSession().getId());
    }

    public static HttpSession find(String sessionId) {
        return sessions.get(sessionId);
    }

}

web.xml中注册如下即可运行:

Just register it in web.xml as follows to run it:

<listener>
    <listener-class>com.example.HttpSessionCollector</listener-class>
</listener>

然后,在您想要的任何地方执行 HttpSessionCollector.find(sessionId) 以获取有问题的 HttpSession.

Then, anywhere you want just do HttpSessionCollector.find(sessionId) to get the HttpSession in question.

也就是说,这是一种巨大的气味.肯定有比这更好的方法来解决实际功能需求;) 正如我在您的 后续问题:

That said, this is a huge smell. There are certainly better ways to solve the actual functional requirement than this ;) As I commented in your follow-up question:

这是您第二次提出在现实世界中永远不应该被实践的问题.老实说,这都是臭味.是什么问题,您认为在服务器端获取与 JESSIONID 关联的 HttpSession 并在客户端获取 JSESSIONID 值的问题是解决方案"?在一个新问题中详细说明这一点,您将获得如何以正确的方式进行操作的答案.

This is the 2nd time that you asked a question which in real world should never be practiced. Honestly said, this all smells. What is it, the problem for which you think that getting the HttpSession associated with JSESSONID in server side and getting the JSESSIONID value in client side is "the" solution? Elaborate about this in a new question, you'll get answers how to do it the right way.

严肃点.我们不是在取笑您,我们只是想帮助您朝着正确的方向发展,避免您的项目/网络应用因安全漏洞和不良做法而中断和/或您被解雇.

Take it serious. We're not teasing you, we're just trying to help you in the right direction to avoid that your project/webapp will break due to security holes and bad practices and/or that you will get fired.

这篇关于如何从 JSESSIONID 加载 Java HttpSession?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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