• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear . Need more suggestions and views.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Need more suggestions and views.


What for? Why don't you just run the code and see what happens?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic