Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doing Forward in a Filter  RSS feed

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we do a RequestDispatcher.forward() inside a filter? If yes, what would happen to the filter chain and the original servlet?

Actually this question could be turned into another one: is the RequestDispatcher object independent of where it is obtained? That means, say, is it possible to use a ContextListener calling ServletContext.getRequestDispatcher() and store a set of RequestDispatcher objects into ServletContext and retrieve them later in our Servlet. Let's not consider if this is a practical approach. But, for the purpose of clarifying the nature of RequestDispatcher, is it possible?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alec Lee:
Can we do a RequestDispatcher.forward() inside a filter? If yes, what would happen to the filter chain and the original servlet?

Yes.

You have to branch your code in such a way that either requestDispatcher.forward OR chain.doFilter is called. If you try to do both, you'll get an IllegalStateException.
[ December 13, 2007: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alec Lee:

Actually this question could be turned into another one: is the RequestDispatcher object independent of where it is obtained? That means, say, is it possible to use a ContextListener calling ServletContext.getRequestDispatcher() and store a set of RequestDispatcher objects into ServletContext and retrieve them later in our Servlet. Let's not consider if this is a practical approach. But, for the purpose of clarifying the nature of RequestDispatcher, is it possible?


Look at the getNamedDispatcher method.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getNamedDispatcher(java.lang.String)
You can actually define them in your deployment descriptor if you like.

There is a link to the servlet spec in my signature.
It sounds like you're ready to read the section on RequestDispatchers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!