简体   繁体   中英

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.

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. If that is the case, you should be able to implement a Session Listener. Declare that in your web.xml. You will have to use the static methods to get component references as this will not be a Seam component.

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. Byn using the filter interface you can intercept the request obects. void init(FilterConfig filterConfig) throws ServletException

By using the doFilter method void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws 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. 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. All you need to do is to just put the appropriate code after the 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. This is untrue, it's the return statement and/or just the end of method block which does that, aside from exceptions/errors. Those methods are just invoked the usual Java way and does nothing special.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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