Sunday, August 2, 2009

JSP interview Questions

inactivity lease period on a per-session basis. This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created. For example:

                <%

                session.setMaxInactiveInterval(300);

                %>

 

would reset the inactivity period for this session to 5 minutes. The inactivity interval is set in seconds.

How can I set a cookie and delete a cookie from within a JSP page?

 A cookie, mycookie, can be deleted using the following scriptlet:

                <%

                //creating a cookie

                Cookie mycookie = new Cookie("aName","aValue");

                response.addCookie(mycookie);

                //delete a cookie

                Cookie killMyCookie = new Cookie("mycookie", null);

                killMyCookie.setMaxAge(0);

                killMyCookie.setPath("/");

                response.addCookie(killMyCookie);

                %>

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, after first extracting it from the default request scope via the useBean action.

jsp:useBean id="fBean" class="govi.FormBean" scope="request"

/ jsp:getProperty name="fBean" property="name"

/ jsp:getProperty name="fBean" property="addr"

/ jsp:getProperty name="fBean" property="age"

/ jsp:getProperty name="fBean" property="personalizationInfo" /

How do I have the JSP-generated servlet subclass my own custom servlet class, instead of the default?

One should be very careful when having JSP pages extend custom servlet classes as opposed to the default one generated by the JSP engine. In doing so, you may lose out on any advanced optimization that may be provided by the JSP engine. In any case, your new superclass has to fulfill the contract with the JSP engine by:

 

1

2

3

4

5

6

7

8

9

10

 

No comments:

Post a Comment