aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes What will happen when you attempt to compile and run the following code 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 "What will happen when you attempt to compile and run the following code " Watch "What will happen when you attempt to compile and run the following code " New topic
Author

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

pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
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


SCJP 5.0(75%), SCWCD 5.0(88%)
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
i think answer is d.
since ServletContext.getRequestDispacher("menu.jsp") should start with "/menu.jsp"

Regards
Sravanthi
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 23, 2007
Posts: 339
I am not clear . Need more suggestions and views.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41509
    
  53
Need more suggestions and views.

What for? Why don't you just run the code and see what happens?


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will happen when you attempt to compile and run the following code