OCJP-90%,OCPWCD-95%
Roel De Nijs wrote:You should answer: "Bad design! Flow control logic should never be in a jsp, but in a servlet."
OCJP-90%,OCPWCD-95%
SCJP
varun Dosapati wrote:Sagar
Are you trying to access directly the jsp.
Thanks
Varun Dosapati
OCJP-90%,OCPWCD-95%
1) If you use a RequestDispatcher, the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute(). With a sendRedirect(), it is a new request from the client, and the only way to pass data is through the session or with web parameters (url?name=value).
2) A sendRedirect() also updates the browser history. Suppose you have JSP-1 which has a form that targets Servlet-2, which then redirects to JSP-3. With a redirect, the user's address bar will read "http://[host]/JSP-3". If the user clicks the Reload/Refresh button, only JSP-3 will be re-executed, not Servlet-2.
If you use a RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read "http://[host]/Servlet-2". A reload/refresh will execute both Servlet-2 and JSP-3. This can be important if Servlet-2 performs some system update (such as credit-card processing).
OCJP-90%,OCPWCD-95%
Confusion 2:
If we are allowed to do this then why is it mentioned in Head First Servlets on pg.no 137 that once committed too late rule applies also to setting headers,cookies,status codes,content type and so on.
See this link: http://oreilly.com/catalog/errata.csp?isbn=9780596005405
So I you need accurate information go through the Servlet/JSP specification.
OCJP-90%,OCPWCD-95%
OCJP-90%,OCPWCD-95%
5.3 Convenience Methods
sendRedirect sendError ...
These methods will have the side effect of committing the response, if it has not already been committed, and terminating it. No further output to the client should be made by the servlet after these methods are called. If data is written to the response after these methods are called, the data is ignored.
9.4 The Forward Method
The forward method of the RequestDispatcher interface may be called by the calling servlet only when no output has been committed to the client. If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet's service method is called. If the response has been committed, an IllegalStateException must be thrown.
Frits Walraven wrote:
9.4 The Forward Method
If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet's service method is called. If the response has been committed, an IllegalStateException must be thrown.
Regards,
Frits
OCJP-90%,OCPWCD-95%
then why an exception ?
OCJP-90%,OCPWCD-95%
this means that it is simply not possible to do a forward from a JSP ? then we can never use a JSP standard forward action?
OR is there any mechanism to avoid the exception ?
OCJP-90%,OCPWCD-95%
Does this explanation make it any clearer?
OCJP-90%,OCPWCD-95%
Have you tried and have you got any exception ? Any idea about why i am not getting one ?
OCJP-90%,OCPWCD-95%
OCJP-90%,OCPWCD-95%
Sagar Shroff wrote:Hello ranchers,when i noticed that forward() inside my jsp , throws IllegalStateException with this code.
java.lang.IllegalStateException: getOutputStream() has already been called for this response
So i tired the same with response.sendRedirect() which was not throwing an exception.
myjsp.jsp
This is all i have have written inside my jsp.So now when i invoke this jsp it successfully invokes the html page ? instead of exception.
Can someone explain why is it was throwing the exception in the previous case (with request dispatcher)?
and why it is not throwing in this case ?
So if any question related to this comes in exam say suppose the above code will throw exception Or not ? what should i answer.??
Regards Sagar.
R. Jain wrote:
Sagar Shroff wrote:Hello ranchers,when i noticed that forward() inside my jsp , throws IllegalStateException with this code.
java.lang.IllegalStateException: getOutputStream() has already been called for this response
So i tired the same with response.sendRedirect() which was not throwing an exception.
myjsp.jsp
This is all i have have written inside my jsp.So now when i invoke this jsp it successfully invokes the html page ? instead of exception.
Can someone explain why is it was throwing the exception in the previous case (with request dispatcher)?
and why it is not throwing in this case ?
So if any question related to this comes in exam say suppose the above code will throw exception Or not ? what should i answer.??
Regards Sagar.
Update your code to add this : -
I think it should work...
R. Jain wrote:
But in my case both are working: -
OCJP-90%,OCPWCD-95%
Sagar Shroff wrote:
R. Jain wrote:
But in my case both are working: -
How come ? as frits mentioned that after forward from your jsp it tries to commit the response so it should throw one exception.
I guess you must be calling forward from your servlet.Try calling it from a jsp and then you'll get one.
R. Jain wrote:
Sagar Shroff wrote:
R. Jain wrote:
But in my case both are working: -
How come ? as frits mentioned that after forward from your jsp it tries to commit the response so it should throw one exception.
I guess you must be calling forward from your servlet.Try calling it from a jsp and then you'll get one.
I though that forward will trouble only when, you modify the response object before forwarding.... But still then it is working fine....
Now here I am myself confused...
R. Jain wrote:
I have added these codes to a JSP and it is working fine.... I though that forward will trouble only when, you modify the response object before forwarding.... But still then it is working fine....
Now here I am myself confused...
So, if anyone can explain, what exactly is the behaviour of this forward thing in JSP... it would be helpful for me...
OCJP-90%,OCPWCD-95%
Sagar Shroff wrote:it forward's the request to the html file.The response gets committed whenever you do a forward
After the html file completes its response the control comes back to this jsp,(at the end of the jsp) the container tries to commit the response.It's kindda auto commit which
i think container does to every jsp.So you should get an exception.
Regards,
Sagar.
R. Jain wrote:
I think what you are saying, happens in case of requestDispatcher.include() where you get the control back.... Not in forward....
OCJP-90%,OCPWCD-95%
Frits Walraven wrote:
then why an exception ?
The exception is thrown because you are forwarding away from this JSP, and after the forward is called (at the end of this JSP) the container is trying to commit the response (but is has already been committed by forwarding to the html file).
Regards,
Frits
OCJP-90%,OCPWCD-95%
Sagar Shroff wrote:I have read about it.And if you dont believe me look at what other's have to say.
Frits Walraven wrote:
then why an exception ?
The exception is thrown because you are forwarding away from this JSP, and after the forward is called (at the end of this JSP) the container is trying to commit the response (but is has already been committed by forwarding to the html file).
Regards,
Frits
Regards,
Sagar.
R. Jain wrote:
http://www.theserverside.com/discussions/thread.tss?thread_id=2205
See this link..
OCJP-90%,OCPWCD-95%
OCJP-90%,OCPWCD-95%
Why I am not getting any Exception as Sagar Shroff said with my JSP?
My second question, the rule is what ? we should get IllegalStateException (with sendRedirect and RequestDispatcher)because we did a forward after writing to the response or after committing the response ?
I try this code I get a blank page and not IllegalStateException, why ?
System.out.println("before flush: " + response.isCommitted());
Cookie c=new Cookie("mycoo","mycoo");
response.addCookie(c); // write to the response
response.setHeader("h","testh");
response.addHeader("hh","testhhh");
response.flushBuffer(); // commit the response
System.out.println("after flush: " + response.isCommitted());
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|