Total Pageviews

Friday, 22 January 2016

J2EE interview Questions & Answers

THE SERVLET MODEL
Q.1 Which method in the HttpServlet class services the HTTP POST request? (Select one)
a. doPost(ServletRequest, ServletResponse)

b. doPOST(ServletRequest, ServletResponse)
c. servicePost(HttpServletRequest, HttpServletResponse)
d. doPost(HttpServletRequest, HttpServletResponse)
Answer: d
Explanation
Remember that HttpServlet extends GenericServlet and provides HTTPspecific functionality. Thus, all its methods take HttpServletRequest and HttpServletResponse objects as parameters.Also, the method names follow the standard Java naming convention—for example, the method for processing POST requests is doPost() and not doPOST().
Q.2 Consider the following HTML page code:
<html>
<body>

<a href=”/servlet/HelloServlet”>POST</a>
</body>
</html>
Which method of HelloServlet will be invoked when the hyperlink displayed by the above page is clicked? (Select one)
a. doGet
b. doPost
c. doForm
d. doHref

e. serviceGet
Answer: a
Explanation
Don’t get confused by the text POST displayed by the hyperlink. A click on a hyperlink always generates an HTTP GET request, which is handled by the doGet() method. You can generate a POST request through a hyperlink by using JavaScript. For example:
<html>

function sendPost() {
dummyform.submit();
}


<body>

<form name=”dummyform” action=”/servlet/HelloServlet” method=”POST”>
<input type=”text” name=”name”>
</form>
<a href=”” onClick=”javascript:sendPost();”>POST</a>

</body>
</html>

This HTML code executes the JavaScript function sendPost() whenever the hyperlink is clicked. This function submits the dummy form, causing a POST request to be sent.
Q.3 Consider the following code for the doGet() method:
public void doGet(HttpServletRequest req, HttpServletResponse res) {
    PrintWriter out = res.getWriter);
    out.println(“<html><body>Hello</body></html>”);
    //1
    if(req.getParameter(“name”) == null) {
         res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    }
}

Which of the following lines can be inserted at //1 so that the above code does 
not throw any exception? (Select one)

a. if ( ! res.isSent() )

b. if ( ! res.isCommitted() )
c. if ( ! res.isDone() )d. if ( ! res.isFlushed() )
e. if ( ! res.flush() )


Answer: b

Explanation
This question is based on the concept that the HttpServletResponse.send- Error() method throws an IllegalStateException if the response has already been sent to theclient. The ServletRequest.isCommitted() method checks whether or not the respons is committed.
Q.4 Which of the following lines would initialize the out variable for sending a Microsoft Word file to the browser? (Select one)
a. PrintWriter out = response.getServletOutput();
b. PrintWriter out = response.getPrintWriter();
c. OutputStream out = response.getWriter();
d. PrintWriter out = response.getOuputStream();
e. OutputStream out = response.getOuputStream();
f. ServletOutputStream out = response.getServletOutputStream();
Answer: e
Explanation
For sending any data other than text, you need to get the OutputStream object. ServletResponse.getOutputStream() returns an object of type Servlet-OutputStream, where ServletOutputStream extends OutputStream.
Q.5 You need to send a GIF file to the browser. Which of the following lines should be called after (or before) a call to response.getOutputStream()? (Select one)
a. response.setContentType(“image/gif”); Before
b. response.setContentType(“image/gif”); After
c. response.setDataType(“image/gif”); Before
d. response.setDataType(“image/gif”); After
e. response.setStreamType(“image/gif”); Before
f. response.setStreamType(“image/gif”); After
Answer: a
Explanation
You need to set the content type of the response using the ServletResponse. setContentType() method before calling the ServletResponse.getOutput-
Stream() method.
Q.6 Consider the following HTML page code:
<html>
<body>

    <form name=”data” action=”/servlet/DataServlet” method=”POST”>
      <input type=”text” name=”name”>
      <input type=”submit” name=”submit”>
    </form>
  </body>
</html>
Identify the two methods that can be used to retrieve the value of the name parameter when the form is submitted.
a. getParameter(“name”);
b. getParameterValue(“name”);
c. getParameterValues(“name”);
d. getParameters(“name”);
e. getValue(“name”);
f. getName();
Answers: a and c
ExplanationServletRequest provides two methods to retrieve input parameters:
• getParameter(“name”): Returns a String or null.
• getParameterValues(“name”): Returns a String array containing all the values for the name parameter or null.
Besides these two, ServletRequest also provides a getParameterNames() method that returns an Enumeration object of all the parameter names present in the request, or an empty Enumeration if the request does not contain any parameter.
Q.7 Which of the following methods would you use to retrieve header values from a request? (Select two)
a. getHeader() of ServletRequest
b. getHeaderValue() of ServletRequest
c. getHeader() of HttpServletRequest
d. getHeaders() of ServletRequest
e. getHeaders() of HttpServletRequest
Answers: b and e
Explanation
Headers are a feature of the HTTP protocol. Thus, all the header-specific methods belong to HttpServletRequest. getHeader() returns a String (or null), while getHeaders() returns an Enumeration of all the values for that header (or an empty Enumeration).
Q.8 Consider the following code:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
if(req.getParameter(“switch”) == null) {
    //1
} else  {//other code}
}
Which of the following lines can be inserted at //1 so that the request is redirected to the collectinfo.html page? (Select one)
a. req.sendRedirect(“collectinfo.html”);
b. req.redirect(“collectinfo.html”);
c. res.direct(“collectinfo.html”);
d. res.sendRedirect(“collectinfo.html”);
e. this.sendRedirect(“collectinfo.html”);
f. this.send(“collectinfo.html”);
Answer: d
Explanation
You can redirect the client to another resource using the HttpServletResponse. sendRedirect() method.
Q.9 Consider the following code:
public void doGet(HttpServletRequest req, HttpServletResponse res) {
    HttpSession session = req.getSession();
    ServletContext ctx = this.getServletContext();
    if(req.getParameter(“userid”) != null) {
        String userid = req.getParameter(“userid”);
        //1
    }
}
You want the userid parameter to be available only to the requests that come from the same user. Which of the following lines would you insert at //1? (Select one)
a. session.setAttribute(“userid”, userid);
b. req.setAttribute(“userid”, userid);
c. ctx.addAttribute(“userid”, userid);
d. session.addAttribute(“userid”, userid);
e. this.addParameter(“userid”, userid);
f. this.setAttribute(“userid”, userid);
Answer: a
Explanation
Attributes stored in the session scope are available only for the requests from the same client. Attributes stored in the context scope are available for all the requests to the same web application from all the clients. Attributes stored in the request scope are available only for the request in which it is stored.
Q.10 Which of the following lines would you use to include the output of Data-Servlet into any other servlet? (Select one)
a. RequestDispatcher rd = request.getRequestDispatcher(“/servlet/DataServlet”);                        rd.include(request, response);
b. RequestDispatcher rd = request.getRequestDispatcher(“/servlet/DataServlet”);                       rd.include(response);
c. RequestDispatcher rd = request.getRequestDispatcher();
   rd.include(“/servlet/DataServlet”, request, response);
d. RequestDispatcher rd = request.getRequestDispatcher();
   rd.include(“/servlet/DataServlet”, response);
e. RequestDispatcher rd = request.getRequestDispatcher();
   rd.include(“/servlet/DataServlet”);
Answer: a
Explanation
To forward or include a request to another resource, first you need to get a RequestDispatcher object from either ServletRequest or ServletContext. Then you can call include() or forward() and pass the current request and response objects as parameters.

No comments: