aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendRedirect ( qsn from Study kit) 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 "sendRedirect ( qsn from Study kit)" Watch "sendRedirect ( qsn from Study kit)" New topic
Author

sendRedirect ( qsn from Study kit)

Mala R
Greenhorn

Joined: Feb 18, 2003
Posts: 3
This is a direct qsn from Study kit in Chap 7, Qs no 2:
----------------------
Consider the following doPost() method of a servlet:
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Inside doPost");
PrintWriter out = response.getWriter();
out.println("Hello, ");
String name = getNameFromDBSomeHow();
if(name == null)
{
response.sendError(HttpServletResponse.SC_NOT_FOUND,
"Unable to get name.");
}
out.println(name);
}
Assuming that getNameFromDBSomeHow() returns null, which of the following
statements regarding this code are correct? (Select one)
a It will throw an InvalidStateException while serving a request.
b It will throw a ServletException while serving a request.
c It will throw a NullPointerException while serving a request.
d It will throw an IllegalStateException while serving a request.
e It will not throw an exception.
Ans : d
-----------------------
But as per the servlet specfns,
If data is written to
the response after these methods are called, the data is ignored.
So Shudn't teh correct answer be option : e
Thanks
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

check out the API for HttpServletResponse.sendError
it says: "If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to."
so D is the correct answer


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
This was discussed just a few days ago here.
I agree that e is the correct answer: if sendError() is called after the response has been commited an IllegalStateException will be thrown, but this does not happen in this example as the response has not been committed; if you write to the response after calling sendError() then the data will be ignored and no exception is thrown.
Mark
[ April 02, 2003: Message edited by: Mark Bensing ]
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Is it true that it will throw an IllegalStateException because there are statement out.println(name) ?
daniel
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
What happens if response is committed in getNameFromDBSomeHow()?
Can't it throw java.lang.IllegalStateException ?
once response is commited, output stream will get closed. so it won't get output stream again to send error message to the client. so it will throw java.lang.IllegalStateException.
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
The response can't be committed in getNameFromDBSomeHow() in the above example because the HttpServletResponse is not passed to it as an argument - it has no way to access the response.
However if the code was changed to commit the response before calling response.sendError() [for example, if out.flush() was added right after out.println("Hello") or if you changed getNameFromDBSomeHow() to commit the response], then response.sendError() would throw an IllegalStateException.
Mark
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Perhaps I'm missing something but I still don't see why d is not correct. The book says that d is correct because:
"When the sendError() method is called, the response is assumed to be committed. If you write anything to the response, after it gets committed, an IllegalStateException is thrown. In this case, sendError() is called if the name is null. This commits the response. However, after the if condition, we have out.println(name), which will cause the IllegalStateException."

So the book clearly says that writing to response after using sendError will create an Exception. But my reading of some of the above answers indicates that they think otherwise.
Can anyone tell me who's right?
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
1)This method will have the side effect of committing the response ,if it has not already been committed and,TERMINATING it.
After out.println.("Hello")response is not committed, you can check it by adding if(response.isCommitted()){
System.out.println("yes ");
}else{
System.out.println("no"); }
2)if you explicitly commit the response,after o.p("hello") by using res.flushBuffer(),data will be sent to the browser(thus exception occurs).Statements after this method is called are SIMPLY IGNORED ,so o.p.(name) is simply ignored.


<i>Dare to dream - everything that exists today,was once a figment of someone's imagination, nobody says tomorrow can't be a figment of your today.</i>
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
All I can do is echo the specification and Jessica's quote of it above:
"After using this method, the response should be considered to be committed and should not be written to."
So, once response.sendError() is called, the response IS committed and any further writing, as in "out.println(name)" causes a IllegalStateException error.
So the book is right. D is the answer.
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
It is much easier to get hold of a concept by exprimenting.
please try this code
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("please");
if(response.isCommitted()){System.out.println("yes");
}else{
System.out.println("not committed");
}
try{
String user =null;
if(user == null){
response.sendError(404,"NO exception,Just an error message from Amer");
} out.println(user);
}catch(IllegalStateException e){ System.out.println("EXCEPTION OCCURED IN SENDERROR");}
out.println("end get");
}
I HOPE THIS HELPS
ANSWER IS E
[ April 14, 2003: Message edited by: Amer Khan ]
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
Dear Ken
important thing here is the statement before sendError() is called[the out.println].Is the response commited or not???the answer is NO,(unless the buffer is full and flush() is called automatically) no exception will be thrown. As far as the statement AFTER sendError() is concerned ,if you write to the response then the data will be SIMPLY IGNORED and no exception is thrown.
Just consentrate on the data before sendError() is called
[ April 14, 2003: Message edited by: Amer Khan ]
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
Now there will be an exception
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("No Worries ");
/*if we don't flush, the response is not committed,if
the response is not commiited this method will commit the response and terminate it (no exception).By flushing, the response
is commited and it will throw IllagalStateException*/
response.flushBuffer();
if(response.isCommitted()){System.out.println("yes ");
}else{
System.out.println("not committed");
}
try{
String user =null;
if(user == null){
response.sendError(404,"SORRY ==========");
} out.println(user);
}catch(IllegalStateException e){ System.out.println("hhhhh");}
out.println("end get");
}
I hope it is clear now
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
Ken,
From the servlet specification SRV5.3:

These methods {referring to sendRedirect() and sendError()} 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.

The quote you had in your reply "After using this method, the response should be considered to be committed and should not be written to." comes from the API documentation for sendError(). Notice that the API documentation also says that an IllegalStateException is thrown "If the response was committed before this method call" It says nothing about an IllegalStateException being thrown if you write to the reponse after calling sendError(). If you do write to the response after sendError(), the data should be ignored as per the specification.
Mark
[ April 11, 2003: Message edited by: Mark Bensing ]
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Mark, Amer,
As I was driving home from work after posting my last message about D it hit me that the spec said pretty much what you have said, the response should be considered committed after using HttpServletResponse.sendError, but also that an IllegalStateException should be thrown if the response was committed before using HttpServletResponse.sendError.
Since it wasn't committed the spec doesn't say anything about IllegalStateException. It just says it should be considered committed once HttpServletResponse.sendError is used. Elsewhere it says it's ignored as Mark noted.
So now I see why the question was raised to begin with and why you've chosen E over D. Thanks for your responses. It took awhile for me to finally get it clear in my head.
I wonder if anyone has posted an errata for the book or if the authors agree one is needed?
Mark Bensing
Ranch Hand

Joined: Mar 09, 2003
Posts: 40
I did post this issue to Manning's SCWCD Exam Study Kit forum over the weekend but there have been no comments from the authors yet.
Mark
Hanumant Deshmukh
Author
Greenhorn

Joined: May 16, 2002
Posts: 27
I think the concern about the correctness of option D is valid. As many of you have mentioned above, the specification does say that if the data is written after this method is called, it is ignored. So option e should be right.
We will make this correction in the errata.
thank you for your continued support and feedback.


Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1930110596/jdiscuss-20" target="_blank" rel="nofollow">SCWCD Exam Study Kit</a>
Sarah Giles
Greenhorn

Joined: Apr 11, 2003
Posts: 3
Thanks Guys
finally i understand sendError();
Amer's examples are great.
check RequestDispatcher.forward
[ April 17, 2003: Message edited by: Sarah Giles ]
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Thanks Hanumant. I'm sure future readers will be happy not to spend so much time on this. On the other hand it's probably been educational for anyone who did partake in discussion.
Ken
Forrest Xu
Ranch Hand

Joined: Nov 16, 2002
Posts: 92
I think the option should be c!!!
c It will throw a NullPointerException while serving a request.
out.println(name)will throw a NullPointerException when name is null?
Forrest Xu
Ranch Hand

Joined: Nov 16, 2002
Posts: 92
any feedback?
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Frank,
1. sendError() has the side effect to commit the response, write to out will be ignored.
2. out.println(name) will not throw a NPE if name is null
please try the code, it does not do much, but NO NPE either.

[ May 17, 2003: Message edited by: chi Lin ]
[ May 17, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Ross Lamont
Greenhorn

Joined: May 21, 2003
Posts: 3
I think that none of the options are correct, at least in the context of the Java exam. The problem is that the IllegalStateException _may_ be thrown as a result of having put some stuff in the buffer prior to the response.sendError call. On the face of it, this seems very unlikely, because the default buffer is large, but everybody is making large assumptions about what has gone on before this code is called.
SRV5.1 says: "A servlet container is allowed, but not required, to buffer output going to the client for efficiency purposes..."
Everyone is assuming there is a buffer, but the Spec makes no guarantee to their being a buffer, so you can't be certain that it _will_ not throw an exception - and from an educational point of view - and a good practices point of view - surely the best choice amongst bad choices would be "d".
Cheers
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
quote: Ross
"Everyone is assuming there is a buffer, but the Spec makes no guarantee to their being a buffer, so you can't be certain that it _will_ not throw an exception - and from an educational point of view - and a good practices point of view - surely the best choice amongst bad choices would be "d"."

If the bufferSize is set to 0(buffering is off)
and something is wrritten to the response before calling sendError().YES the response will be considered committed and YES an exception will be thrown.(response.setBufferSize(0))
(99% of the container's use buffering by default to improve performance)
As far as the data written to the response after calling sendError is concerned "IT WILL BE IGNORED"
EXCEPTION occurs ONLY if response is committed BEFORE calling this method.it's got NOTHING to do with the data written to the response after this method is called.
The example in the mock is a bit distracting
To better understand it concentrate on these 2 points.
1, null is returned ONLY for making sure that sendError is called.
2.statement after sendError is called,is just to clear the point that anything written to the response after a call to sendError , will be IGNORED(exception or no exception)
The important thing to understand ,is the response committed or not committed, before sendError is called.
If buffering is used and, for example the buffer is half full this method will commit and TERMINATE the response(means now the buffer will be empty),thus no exception thrown.
[To understand buffering ,read the API about buffers,for example what happen's when the buffer is full"it should be flushed automatically"]
So if the buffer is full ,it will be flushed automatically,and then if we call sendError it will throw an exception.(sendError won't have a chance to clear or terminate the response ,because the buffer is already flushed
meaning the response is committed)
I hope this helps
"Don't worry, be Happy"
Amer
[ May 21, 2003: Message edited by: Amer Khan ]
Ross Lamont
Greenhorn

Joined: May 21, 2003
Posts: 3
Well I completely agree with you about what happens after the call to sendError, my difficulty as an exam taker is what to do when I come across the line:
out.println("Hello, ");
before the attempt to sendError. If I look at this with the knowledge that the spec strictly doesn't require buffering then I would assume that this is a very difficult question designed to test that knowledge. In which case none of the answers is correct but working on the basis of writing code that will work 100% of the time, now and in the future, I would pick "d" because of that reason, and because "e" implies that an exception will _never_ be thrown.
However if I look at the world in terms of practical realities, then "e" is true 99% of the time.
But as I said in a post on the Enthuware site, the test is really about knowledge of the spec, not the real world, and I tend to think thats reflected in a few of the questions.
So, what's a test taker to do? Take the spec as gospel or draw on practical experience? I don't have an answer to that...
Amer Khan
Ranch Hand

Joined: Apr 05, 2003
Posts: 163
quote: Ross
"So, what's a test taker to do? Take the spec as gospel or draw on practical experience? I don't have an answer to that... "
Please pick e.
NO WoRRIES
picking d.
WORRIES
'Dont worry,Be Happy'
Amer
[ May 21, 2003: Message edited by: Amer Khan ]
Ross Lamont
Greenhorn

Joined: May 21, 2003
Posts: 3
OK, on the basis that e is right 99% of the time and d is right only 1% of the time.
But just remember, 1% of the bugs cost 99% of the dollars <exaggeration>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendRedirect ( qsn from Study kit)
 
Similar Threads
sendError
response commitment
doubt in Exam Kit Book
some questions on J2EE test
page gets hang on runtime