How can I avoid writing Java code in JSP files using JSP 2?

Writing Java code within JSP files has been a common practice, especially in the earlier versions of JSP. However, with the introduction of JSP 2, there are alternative ways to avoid writing Java code directly in JSP files. This article will discuss the alternative JSP 2 lines and the technique called Expression Language (EL) and JSTL (JSP Standard Tag Library) that can be used to achieve this.

1. Expression Language (EL)

Expression Language is a scripting language used in JSP files to access and manipulate Java objects or values without the need for embedded Java code. It simplifies the code and makes it more readable. EL can be used in JSP 2 or later versions.

The following are some examples of how to use EL in JSP files:

1.1 Printing Variable Value


                ${variableName}
            

For example:


                ${name}
            

In the above example, if 'name' is a variable in the Java code associated with the JSP file, its value will be printed.

1.2 Evaluating Expressions


                ${expression}
            

For example:


                ${1 + 1}
            

In this example, the expression "1 + 1" will be evaluated and the result will be printed in the JSP file.

1.3 Accessing Properties or Methods of Objects


                ${object.property}
                ${object.method()}
            

For example:


                ${person.name}
                ${person.getAge()}
            

In the above examples, the property 'name' of the 'person' object and the result of the 'getAge()' method of the 'person' object will be accessed and printed.

2. JSP Standard Tag Library (JSTL)

JSTL is a collection of custom tags that provides additional functionality to JSP files. JSTL tags can be used to perform various tasks without writing Java code directly. JSTL is available in JSP 1.2 or later versions.

The following are examples of how to use JSTL in JSP files:

2.1 Core Tags

The core tags of JSTL are used for basic looping, conditional processing, and variable manipulation.


                <c:forEach>
                    ...
                </c:forEach>

                <c:if test="${condition}">
                    ...
                </c:if>

                <c:set var="variableName" value="${expression}">

                </c:set>
            

For example:


                <c:forEach items="${people}" var="person">
                    <li>${person.name}</li>
                </c:forEach>

                <c:if test="${age >= 18}">
                    <p>You are eligible to vote.</p>
                </c:if>

                <c:set var="sum" value="${number1 + number2}">
            

In the above examples, the <c:forEach> tag is used to iterate over a collection of 'people' objects and print their names. The <c:if> tag is used to conditionally display a message based on the 'age' variable. The <c:set> tag is used to set the value of the 'sum' variable based on the addition of 'number1' and 'number2'.

2.2 Formatting Tags

The formatting tags of JSTL are used for formatting dates, numbers, and strings.


                <fmt:formatDate value="${date}" pattern="dd-MM-yyyy" />

                <fmt:formatNumber value="${number}" pattern="#,##0.00" />

                <fmt:formatMessage key="messageKey" />
            

For example:


                <fmt:formatDate value="${date}" pattern="dd-MM-yyyy" />

                <fmt:formatNumber value="${number}" pattern="#,##0.00" />

                <fmt:formatMessage key="welcome.message" />
            

In the above examples, the <fmt:formatDate> tag is used to format the 'date' variable based on the specified pattern. The <fmt:formatNumber> tag is used to format the 'number' variable based on the specified pattern. The <fmt:formatMessage> tag is used to retrieve and display a localized message based on the specified key.

Conclusion

By using Expression Language (EL) and JSP Standard Tag Library (JSTL), Java code can be avoided in JSP files, making the code more readable and maintainable. EL can be used in JSP 2 or later versions, while JSTL is available in JSP 1.2 or later versions. These techniques provide a more declarative approach to manipulating and displaying data in JSP files, reducing the need for embedded Java code.