What JSP lifecycle methods can I override?
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page.
jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page.
It is good programming practice to free any allocated resources within jspDestroy().
The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:
<%!public void jspInit() {. . .}%><%!public void jspDestroy() {. . .}%>
Is there a way to reference the "this" variable within a JSP page?
Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the servlet generated by the JSP page.
Can I stop JSP execution while in the midst of processing a request?
Yes. Preemptive termination of request processing on an error condition is a good way to maximize the throughput of a high-volume JSP engine. The trick (asuming Java is your scripting language) is to use the return statement when you want to terminate further processing.For example, consider:<% if (request.getParameter("foo") != null) {// generate some html or update bean property} else {/* output some error message or provide redirection back to the input form after creating a memento bean updated with the "valid" form elements that were input. This bean can now be used by the previous form to initialize the input elements that were valid then, return from the body of the _jspService() method to terminate further processing */return;}%>
How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
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.1response.setHeader("Pragma","no-cache"); //HTTP 1.0response.setDateHeader ("Expires", 0); //prevents caching at the proxy server%>
How does JSP handle run-time exceptions?
You can use the errorPage attribute of the page directive to have uncaught runtime 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" %>the 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.
0 Comments
Can you make use of a ServletOutputStream object from within a JSP page? What is preinitialization of a servlet? What are cookies and how will you use them? What is a Expression? What are implicit objects? List them? What does the JSP engine do when presented with a JavaServer Page to process? What is the difference between RequestDispatcher and sendRedirect? |