简体   繁体   English

用Seam拦截HTTP会话的结束

[英]Intercepting the end of the HTTP Session with Seam

I need to intercept the end of each request a user does against a Seam web app. 我需要拦截用户针对Seam Web应用程序所做的每个请求的结尾。

While Filters seem to be the right tool for catching requests before they hit your business logic, Interceptors seem to be useful as advices for individual classes... 过滤器似乎是在请求到达业务逻辑之前捕获请求的正确工具,而拦截器似乎对于单个类的建议很有用...

Thanks! 谢谢!

Your question is regarding the interception of the end of an Http Session not Request. 您的问题是关于Http Session not Request的拦截。 If that is the case, you should be able to implement a Session Listener. 在这种情况下,您应该能够实现会话监听器。 Declare that in your web.xml. 在您的web.xml中声明。 You will have to use the static methods to get component references as this will not be a Seam component. 您将必须使用静态方法来获取组件引用,因为这不是Seam组件。

Otherwise, you may want to implement a component that simply observes the end of the session context. 否则,您可能想要实现一个仅观察会话上下文结束的组件。 It can be an event or application-scoped component. 它可以是事件或应用程序范围的组件。

@Scope(ScopeType.EVENT)
@Name("com.yourdomain.observer.sessionObserver")
public class SessionObserver
{

@In
private EntityManager entityManager;

// automatically create this component when the session is destroyed (actually just before it is)
@Observer("org.jboss.seam.context.preContextDestroy.SESSION", create = true)
@Transactional
public void onSessionDestroyed()
{
  entityManager.persist(httpSession);
}
}

That should be close to working, but there may be some typos. 那应该接近工作了,但是可能会有一些错别字。 This should answer your question in regards to observing the end of the session context. 这应该回答您有关观察会话上下文结束的问题。

Walter 沃尔特

Using the FILTERS you can intercept the request and response objects. 使用FILTERS,您可以拦截请求和响应对象。 Byn using the filter interface you can intercept the request obects. 通过使用筛选器接口,您可以拦截请求对象。 void init(FilterConfig filterConfig) throws ServletException void init(FilterConfig filterConfig)抛出ServletException

By using the doFilter method void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException 通过使用doFilter方法,无效doFilter(ServletRequest请求,ServletResponse响应,FilterChain链)引发IOException,ServletException

using the above method you can intercept the request objects. 使用以上方法,您可以拦截请求对象。

The topic title conflicts with the question in the message. 主题标题与消息中的问题冲突。 You're asking in the title how to hook on the end of session , but in the message you're asking how to hook on the end of request . 您在标题中询问如何在会话结束时挂机,但在消息中您询问如何在请求结束时挂机。 Those are two entirely different scopes. 这是两个完全不同的范围。 The session lives from the first request a client ever made for which no HttpSession object is been created until the time that it times out or got invalidated. 该会话从客户端发出的第一个请求开始,直到该客户端超时或被无效为止,该客户端没有为其创建HttpSession对象。 The request lives from the first click/bookmark/addressbar-invocation of the client until the associated response has been fully committed and sent. 该请求从客户端的第一次单击/书签/地址栏调用开始,直到关联的响应已完全提交并发送为止。

Let's assume that you actually meant request as your're already talking about the benefit of filters to hook some code before the request is been processed. 假设您实际上是在说请求,因为您已经在谈论过滤器在处理请求之前挂钩某些代码的好处。 You probably didn't realize that you can use the very same Filter to hook some code after the request is been processed. 您可能没有意识到,在处理完请求之后 ,可以使用相同的Filter来挂接一些代码。 All you need to do is to just put the appropriate code after the FilterChain#doFilter() . 所有你需要做的是只要把相应的代码 FilterChain#doFilter()

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    // You can do here stuff BEFORE the request is been processed further.

    chain.doFilter(request, response);

    // You can do here stuff AFTER the request is been processed.
}

You probably expected that the FilterChain#doFilter() kind of automagically exits the method block immediately, like as many starters would expect that for for example HttpServletResponse#sendRedirect() and consorts. 您可能希望FilterChain#doFilter()立即自动退出方法块,就像许多初学者一样,例如HttpServletResponse#sendRedirect()和consorts。 This is untrue, it's the return statement and/or just the end of method block which does that, aside from exceptions/errors. 这是不正确的,除了异常/错误之外, return语句和/或方法块的末尾才执行此操作。 Those methods are just invoked the usual Java way and does nothing special. 这些方法只是用通常的Java方式调用的,没有什么特别的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM