File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question 8 Sample Chapter 4 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 "Question 8 Sample Chapter 4" Watch "Question 8 Sample Chapter 4" New topic
Author

Question 8 Sample Chapter 4

Isaias C. Barroso
Ranch Hand

Joined: Jan 22, 2002
Posts: 165
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?


Isaias
Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
there exists an exception thrown, waiting to be caught by you
regards,
Timber Lee
Ranch Hand

Joined: Oct 14, 2002
Posts: 157
firstly,hello world is output ,
then throw a exception .
right?


SCJP<br />SCWCD<br />SCEA
Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
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

Joined: Jan 22, 2002
Posts: 165
Thanks all,
Now i understand what happened.
Best Regards
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
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

Joined: Nov 15, 2001
Posts: 132
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

Joined: Nov 15, 2001
Posts: 132
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

Joined: Oct 14, 2002
Posts: 157
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!
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
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


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question 8 Sample Chapter 4