aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes some confusions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "some confusions" Watch "some confusions" New topic
Author

some confusions

geet rahane
Ranch Hand

Joined: Jul 06, 2009
Posts: 45
Hi to all...

I have some confusions as follows.....

1.what happens if we try to write some text (out.println("hello")) after requestDispatcher.forward()...
whether exception get thrown or just the output of forwarded resource get displayed??

2.what happens if we try to write some text (out.println("hello")) after response.sendRedirect()...
whether exception get thrown or the resource get redirected succesfully??

3.If there is tag handler class which extends TagSupport not BodyTagSupport and whose doStartTag
method returns EVAL_BODY_BUFFERED...then is that code succesfully compiled or exception get thrown???

4.During forward on ServletContext if we had used relative path in requestDispatcher and then do forward on that object...what happens in this case....

5.Is */* a valid url pattern for servlet mapping?


please help me.......

thanks in advance
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Did you try any of this yourself. I think the best way to find out the answer is to try this yourself, that way you'll get the answer and get some practice at the same time...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
geet rahane
Ranch Hand

Joined: Jul 06, 2009
Posts: 45
yor are right .......that i should have to try this..i will try it...but due to some problems in my pc ,it is not possible to try them now..so now please help me...........
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

geet rahane wrote:1.what happens if we try to write some text (out.println("hello")) after requestDispatcher.forward()...
whether exception get thrown or just the output of forwarded resource get displayed??

2.what happens if we try to write some text (out.println("hello")) after response.sendRedirect()...
whether exception get thrown or the resource get redirected succesfully??


I think in both the cases, the client will get normal response (the forwarded content and redirected page) and internally an exception will also be thrown on the server but the client will have no knowledge about it.

4.During forward on ServletContext if we had used relative path in requestDispatcher and then do forward on that object...what happens in this case....


You'll get an IllegalArgumentException at runtime.

5.Is */* a valid url pattern for servlet mapping?


No, the mapping URL must start with /

(Sorry I skipped the custom tag question, I'm not good at it )
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Geet.
3.If there is tag handler class which extends TagSupport not BodyTagSupport and whose doStartTag
method returns EVAL_BODY_BUFFERED...then is that code succesfully compiled or exception get thrown???
It will not compile.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Ankit.
I think in both the cases, the client will get normal response (the forwarded content and redirected page) and internally an exception will also be thrown on the server but the client will have no knowledge about it.
After forward if we use like if exception occurs internally at first statement, the second will not be executed.
Here is executing. Can you please explain internal working? Thankyou.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

As far as I know, there should have been an exception as given here. I tried it and it doesn't give any exception in Tomcat or Glassfish. Maybe Christophe can throw some light ...
geet rahane
Ranch Hand

Joined: Jul 06, 2009
Posts: 45
Thanks a lot !!!
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Thanks for the reply.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



why would that throw IllegalStateException exception ? It is thrown when you flush the buffer and then call response.sendDirect() or request dispatcher. There is no behavior to say otherwise as Printwriter.println() does not throw IllegalStateException .
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Hi Balu, thanks for the reply. What happens when the container sees after forward? Can you please explain? Thankyou.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


what it does to the out buffer that exists before calling forward ? it ignores the entire buffer, similarly it ignores after that.
Chinmaya Chowdary
Ranch Hand

Joined: Apr 21, 2008
Posts: 432
Thankyou.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

what it does to the out buffer that exists before calling forward ? it ignores the entire buffer, similarly it ignores after that.


More specifically, it clears the buffer before the forward, the other part is correct.

Now actually when the call to forward returns, the PrintWriter is already closed. Additional calls to out.println() result in nothing. This is written in the documentation of PrintWriter class

Methods in this class never throw I/O exceptions, although some of its constructors may.


So if you try to call getOutputStream() after a forward, you'll get an exception because the forwarded JSP would've already called getWriter() on the response (If you forward the request to a Servlet which retrieves the OutputStream, then calling getWriter after the forward will result in an exception). I even tried something like this which proves what Balu said (it was unexpected for me, I expected IOException till I saw the docs )

 
jQuery in Action, 2nd edition
 
subject: some confusions