This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What will happen when you attempt to compile and run the following code

 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:What will happen when you attempt to compile and run the following code (assuming menu.jsp is available)?


package com.examulator;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ReqD extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
RequestDispatcher dis = sc.getRequestDispatcher("menu.jsp");
if (dis != null){
dis.include(request, response);
}
PrintWriter out = response.getWriter();
out.print("Output after menu.jsp");
}
}


Choose at least one answer.

a. Compilation error, the this object is not available within doGet


b. Compilation and output of the contents of menu.jsp followed by "output after menu.jsp"


c. Compilation and output of the contents of menu.jsp only


d. Compilation but error at runtime.




RequestDispatcher dis = sc.getRequestDispatcher("menu.jsp");
I think here dis has value null.so it does not satisfy the if condition so it execute the further code and get a object of PrintWriter and output the message "Output after menu.jsp".


Source : Marcus 2 exam
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think answer is d.
since ServletContext.getRequestDispacher("menu.jsp") should start with "/menu.jsp"

Regards
Sravanthi
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijaya Sravanthi

i think answer is d.
since ServletContext.getRequestDispacher("menu.jsp") should start with "/menu.jsp"


Here menu.jsp in RequestDispatcher method is not start with "/" so this method returns null and so dis has null value.I think no error generate.Please correct me if i am wrong.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijaya Sravanthi:
[QB]i think answer is d.
since ServletContext.getRequestDispacher("menu.jsp") should start with "/menu.jsp"

i agree vijaya. if it is

request.getRequestDispacher("menu.jsp") ..

then it will run without any problem
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear . Need more suggestions and views.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need more suggestions and views.

What for? Why don't you just run the code and see what happens?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic