在 HttpSessionListener 中如何获取 HttpServletRequest? [英] How can I get HttpServletRequest when in an HttpSessionListener?

查看:20
本文介绍了在 HttpSessionListener 中如何获取 HttpServletRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 SessionListener 访问请求标头?

How can I access request headers from a SessionListener?

我需要在当前会话创建时设置超时.超时需要根据 HttpServletRequest 中的标头而变化.我已经有一个 SessionListener(实现 HttpSessionListener)来记录新会话的创建和销毁,它似乎是设置超时的最合乎逻辑的地方.

I need to set a timeout on the current session when it is created. The timeout needs to vary based on a header in the HttpServletRequest. I already have a SessionListener (implements HttpSessionListener) that logs the creation and destruction of new sessions, and it seems to be the most logical place to set the timeout.

我尝试了以下方法,但它总是将 ctx 设置为 null.

I've tried the following, but it always sets ctx to null.

FacesContext ctx = FacesContext.getCurrentInstance();

推荐答案

HttpSessionListener 无权访问请求,因为它在没有发出请求时被调用——通知会话销毁.

The HttpSessionListener does not have access to the request because it is invoked when no request has been made—to notify of session destruction.

因此,FilterServlet 将是检查请求和指定会话超时的更好位置.

So, a Filter or Servlet would be better places to examine the request and specify the session timeout.

这篇关于在 HttpSessionListener 中如何获取 HttpServletRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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