JSP Best Practices


1. Overview

In this post, I would like to explain the best practices for Java Server Pages(JSP). This post belongs to my Java Best Practices Series category. These JSP best practices I follow my day to day project work. This post may help to design a clean dynamic JSP page.

JavaServer Pages (JSP) is a technology for developing Web pages that support dynamic content. This helps developers insert Java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.

Check out complete JSP latest up-to-date tutorial at https://www.javaguides.net/p/jsp-tutorial.html.

2. JSP Best Practices

Here are a few best practices to design a clean dynamic JSP page design.

2.1 Separate Presentation Markup From Java

Separating presentation markup such as HTML from Java code is a good practice to get better performance from your application.

The following are a few tips:
  • Use JavaBeans for the business logic and JSPs only for the view. Thus, JSPs should primarily contain logic for HTML (or other presentation markups) generation only. 
  • Use stylesheets when appropriate to provide even more separation of the aspects of HTML that a user can control better. 
  • JSPs containing a large amount of static content, including large amounts of HTML code that does not change at runtime, which may result in slow translation and execution. 
  • Use dynamic includes, or better, enable the external resource configuration parameter to put the static HTML into a Java resource file.

2.2 Use JSP Template Mechanism

Using the JSP code out.print("") requires more resources than including static template text. For performance reasons, it is best to reserve the use of out.print() for dynamic text.

2.3 Use the MVC Pattern

While other design patterns can be used effectively with JSPs, I often use the Model-View-Controller (MVC) architecture with JSP technology. Frameworks like Spring and Struts provides Model-View-Controller (MVC) architecture with JSP technology.

2.4 Use View Helpers

View Helper Pattern separates the static view such as JSPs from the processing of the business model data.
Frameworks like Spring and Struts provide their own tag libraries to encapsulate processing logic in a helper instead of a view such as JSP files. For example,
  • Struts framework provides Tag libraries that allow you to build the view part of the MVC without embedding Java code directly within your application JSPs.
  • Spring Framework provides some tags for form bindings, spring security tabs, evaluating errors, setting themes and outputting internationalized messages.

2.5 Use Available JSP Tags in Tag Library

JSP tags make the JSP code cleaner, and more importantly, provide easy reuse. In some cases, there is also a performance benefit. Frameworks like Spring and Struts provide their own tag libraries to encapsulate processing logic in a helper instead of a view such as JSP files.

2.6 Set Sessions=False If Not Using Sessions

The default for JSPs is session="true". If your JSPs do not use any sessions, you should set session="false" to eliminate the overhead of creating and releasing these internal sessions created by the JSP runtime. To disable sessions, set the directive as follows:
<%@page session="false" %>

2.7 Always Invalidate Sessions When No Longer Used

Sessions add performance overhead to your Web applications. Each session is an instance of the javax.servlet.http.HttpSession class. The amount of memory used per session depends on the size of the session objects created.
If you use sessions, ensure that you explicitly cancel each session using the invalidate() method to release the memory occupied by each session when you no longer need it.
You can change this for a specific application by setting the <session-timeout>parameter in the <session-config> element of web.xml.

2.8 Use Compile-Time Object Introspection

Developers should try to rely on compile-time object introspection on the beans and objects generated by the tag library instead of request-time introspection.

2.9 Choose Static Versus Dynamic Includes Appropriately

JSP pages have two different include mechanisms:
Static includes which have a page directive such as:
    <%@ include file="filename.jsp" %>
Dynamic includes which have a page directive such as:
    <jsp:include page="filename.jsp" flush="true" />
Static includes create a copy of the include file in the JSP. Therefore, it increases the page size of the JSP, but it avoids additional trips to the request dispatcher. 

Dynamic includes are analogous to function calls. Therefore, they do not increase the page size of the calling JSP, but they do increase the processing overhead because each call must go through the request dispatcher.

2.10 Disable JSP Page Buffer If Not Used

In order to allow part of the response body to be produced before the response headers are set, JSPs can store the body in a buffer.
When the buffer is full or at the end of the page, the JSP runtime will send all headers that have been set, followed by any buffered body content. This buffer is also required if the page uses dynamic contentType settings, forwards, or error pages. The default size of a JSP page buffer is 8 KB. If you need to increase the buffer size, for example to 20KB, you can use the following JSP attribute and directive:
<%@page buffer="20kb" %>
If you are not using any JSP features that require buffering, you can disable it to improve performance; memory will not be used in creating the buffer, and output can go directly to the browser. You can use the following directive to disable buffering:
<%@ page buffer="none" %>

2.11 Use Forwards Instead of Redirects

For JSPs, you can pass control from one page to another by using forward or redirect, but forward is always faster. When you use forward, the forwarded target page is invoked internally by the JSP runtime, which continues to process the request. The browser is totally unaware that such an action has taken place.
When you use redirect, the browser actually has to make a new request to the redirected page. The URL shown in the browser is changed to the URL of the redirected page, but it stays the same in forwarding operation.
Therefore, redirect is always slower than the forward operation. In addition, all request scope objects are unavailable to the redirected page because redirect involves a new request. Use redirect only if you want the URL to reflect the actual page that is being executed in case the user wants to reload the page.

2.12 Use JSP comments in most cases

Appropriate commenting seems to challenge software developers. JSPs, like other types of code, should include comments that describe complex or extraordinary functionality, the pages' purpose, and other general information typically commented out in the source code.

2.13 Follow HTML best practices

The W3C (World Wide Web Consortium) has some resources related to HTML best practices and validation. Utilize the JSP exception mechanism.

3. Conclusion

This post explained the best practices for JSP dynamic page. Learn more about core java on Core Java Developer Guide.
Check out complete JSP latest up-to-date tutorial at https://www.javaguides.net/p/jsp-tutorial.html.

Let us know if you know any other best practices for JSP. Feel free to comment on this post and give us a suggestion or feedback.

4. Related Posts

Comments

Post a Comment