Home Java Technology JSP JSP Sample Interview Questions 7

Login Form




JSP Sample Interview Questions 7 Print E-mail

1. How does JSP handle run-time exceptions?
 
Answer: You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example:
<%@ page errorPage=\"error.jsp\" %> redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive: <%@ page isErrorPage=\"true\" %> Throwable object describing the exception may be accessed within the error page via the exception implicit object. Note: You must always use a relative URL as the value for the errorPage attribute.
 
2. How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values?
 
Answer: 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="lastName" value="<%=blanknull(lastName)% >" >


3. Is JSP technology extensible
Answer: Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.


4. What is difference between custom JSP tags and beans
Answer: Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:

    * Custom tags can manipulate JSP content; beans cannot.
    * Complex operations can be reduced to a significantly simpler form with custom tags than with beans.
    * Custom tags require quite a bit more work to set up than do beans.
    * Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.
    * Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.


5. Is there a way I can set the inactivity lease period on a per-session basis
Answer: Typically, a default inactivity lease period for all sessions is set within your JSP engine 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. For example:
    <%
    session.setMaxInactiveInterval(300);
    %>
would reset the inactivity period for this session to 5 minutes. The inactivity interval is set in seconds.

6. How do you pass an InitParameter to a JSP
Answer: The JspPage interface defines the jspInit() and jspDestroy() method which the page writer can use in their pages and are invoked in much the same manner as the init() and destory() methods of a servlet. The example page below enumerates through all the parameters and prints them to the console.
    <%@ page import="java.util.*" %>
    <%!
    ServletConfig cfg =null;
    public void jspInit(){
    ServletConfig cfg=getServletConfig();
    for (Enumeration e=cfg.getInitParameterNames(); e.hasMoreElements();) {
    String name=(String)e.nextElement();
    String value = cfg.getInitParameter(name);
    System.out.println(name+"="+value);
    }
    }
    %>

 

7. What is the difference between directive include and jsp include
Answer: <%@ include>: Used to include static resources during translation time. JSP include: Used to include dynamic content or static content during runtime.

8. How do I prevent the output of my JSP or Servlet pages from being cached by the browser
Answer: You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.
<%
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>

 

9. How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values
Answer: 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)% >" >