wood burning stoves 2.0
The moose likes Servlets and the fly likes RequestDispatcher and thread-safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher and thread-safe" Watch "RequestDispatcher and thread-safe" New topic

RequestDispatcher and thread-safe

Satheesh Satti

Joined: Sep 12, 2005
Posts: 10

Can some one please answer the following question.
Is the RequestDispatcher returned by getRequestDispatcher(java.lang.String path) of ServletContext class thread-safe.

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you declare it in (and keep it local to) the service method (doPost, doGet...) it is.

If you declare it as an instance variable and set it's value in the service method, no.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Satheesh Satti

Joined: Sep 12, 2005
Posts: 10
Many Thanks for ur reply.

It is an instance variable and it is being set once in the init method of the servlet which will be called only once when the servlet is loaded and later on I am using it to do the forwards in the service methods(doGet etc..,).Is it safe to do something like that.

Thanks again,
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63837

Absolutely not. Re-read Ben's reply.
[ September 12, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

What make you think that request dispatcher returns every time a new instance instead of reusing one?

Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Satheesh Satti

Joined: Sep 12, 2005
Posts: 10
Thanks for all the responses

I am wondering what spec says about it and API doesn�t say that RequestDispatcher returned by servletContext is not Thread Safe. I am using Tomcat and hence can know whether I can do it or not by looking in to the RequestDispatcher implementation. But that makes my code vendor specific and didn�t want to do something like it.

I am using at as read only inside the Service Methods by assigning it once in the init Method.

I agree. Here's the link: http://aspose.com/file-tools
subject: RequestDispatcher and thread-safe
It's not a secret anymore!