• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets - Request - Threads question?

 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a non-distributed web-app, I have two servlets ServletOne and ServletTwo. For simplicity, lets say ServletOne forwards the request to ServletTwo.
Both of these don't implement SingleThreadModel.
From a browser, I access ServletOne, it forwards to ServletTwo and I am happy.
In this scenario, my request has spaned over TWO threads.
Am I right?
One thread through the service() method of ServletOne and the other thread through the service() method of ServletTwo.
Can someone (with the knowledge of Threads) testify this.
Thanks.
- satya
 
Gennady Shapiro
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incorrect. The spec states that forwards must be handled by the same thread. So no matter how many consequent forwards (or includes) you make they all will be handled synchronously by one thread.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The spec states that forwards must be handled by the same thread
Okay, I was able to locate this sentence in SRV.8.2 which is for RequestDispatcher. But then what about the fact that each call to service method of a Servlet (each request) must be handled in its own thread.
Seems like when we forward the request and response to another Servlet, it must be treated differently as opposed to a request from a client browser.
Would you agree to this then?
Thanks.
- satya
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same thread. In each servlet, do:
Thread t = Thread.currentThread();
System.out.println( t.getName() );
Regards, Guy
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Guy.
However, why doesn it always show me (on my system) the foll. output........

Current Thread = Thread[HttpProcessor[8080][4],5,main]
Current Thread = Thread[HttpProcessor[8080][4],5,main]
Session Destroyed in TestingSessionsFri Mar 01 15:32:50 EST 2002
Session Created in TestingSessionsFri Mar 01 15:33:06 EST 2002
Current Thread = Thread[HttpProcessor[8080][4],5,main]
Current Thread = Thread[HttpProcessor[8080][4],5,main]
attributeValue = abcd
attributeValue = abcd
Current Thread = Thread[HttpProcessor[8080][4],5,main]

Ignore the attribute stuff......
See that the Thread is always 4, 5 and main.
Even after I restarted Tomcat.
I used:

Thread t = Thread.currentThread();
System.out.println( "Current Thread = " + t );

Just curious............
- satya
ps:
Din't someone say "The curiosity killed the cat!"
 
Gennady Shapiro
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav, you have to realize that its not every service call must be hadled by its own thread but every user request must be handles by its own thread, no matter how many service()s it hits along the way.
When ServletA forwards to ServletB in java code it looks just like any other method call:
ServletADispatcher.forward(urlB) -- the processing thread proceeds to ServrletB, does its business and returns to ServletA. So evrything happens within the same thread.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree re the thread stuff. Makes sense, since we don't create a new request/response its probably like anyother method call.
Din't think of it that way, till you pointed out SRV.8.3.
ServletADispatcher.forward(urlB) -- the processing thread proceeds to ServrletB, does its business and returns to ServletA
forward doesn't return to ServletA....just clarifying.
- satya
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be i am thinking wrong.....But i have another logic to prove same thing...

JSP provides an implicit object called reuqest object.Its scope is define as request scope(Means where ever request will proceed it will be attached with the client's request thread).Now consider a JSP let say A,which forwords a request to JSP B.The implicit same request object is available in both JSP's.That is, there must be one thread having lock of same request object due to which same request object is available in both JSP's
May be i failed to define...
Bye.
Viki.
[ March 01, 2002: Message edited by: Vikrama Sanjeeva ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then how about redirection of the request???
from above discussion i 've a very weired question. sound funny. i've not tried it. but here it goes,
can i create and object of a servlet and then call doGet() method which i made public while extending the HttpServlet class??? (of course i must provide valid request and response objects)
regards
maulin.
 
Gennady Shapiro
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forward doesn't return to ServletA....just clarifying.
you saying a java function never returns? where does it go?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you saying a java function never returns? where does it go?
I am saying when you use forward(...) method of the RequestDispatcher, it will NOT return to the calling Servlet/JSP.
Hope I am clear.
- satya
 
Gennady Shapiro
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am saying when you use forward(...) method of the RequestDispatcher, it will NOT return to the calling Servlet/JSP.
So you saying if you call myRequestDispatcher.forward(url) from service, the control does not return to the calling servlet? So you saying you can call a java function and not expect it to come back? How does java allow that?
I bet you it comes back.
Moreover, I bet you can do one forward after another and it'll still work correctly.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently it does come back.
Make a couple of simple servlets like such:

And your browser should return the following:
In TestServlet
In Test2Servlet
In TestServlet
I think what you might be thinking of is using jsp:forward, which does not return back to the forwarding page.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gennady:

So you saying if you call myRequestDispatcher.forward(url) from service, the control does not return to the calling servlet? So you saying you can call a java function and not expect it to come back? How does java allow that?
I bet you it comes back.

OOPS....
Thanks for the argument. Yes, it does come back. After all its a java program....Duh! what was I thinking.

Moreover, I bet you can do one forward after another and it'll still work correctly.

Now this is a stretch.......
I get the following error when I forward to Test3Servlet after "returning" from a forward to Test2Servlet.
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:159)
at madhav.misc.Test3Servlet.doGet(Test3Servlet.java:14)

The reason is as explained in SRV.8.4.

Before the forward method of the RequestDispatcher interfae returns, the response content must be sent and committed, and closed by the servlet container.

Thanks for the argument, I learnt something new.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jason:
Thanks for the code.

And your browser should return the following:
In TestServlet
In Test2Servlet
In TestServlet

FYI.......
There is an error in the above response....
The output you mentioned is wrong. Yes, the control returns, but you can't add anything else to the response object. Please see my reply to Gennady above.


I think what you might be thinking of is using jsp:forward, which does not return back to the forwarding page

Well, yes I was getting confused a little here.
But, the fact that JSP code translates to a servlet and then we are at the start again. I would assume even the JSP code would follow the same process that I now understand regarding the Servlets. I haven't tried this yet.
So, in conclusion, I would say "Yes the control returns to the calling servlet but the response object is flushed, committed and closed. Hence no further content can be sent to the client from the calling servlet or from another forwarded servlet".
Furthermore, if you try to forward a request more than once, the second attempt would thorw an IllegalStateException since the response is already committed.
Thanks.
- satya
[ March 03, 2002: Message edited by: Madhav Lakkapragada ]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note, it is valid to call getWriter() more than once on a response object.
Its a surprise to me......... :roll:
Really shocked!!!
- satya
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:
jason:
Thanks for the code.

And your browser should return the following:
In TestServlet
In Test2Servlet
In TestServlet

FYI.......
There is an error in the above response....
The output you mentioned is wrong. Yes, the control returns, but you can't add anything else to the response object. Please see my reply to Gennady above.
[ March 03, 2002: Message edited by: Madhav Lakkapragada ]

Have you tried to actually run the code? I don't get the error you are stating, I get the response that I posted. Apparently, the response is flushed and closed when Test2Servlet is ended, allowing TestServlet to print out a new message. This perhaps ties in with the multiple calls to getWriter(), allowing me to print a response <b>after</b> returning to the forwarding servlet.
In regards to JSP, I'm not sure how that works. I know that it is just turned into a servlet before running, but if you make a JSP that does <jsp:forward...>, put some output in the JSP that was forwarded to, and then put some ouput in the forwarding JSP after the <jsp:forward...>, it will not be shown. Apparently, the translation from JSP to servlet uses a different mechanism for forwarding. If anyone knows the particulars, would love to see a post about it (so we don't go on a tangent and get off the initial intent of this thread).
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you tried to actually run the code?

Yes, I did - first thing I did today morning. Earlier I tried it with one getWriter() method call. Now I tried it with the exact code you posted.
I am running with Tomcat 4.0.1 and I get IllegalStateException on the second getWriter() line of the TestServlet.java code.


java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:159)
at TestServlet.doGet(TestServlet.java:15)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)


I don't get the error you are stating, I get the response that I posted. Apparently, the response is flushed and closed when Test2Servlet is ended, allowing TestServlet to print out a new message. This perhaps ties in with the multiple calls to getWriter(), allowing me to print a response <b>after</b> returning to the forwarding servlet.
It would be interesting to know what server you are running with and on what OS?
Thanks.
- satya
ps:
// added this comment so the line number is not lost
I have two blank lines in the code (one before and after the commented package line) so in this post I added two comments.
[ March 03, 2002: Message edited by: Madhav Lakkapragada ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh! Forgot I was running Orion at home and Tomcat at work. I haven't tried it yet on Tomcat, and since that is the reference platform, I think I would go with your behavior over what Orion provides. I'll give it a go at work tomorrow, and most likely will get the same results you have
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I get the same exception. It appears it's server dependent on what behavior to expect. I knew there were some things that were server dependent, just didn't think this would be one of them! Live and learn...
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yup, I get the same exception. It appears it's server dependent on what behavior to expect

Finally, atleast someone agrees with me.
Thanks for verifying, jason.
Now that you have concluded this, I was wondering if you could comment on the other thread re this. I appreciate your help.
regds.
- satya
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:
On a side note, it is valid to call getWriter() more than once on a response object.

y not? it returns the same object ref. try to do sth like,
PrintWriter pw1 = response.getWriter();
PrintWriter pw2 = response.getWriter();
if ( pw1.equals(pw2) )
S.o.p("equals");
else
S.o.p("not-equal");
i tried it and it says "equals".
regards
maulin.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maulin Vasavada:

y not? it returns the same object ref. try to do sth like,
PrintWriter pw1 = response.getWriter();
PrintWriter pw2 = response.getWriter();
if ( pw1.equals(pw2) )
S.o.p("equals");
else
S.o.p("not-equal");
i tried it and it says "equals".
regards
maulin.

So what's the difference between your findings and my stmt again....
- satya
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no difference. i wrote it 'coz u said u were surprised to find that so i wondered that if something seems logical should work and tried to run it...
regards
maulin.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jason adam:
Doh! Forgot I was running Orion at home and Tomcat at work. I haven't tried it yet on Tomcat, and since that is the reference platform, I think I would go with your behavior over what Orion provides. I'll give it a go at work tomorrow, and most likely will get the same results you have

The ISE from Tomcat is 'per spec'. See SRV 8.4, first paragraph, last sentence.
Regards, Guy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic