[JSP] Use JSTL to enhance JSP functions

This article is for learning reference only!

JSTL stands for JavaServer Pages Standard Tag Library; it provides a core set of web page functionality that can perform many basic tasks such as conditional and iteration of structural elements, manipulation of XML documents, and support for internationalization tags to more complex SQL element processing. JSP is famous for scriples, but since the advent of JSTL and EL (Expression Language), people have strongly discouraged the use of JSP. The reason is that scriplet elements in JSP violate many basic principles of good engineering practice. This article digs into some of the key aspects of JSTL that underscore the ease of using it on scriplets.

Problems with using script elements in JSP

See some issues caused by script elements (elements written within <% … %> scope). It’s just too much. Let’s take a quick look at some of them and the rest you can easily deduce.

  • **Reusable:** A scriptlet embedded in a JSP cannot be reused. Therefore, features that use object-oriented techniques such as inheritance, composition, and association are completely inapplicable. Also, there is no way to make scriplets abstract. The side effect is duplicating the same code in multiple places, which is very unacceptable practice in object-oriented programming.
  • **Debugging:** This is one of the dark areas. Trying to debug a JSP page littered with scriplets scattered decoratively on some insignificant corners of HTML markup. I mean, if you haven’t come across such a file, let me tell you, it’s easy to create one, at least in JSP. to debug it. Now comes the hard part; don’t bang your head. Any exception thrown halfway through the code will give a blank page, an empty scroll to create your own meaning.
  • **Maintainability:** Well, against something reusable with horrible debug capabilities, it’s impossible. Messy, crippled and repetitive code is almost unmaintainable.

However, some simple scripts in JSP are a sign of convenience, but business logic written in JSP scripts is still a strict no-no, not because it cannot be written, but because it is a bad omen (difficult to maintain). The coding conventions in the JSP 1.2 specification recommend the use of the JSP standard tag library in Web applications. This will help reduce the need for JSP scripts in JSP pages. In general, pages using JSTL are easier to read and maintain. See Code Conventions for JSP Technology…. In addition to JSTL, there are other ways to overcome some of the above problems, such as using Java Bean components to write business logic, etc. But let’s drop those details and stick with what JSTL has to offer.

JSTL Overview

JSTL tags can be divided into five categories. Each of them can be used for a specific purpose. For example:

  • Core tags are used for general programming in JSP pages, such as displaying string elements, conditional statements, iteration, and redirecting to new URLs. The syntax for including the core library is:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
  • Formatting tags are especially useful for internationalization, in addition to formatting text, dates, times, and numbers. The syntax for including a library is:

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
  • When dealing with relational databases, SQL tags can be used for all CRUD operations. The syntax for including a library is:

    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    
  • XML tags can be used to manipulate XML documents, such as parsing and transforming XML data. The syntax for including a library is:

    <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
    
  • JSTL functions provide a number of tags for string manipulation. The syntax for including a library is:

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    

See the API documentation for the JSTL 1.1 library for more information on the markup details for each category.

Simple CRUD application in JSTL

This example demonstrates how to write a simple CRUD application using JSTL without even writing a single scriplet element in the JSP page. Note that this program is a working but basic implementation. Data validation and error handling are omitted for brevity. For the same reason, the dazzling CSS part is not given in the code section below.

JSTL1

**Figure 1:** The program being executed

The program uses a MySQL database on the backend. The SQL code to create the table is as follows.

CREATE DATABASE testdb1;
USE testdb1;
CREATE TABLE address_book
( id INT PRIMARY KEY,
   fname VARCHAR(30),
   lname VARCHAR(30),
   phone VARCHAR(30),
   email VARCHAR(30)
);

Page workflow

JSTL2
**Figure 2:** The workflow diagram of the page

index.jsp

<%--
   Document : index
   Created on : 29 Dec, 2015, 7:48:45 PM
   Author : mano
--%>

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


   
      
      
      Home Page
   
   
      
      
         SELECT * FROM address_book;
      

      

New Address

Add new address details



ID First Name Last Name Phone Email
">Update ">Delete

insert. jsp

<%--
   Document : insert
   Created on : 29 Dec, 2015, 8:15:47 PM
   Author : mano
--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


   
      
      JSP Page
   
   

      


      
         INSERT INTO address_book(id, fname, lname, phone, email)
            VALUES (?,?,?,?,?);
         
         
         
         
         
      
      
         
            
         
      
   

insertForm.jsp

<%--
   Document : insertForm
   Created on : 29 Dec, 2015, 8:36:06 PM
   Author : mano
--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


   
      
      
      Insert form Page
   
   
      

      
         SELECT * from address_book where id=?;
         
      


      

Update Address

Update Address ID

update.jsp

<%--
   Document : update
   Created on : 29 Dec, 2015, 9:52:26 PM
   Author : mano
--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


   
      
      JSP Page
   
   
      

      
         UPDATE address_book SET fname=?, lname=?, phone=?,
            email=? WHERE id=?;
         
         
         
         
         
      

      
         
            
         
      
   

delete.jsp

<%--
   Document : delete
   Created on : 29 Dec, 2015, 10:08:06 PM
   Author : mano
--%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>


   
      
      JSP Page
   
   
      
      
         DELETE FROM address_book WHERE id=${param.id};
      
      
         
            
         
      
   

Conclusion

By using JSTL, we can easily overcome the shortcomings of script elements in JSP pages. Code becomes simpler and more readable. However, JSTL is no substitute for the flexibility of scripting. However, as a rule of thumb, programmers should avoid writing scriplets, especially if other technologies such as JSTL meet their needs.