Forums Register Login

Question 8 Sample Chapter 4

+Pie Number of slices to send: Send
Hi all,
In the Sample Chapter of Professional SCWCD have a question (#8):
What is the result if the following servlet's doPost() method is called? Assume the response has been commited as a result the writer.flush() method. Select one correct answer:
public class ServletY extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
PrintWriter writer = res.getWriter();
writer.println("Hello World");
writer.flush();
res.sensError(HttpServletResponse.SC_FORBIDDEN, "bang!");
}
}
a. A java.lang.IllegalStateException is thrown
b. A NullPointerException is thrown
c. The browser displays SC?_FORBIDDEN error page

I Think and have gotten none above, i'v gotten "Hello Word".
What is the correct answer?
+Pie Number of slices to send: Send
there exists an exception thrown, waiting to be caught by you
regards,
+Pie Number of slices to send: Send
firstly,hello world is output ,
then throw a exception .
right?
+Pie Number of slices to send: Send
hello world is what your browser gets.
a server side exception , after the output having been committed, cannot be displayed on the browser; but on the System.out.println()
+Pie Number of slices to send: Send
Thanks all,
Now i understand what happened.
Best Regards
+Pie Number of slices to send: Send
Yo genius,
What do you mean when saying " a server side exception , after the output having been committed, cannot be displayed on the browser; but on the System.out.println() "??
I tried the code ,didnt give me ANY exception ANYWHERE not in the browser & not on the "System.out.println()" either.
And anyway as the default buffer is 8k & "hello world " is less than 8k,you wont get any Exception.
Any diference of opinion,ranchers?
+Pie Number of slices to send: Send
public class ServletY extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
PrintWriter writer = res.getWriter();
writer.println("Hello World");
writer.flush();
try{
res.sensError(HttpServletResponse.SC_FORBIDDEN, "bang!");
} catch (Exception e){System.out.println("here is "+e);
}
}//end doPost()
}//end Servlet
+Pie Number of slices to send: Send
by the way
sendError
public void sendError(int sc)
throws java.io.IOException
Sends an error response to the client using the specified status. The server generally creates the response to look like a normal server error page.
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.
Parameters:
sc - the error status codeThrows:
java.io.IOException - If an input or output exception occursjava.lang.IllegalStateException - If the response was committed

cheers,
c.
+Pie Number of slices to send: Send
if "writer.flush();" is deleted from this program, there is no output "hello world" because uncommitted output in the response buffer is automatically cleared . wrong or right? please correct me if wrong!
+Pie Number of slices to send: Send
There is an IllegalStateException because you're trying to add something to the headers after you commited the response. Deleting the flush() should erase the problem.
Hope it helps
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 847 times.
Similar Threads
ques from j2eecertificate.com
Possible error in question from Kathy Sierray, Bert Bates SCJP6 book
answer to question no8 of sample questions is wrong
Typo in Mcgraw-Hill SCJP 5 Book?
Class using Main without declaring itself Public
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:12:47.