Pr.Pg Next Pg

JSP Interview Questions and Answers Part 5

 

41.How do I use a scriptlet to initialize a newly instantiated bean?

  • A jsp:useBean action may optionally have a body.

  • If the body is specified, its contents will be automatically invoked when the specified bean is instantiated.

  • Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.

  • The following example shows the "today" property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.
    value=""/ >

 

42.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:

 

43.How do you connect to the database from JSP?

  • A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets.

  • Further then you can use the resultset object "res" to read data in the following way.

 

44.What is the page directive is used to prevent a JSP page from automatically creating a session?

  • <%@ page session="false">

 

45.How do you delete a Cookie within a JSP?

Cookie mycook = new Cookie("name","value");
response.addCookie(mycook);
Cookie killmycook = new Cookie("mycook","value");
killmycook.setMaxAge(0);
killmycook.setPath("/");
killmycook.addCookie(killmycook);

 

46.Can we implement an interface in a JSP?

  • No

 

47.What is the difference between ServletContext and PageContext?

ServletContext:

  • Gives the information about the container

PageContext:

  • Gives the information about the Request

 

48.What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

  • request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.

 

49.How to pass information from JSP to included JSP?

  • Using <%jsp:param> tag.

 

50.How is JSP include directive different from JSP include action. ?

  • When a JSP include directive is used, the included file's code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet.

  • While if any page is included using action tag, the page's output is returned back to the added page.

  • This happens at runtime.


Pr.Pg border                                              Next Pg