Logo F2FInterview

JSP Interview Questions

Q   |   QA

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

//creating a cookie
Cookie mycookie = new Cookie(“aName”,”aValue”);
//delete a cookie
Cookie killMyCookie = new Cookie(“mycookie”, null);

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”);
//use the id to compute
//additional bean properties like info
//maybe perform a db query, etc.
// . . .
(“/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” /

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:

Implementing the HttpJspPage interface, if the protocol used is HTTP, or implementing JspPage otherwise Ensuring that all the methods in the Servlet interface are declared final Additionally, your servlet superclass also needs to do the following:
The service() method has to invoke the _jspService() method

The init() method has to invoke the jspInit() method
The destroy() method has to invoke jspDestroy()
If any of the above conditions are not satisfied, the JSP engine may throw a translation error.
Once the superclass has been developed, you can have your JSP extend it as follows:

<%@ page extends=”packageName.ServletName” %>

You could make a simple wrapper function, like

String blanknull(String s) {
return (s == null) ? “” : s;

then use it inside your JSP form, like

<input type=”text” name=”shoesize” value=”<%=blanknull(shoesize)% >” >

By printing out the exception’s stack trace, you can usually diagonse a problem better when debugging JSP pages. By looking at a stack trace, a programmer should be able to discern which method threw the exception and which method called that method. However, you cannot print the stacktrace using the JSP out implicit variable, which is of type JspWriter. You will have to use a PrintWriter object instead. The following snippet demonstrates how you can print a stacktrace from within a JSP error page:

<%@ page isErrorPage=”true” %>
out.println(” ”);
PrintWriter pw = response.getWriter();
out.println(” “);

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: