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?
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()
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?
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
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!
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
Post by:autobot
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