JSP tag library (JSTL)

So in this tutorial we are going to learn how to use JSP Tags. We are going to cover the following topics;

  • What exactly are JSP tags?
  • We’ll take a look at a sample use case.
  • Then we’ll take a look at the benefits of JSP Tags.

There are basically two categories of JSP Tags:

  • JSP Standard Tag Library (JSTL): Folks over at Oracle have developed a common set of tags you can use in your JSP pages.
  • JSP Custom Tags: Here you can write your own custom code and implement that as JSP tags. I’ll show you how to write your custom tags later.

Sample Use Case

In order to better understand JSP tags, let’s consider this problem.

Your boss comes up to you and asks you to display the price of a specific stock on your JSP pages. Now you can go about solving this problem in two different ways.

  • You can make use of a scriptlet on your JSP page. In the Scriptlet, you can connect to the main stock service, query for your desired stock, parse the XML or JSON you receive and display the result in your page. Doing so can achieve the required result, but it is considered really bad to mix your business code with your presentation code.
  • Or you can make use of JSP Tags to add the stock price to your JSP page. Doing so, you’ll move the business logic into a supporting class and then you’ll simply insert a JSP tag into the page to achieve the desired functionality. E.g:
<demo:stockPricestockName=”xyz” />

The real heavy processing will be done by a supporting Java class.

So that is the basic idea for JSP Custom Tags, you implement a class with all the required functions and then drop a tag in the JSP page to implement your custom functionality.

Benefits of Using JSP Custom Tags

There are a lot of benefits of using JSP Tags:

  • You’ll minimize the amount of code in your JSP scriptlets.
  • You’ll minimize the amount of code in your JSP page.
  • You’ll get separation of concerns, keeping your business logic separate from your presentation code.
  • You’ll get to keep your JSP page simple.
  • It allows you to keep your focus on presentation.
  • The best part is that tags are reusable, you can easily reuse the tag in your other pages.

JSTL

So the JSP Standard Tag Library is a common set of tags you can use and reuse in your JSP Pages. There is:

  • Core Tags: for handling variables, looping and conditionals.
  • Messages Formatting I1&N: for handling internationalization and formatting.
  • Function Tags: for stuff like string manipulation, getting the sizes of arrays and collections.
  • XMLTags: for handling XML and parsing it for your JSP pages.
  • SQL Tags: it contains all the tags for connecting with a database and all the related operations.

Now in the following tutorial, I am going to go into detail with every one of these categories but won’t discuss the SQL tags since they are generally considered a bad practice. In the industry SQL tags are considered good for prototyping, but not for production environments. Even Oracle says that SQL tags aren’t good for production apps.

You can head over to http://jstl.java.net for more information on the Standard Tag Library.

Series Navigation<< State management in JSP – 2JSP Tags – Installing JSTL >>
By | 2017-07-14T22:21:24+00:00 April 11th, 2016|JSP|0 Comments

About the Author:

Leave A Comment