This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Request Dispatch Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Request Dispatch Problem" Watch "Request Dispatch Problem" New topic

Request Dispatch Problem

singh santosh
Ranch Hand

Joined: Jan 13, 2006
Posts: 136
can someone expalin this ...

"The Container Provider should ensure that the dispatch of the request to a target servlet occurs in the same thread of the same JVM as the original

Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
Just imagine that you use your RequestDispatcher to include a servlet (for a forward this is not so clear). You have some variables, some kind of state that you want to find again when you come back from the include call.

But a servlet instance is part of a thread, and only one thread access a servlet at once. So if a dispatch is made in another thread, the coming back could come in another thread, which means you could loose your previous state.

For this (and other reasons probably), a dispatch should be made in the same thread.

Is it clear?

SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources:
singh santosh
Ranch Hand

Joined: Jan 13, 2006
Posts: 136
hi fedric,
thanks for ur reply..
But i have a question ,since for each request container forks a thread from a servlet instance(not taking SingleThread Model into cosideration) which serves the request .But how can a request meant for thread (Lets say A) be dispatched from another thread (B)

I mean if i have to diapcatch(include) a request from A->C then how it is possible to do same(for the same request) from B->C unless u do somthing like A->B then B->C.

Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
from a servlet instance(...) which serves the request .But how can a request meant for thread (Lets say A) be dispatched from another thread (B)

It is not dispatched to other threads, that's the point.

Your container creates a ServletRequest instance when it receives an HTTP request from outside, then forks a new thread (or picks one from a pool or whatever) and then runs the service(...) of the Servlet instance with this ServletRequest instance (and ServletResponse) using the thread it just picked. Then, if you never fork the thread yourself, the container guaranties than whenever you forward/include the request, all the processing will happen in the same thread, that is, sequencially : you never have the servlet where you forwarded/included your request run in parallel of you current request.

It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
I agree. Here's the link:
subject: Request Dispatch Problem
Similar Threads
How Multiple Requests for a Servlet class are handled
Webservices Client
why Not encodeDispatchURL ????