• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question 8 Sample Chapter 4

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there exists an exception thrown, waiting to be caught by you
regards,
 
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
firstly,hello world is output ,
then throw a exception .
right?
 
Brusk Baran
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Isaias C. Barroso
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all,
Now i understand what happened.
Best Regards
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Brusk Baran
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Brusk Baran
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Timber Lee
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic