Pr.Pg Next Pg

JSP Interview Questions and Answers Part 4

 

31.Can a JSP page process HTML FORM data?

  • Yes.

  • However, unlike Servlet, you are not required to implement HTTP-protocol specific methods like doGet() or doPost() within your JSP page.

  • You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression as.

 

32.Is there a way to reference the "this" variable within a JSP page?

  • Yes, there is.

  • Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the Servlet generated by the JSP page.

 

33.How do you pass control from one JSP page to another?

  • Use the following ways to pass control of a request from one servlet to another or one jsp to another.

  • The RequestDispatcher object s forward method to pass the control.

  • The response.sendRedirect method

 

34.Is there a way I can set the inactivity lease period on a per-session basis?

  • Typically, a default inactivity lease period for all sessions is set within your JSPengine admin screen or associated properties file.

  • However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis.

  • This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created.

 

35.How does a servlet communicate with a JSP page?

  • The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser.

  • The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.


public void doPost (HttpServletRequest request, HttpServletResponse response)
{
try {
govi.FormBean f = new govi.FormBean();
String id = request.getParameter("id");
f.setName(request.getParameter("name"));
f.setAddr(request.getParameter("addr"));
f.setAge(request.getParameter("age"));

//use the id to compute
//additional bean properties like info
//maybe perform a db query, etc.
// . . .

f.setPersonalizationInfo(info);
request.setAttribute("fBean",f);
getServletConfig().getServletContext().getRequestDispatcher
("/jsp/Bean1.jsp").forward(request, response);
} catch (Exception ex) {
. . .
}
}

  • The JSP page Bean1.jsp can then process fBean, a fter first extracting it from the default request
    scope via the useBean action.



36.Can you make use of a ServletOutputStream object from within a JSP page?

  • No.

  • You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients.
    A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not.
    A page author can always disable the default buffering for any page using a page directive as:

 

 

37.Can a JSP page instantiate a serialized bean?

  • No problem!

  • The use Bean action specifies the beanName attribute, which can be used for indicating a serialized bean.

  • For example: A couple of important points to note. Although you would have to name your serialized file "filename.ser", you only indicate "filename" as the value for the beanName attribute.

  • Also, you will have to place your serialized file within the WEB-INFjspbeans directory for it to be located by the JSP engine.

 

38.How do I mix JSP and SSI #include?

  • If you're just including raw HTML, use the #include directive as usual inside your .jsp file.
    But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file.

  • If your data.inc file contains jsp code you will have to use
    The is used for including non-JSP files.

39.How do I mix JSP and SSI #include? What is the difference between include directive & jsp:include action?

  • Difference between include directive and include action:

1. provides the benefits of automatic recompliation,smaller class size ,since the code corresponding to the included page is not present in the servlet for every included jsp page and option of specifying the additional request parameter.
2. The also supports the use of request time attributes values for dynamically specifying included page which directive does not.
3. the include directive can only incorporate contents from a static document.
4. can be used to include dynamically generated output e.g.. from servlets.
5. include directive offers the option of sharing local variables, better run time efficiency.Because the include directive is processed during translation and compilation, it does not impose any restrictions on output buffering.

40.How do you prevent the Creation of a Session in a JSP Page and why? What is the difference between include directive & jsp:include action?

  • By default, a JSP page will automatically create a session for the request if one does not exist.
    However, sessions consume resources and if it is not necessary to maintain a session, one should not be created.

  • For example, a marketing campaign may suggest the reader visit a web page for more information.

  • If it is anticipated that a lot of traffic will hit that page, you may want to optimize the load on the machine by not creating useless sessions. 


Pr.Pg border                                              Next Pg