my dog learned polymorphism
The moose likes Servlets and the fly likes Problem with RequestDispatcher. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with RequestDispatcher." Watch "Problem with RequestDispatcher." New topic

Problem with RequestDispatcher.

Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Hi ppl,
i am facing a problem with RequestDispatcher and need help about the possible causes as it does not repeat itself and arises on a random basis.

what i am doin in my servlet which acts as a controller is on a request received i do the processing and then fwd the data to the revelent page.
The flow is
jsp -> control servlet -> java class -> control servlet -> jsp
now sometimes when two users are using using the applications sometimes one of the users gets the page which was meant for the other user. I have module wise control servlet and this problem arises even when the users are working on diff modules...
the code i have used in the servet for eg :
" RequestDispatcher oRequestDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/common/jsp/login.jsp");
can anybody help me out with where i may be goin wrong ...???
Thanking all in advance.
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
It sounds like your controller has not been written in a thread-safe manner. Make sure that you have NO instance variables defined on the Controller so that 2 threads can pass through the doGet()/doPost() method simultaneously.

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Thanx andy,

ya what u said is correct i was using a HashMap which was declared at class level to store the information about which jsp to fwd to after the processing was done i have changed it to a method level scope to take care of the sync problem.
While going thru the api of a HashMap and Hashtable i read that HashMap is unsynchronized while Hashtable is syncronized so what i wanted to know is what extra precautions we need to take while using a HashMap vis a vis a Hashtable as a storage element in a threaded model or which one is preferable .
by the way just on second thoughts can anyone tell me why is the M in caps in HashMap and the t is small in Hashtable now i kno this sounds silly but there has to be some reason to it ... ...
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Seconding Andy. And, what I'd really like to know, in a world that has freely available products like Apache Jakarta Struts, what made you decide to reinvent the wheel? Especially if you have no background in concurrent programming?
Regarding HashMap vs Hashtable, things are simple. Hashtable is an old legacy class which (together with Vector, Stack and Enumeration) has been effectively superseded by the Collections framework (HashMap, ArrayList, LinkedList, Iterator, etc). Don't use any of the old classes if you can at all avoid it.
If you need a synchronized Map, run your HashMap through the Collections.synchronizedMap() wrapper. You will very rarely need this, because it's rarely the little Map methods that need to be atomic with regard to other threads. Your question is a case in point: synchronization wouldn't have made a difference. The code was thread-unsafe regardless of whether your Map was synchronized or not. As mentioned, beware of any mutable state that is not method-local, for example instance variables. Lookhere for more information about threads and multithreaded programming.
There is no particular significance to the difference in capitalisation between Hashtable and HashMap; the Sun boys obviously regarded "hashtable" as one single word and "hash map" as two words.
- Peter
[ April 07, 2003: Message edited by: Peter den Haan ]
I agree. Here's the link:
subject: Problem with RequestDispatcher.
It's not a secret anymore!