JSP (Java Server Pages) best practices

This tutorial will explain you some coding standard that you should follow while writing JSP files, the information personally gathered from my last project. Even the developers have their own way to, but try to follow the standard which is the best in industry level and can be easily understand by other programmer.

JSP File Names

The name of your JSPs follows the same convention as standard java variables. That is, the first word should be all lower-case, and every concatenated word after that should begin in upper case. For example :




Scriptlets and Tags

Where possible, avoid JSP scriptlets whenever tag libraries provide equivalent functionality. This makes pages easier to read and maintain, and helps to separate business logic from presentation logic.

In this example, the variable customers is an array of Customers

<% for ( int i=0; i<persons.length; i++ ) { %>
<td><%= persons[i].getLastName() %></td>
<td><%= persons[i].getFirstName() %></td>
<% } %>

If you write the same code using JSTL (JSP Standard Tag Library), the variable customers will support any type of collection, including an array, an Enumeration, or any object implementing the java.util.Collection interface.

<c:forEach var="person" items="${ persons }">
<td><c:out value="${person.lastName}"/></td>
<td><c:out value="${person.firstName}"/></td>

Summary for the use of Scriptlets and Tags in JSPs

• JSP scriptlets should ideally be non-existent in the JSP page, and business logic implementation within the JSP page is avoided.

• If not possible, use value objects (JavaBeans components) for carrying information to and from the server side, and use JSP scriptlets for transforming value objects to client outputs.

• Use custom tags whenever available for processing information on the server side.

JSP Expressions

JSP Expressions should be used just as sparingly as JSP Scriptlets. To illustrate this, let’s look as the following three examples which accomplish equivalent tasks:

Example 1 - explicit Java code :
<%= myBean.getName() %>
Example 2 - with JSP tag :
<jsp:getProperty name="myBean" property="name" />
Example 3 - with JSTL tag :
<c:out value="${myBean.name}" />

Example 1 assumes that a scripting variable called myBean is declared. The other two examples assume that myBean is a scoped attribute that can be found using PageContext.findAttribute(). The second example also assumes that myBean was introduced to the page using .

Of the three examples, the JSTL tag example is preferred. It is almost as short as the JSP expression, it is just as easy to read and easier to maintain, and it does not rely on Java scriptlets.

Programming Practices

In general, you should not write Java code (declarations, scriptlets and expressions) in your JSP pages. Here is a list of the reasons why:

• Syntax errors in Java code in a JSP page are not detected until the page is deployed. Syntax errors in tag libraries and servlets, on the other hand, are detected prior to deployment.

• Java code in JSP pages is harder to debug.

• Java code in JSP pages is harder to maintain, especially for page authors who may not be Java experts.

• You should not mix complex business logic with presentation logic. JSP pages are primarily intended for presentation logic.

• Code containing Java code, HTML and other scripting instructions can be hard to read.

• Concerning the use of tags, preference should be given to the use of JSTL over Struts and other tag libraries.


The uniform use of quoting is adopted. Quotations should be bound by two double-quote characters ” instead of two apostrophe characters ‘ .

Non-uniform quoting

<%@ page import='javabeans.*'%>
<%@ page import="java.util.*" %>

Preferred quoting

<%@ page import="javabeans.*" %>
<%@ page import="java.util.*" %>

An exception is when apostrophes are needed, for example when double-quote characters are required within the scripting language:

<jsp:include page='<%= getFoodMenuBar("Monday") %>' />