Forums Register Login

sendRedirect ( qsn from Study kit)

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Is it true that it will throw an IllegalStateException because there are statement out.println(name) ?
daniel
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks Guys
finally i understand sendError();
Amer's examples are great.
check RequestDispatcher.forward
[ April 17, 2003: Message edited by: Sarah Giles ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
any feedback?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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>
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1117 times.
Similar Threads
sendError
doubt in Exam Kit Book
response commitment
some questions on J2EE test
page gets hang on runtime
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:34:07.