Question: How do I have the JSP-generated servlet subclass my own custom servlet class, instead of the default?
Answer: 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" %<
Question: 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
" >
Question: How can I get to print the stacktrace for an exception occuring within my JSP page?
Answer: 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("");%>
Question: 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.
0 comments:
Post a Comment