File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ Mock Exam question 45 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 "HFSJ Mock Exam question 45" Watch "HFSJ Mock Exam question 45" New topic

HFSJ Mock Exam question 45

Tyronne Fernando
Ranch Hand

Joined: May 11, 2008
Posts: 50
This is the question:

Given this fragment from a valid doGet() method:
12. OutputStream os = response.getOutputStream();
13. byte[] ba = {1,2,3};
14. os.write(ba);
15. RequestDispatcher rd = request.RequestDispatcher("my.jsp");
16. rd.forward(request, response);
Assuming that "my.jsp" adds the bytes 4, 5, and 6 to the response, what is the result?

A. 123
B. 456
C. 123456
D. 456123
E. An exception is thrown

B is given as the correct answer.

I ran a simple test and I get an excpetion with the following message:

"org.apache.jasper.JasperException: getOutputStream() has already been called for this response"

I thinks E is correct. Any suggestions about this question? Thanks in advance.
cesar valencia
Ranch Hand

Joined: Oct 14, 2008
Posts: 33
456 is the correct answer if and only if the first servlet hasn't commited the answer (flushing the output). Would you mind posting your servlet in order to check if it's flushing or not?

scjp5, scwcd5
Tyronne Fernando
Ranch Hand

Joined: May 11, 2008
Posts: 50
This is the main jsp code:

byte[] ba = {1,2,3};
request.getRequestDispatcher("test2.jsp").forward(request, response);

and the code in test2.jsp

byte[] ba = {4,5,6};
Justin Rundle
Ranch Hand

Joined: Mar 26, 2008
Posts: 123

If you are going to replicate a question ensure you replicate it properly, more so the question is pertaining to the result when a doGet method of a servlet outputs an array of bytes and then redirects the request to a JSP.

Furthermore you have a JSP outputting code and then redirecting to another JSP.
I agree. Here's the link:
subject: HFSJ Mock Exam question 45
It's not a secret anymore!