File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ Mock Exam question 45

 
Tyronne Fernando
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tyronne Fernando
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the main jsp code:

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

and the code in test2.jsp

<%
byte[] ba = {4,5,6};
response.getOutputStream().write(ba);
%>
 
Justin Rundle
Ranch Hand
Posts: 123
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic