Guys, i took the enthuware JWebplus v4
test today. I had a query regarding the same. I'm posting it here and i hope i ain't violating any copyright laws.
Que:
You want the copyright information contained in <webapp root>/html/copyright.html to be added to the html content that your
servlet generates. Which of the following code fragments occuring in doGet method would achieve this objective?
Select 1 correct option.
a. this.getServletContext().getRequestDispatcher("../html/copyright.html").include(request, response);
b. this.getServletContext().getRequestDispatcher("/html/copyright.html").include(request, response);
c. request.getRequestDispatcher("../html/copyright.html").include(request, response);
d. request.include("../html/copyright.html", request, response);
e. this.getServletContext().getRequestDispatcher("/html/copyright.html").forward(request, response);
f. request.getRequestDispatcher("/html/copyright.html").forward(request, response);
Ans: c
My ans: b
Their comments: 1. This is invalid because a relative path is being passed to getRequestDispatcher method of ServletContext. 2. This is valid because an absolute path is being passed to getRequestDispatcher method of ServletContext. (/html/copyright.html starts with a "/"). 3. This is valid because getRequestDispatcher() of ServletRequest interface supports absolute as well as relative paths. 4. There is no include method in request. 5 and 6. calling forward will forward the request to the given resource and so the data generated by this servlet will be lost.