Sunday, August 2, 2009

JSP interview Questions

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" %>

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? - 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)% >" >

How can I get to print the stacktrace for an exception occuring within my JSP page?

 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();

                 exception.printStackTrace(pw);

                out.println(" ");

                %>

 

How do you pass an InitParameter to a JSP?

 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);

                }

                }

                %>

How can my JSP page communicate with an EJB Session Bean?

 The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean:

                <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %>

                <%!

                //declare a "global" reference to an instance of the home interface of the session bean

 

1

2

3

4

5

6

7

8

9

10

 

No comments:

Post a Comment