Thursday, June 24, 2010

JSP

QUESTION: How do I mix JSP and SSI #include?
ANSWER: If you're just including raw HTML, use the #include directive as usual inside your .jsp file.

But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data.inc file contains jsp code you will have to use

The is used for including non-JSP files.
QUESTION: Can a JSP page process HTML FORM data?
ANSWER: Yes. However, unlike servlets, you are not required to implement HTTP-protocol specific methods like doGet() or doPost() within your JSP page. You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression as:

or


QUESTION: What JSP lifecycle methods can I override?
ANSWER: 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:

QUESTION: How do I include static files within a JSP page?
ANSWER: Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. The following example shows the syntax:

Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.

QUESTION: How do I perform browser redirection from a JSP page?
ANSWER: You can use the response implicit object to redirect the browser to a different resource, as:
response.sendRedirect(http://www.foo.com/path/error.html);

You can also physically alter the Location HTTP header attribute, as shown below:

You can also use the: Also note that you can only use this before any output has been sent to the client. I beleve this is the case with the response.sendRedirect() method as well.
If you want to pass any paramateres then you can pass using >
QUESTION: Can a JSP page instantiate a serialized bean?
ANSWER: No problem! The useBean action specifies the beanName attribute, which can be used for indicating a serialized bean. For example:
 
A couple of important points to note. Although you would have to name your serialized file "filename.ser", you only indicate "filename" as the value for the beanName attribute. Also, you will have to place your serialized file within the WEB-INF\jsp\beans directory for it to be located by the JSP engine.

QUESTION: Can you make use of a ServletOutputStream object from within a JSP page?
ANSWER: No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients. A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not. A page author can always disable the default buffering for any page using a page directive as:


QUESTION: What's a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?
ANSWER: Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data. The key however, is to effectively minimize the amount of code that is synchronzied so that you take maximum advantage of multithreading.
Also, note that SingleThreadModel is pretty resource intensive from the server's perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free - which results in poor performance. Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool.

QUESTION: Can I stop JSP execution while in the midst of processing a request?
ANSWER: 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:

QUESTION: How can I get to view any compilation/parsing errors at the client while developing JSP pages?
ANSWER: With JSWDK 1.0, set the following servlet initialization property within the \WEB-INF\servlets.properties file for your application:

jsp.initparams=sendErrToClient=true

This will cause any compilation/parsing errors to be sent as part of the response to the client.