Updated April 15, 2023
Introduction to JSP format
Format tags in JSP are imported with the help of (The JavaServer Pages Standard Tag Library) JSTL library. These are used to edit the look of text or numbers used on the page. For example, date, time, timestamp, etc., are formats which can be obtained in a standardized form with the help of format functions in JSP. These standards are maintained to avoid the confusion on formats used throughout the world. There can be several ways to define a format as per the business requirements; some of the ways are via JSP functions, using format tags from the JSTL library in JSP, using XML data source for maintaining the format, and then extracting it using JSTL and JSP. In this topic, we would see examples and explanations for formatting the data using some of these methods.
Syntax
Some dependent libraries should be included in the project’s lib folder as the format tag has dependencies in the JSTL library. You can either link a full set of libraries in jSTL or can target specific files as per your purpose. Two statements to be written before start writing the code to use the format tag are:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
The prefix denotes the format one would like to use. One can get a complete list of “prefixes” with uri the same as above as per the requirements.
The syntaxes used by JSP Format are:
- Number: <fmt:formatNumber properties> Content to be formatted </fmt:formatNumber>
- Parsing a customised TimeZone: <fmt:timeZone properties> Content to be formatted </fmt:timeZone>
- Parsing a new number: <fmt:parseNumber properties> Content to be formatted </fmt:parseNumber>
- Parsing a date: </fmt:parseDate val=”” var=”” pattern=””>
- Setting a timezone from existing Time Zones in the system: <fmt:setTimeZone value=”” var=”” scope=””/>
- Setting a date format from existing date format in the system: <fmt:formatDate var=”” value=”” type=”” dateStyle=”” timeStyle=””/>
How does the format tag work in JSP?
JSP Format uses the JSTL library to use the inbuilt formatting options incorporated in predefined format tags with the help of various formats. The prerequisite to use formatting in JSP is that libraries should be attached and present in the lib folder of your project. The syntax above contains some of the common property names. Formatting can be done to match up the existing standards with the help of inbuilt properties, but sometimes the format is local; for example, we want a pricing rate format to be 1 rupee per word, i.e. “1/word”. To save this format, a new pattern or a local xml database has to be created. While just showing the time from “timestamp” is more of formatting from the existing pattern. These are explained in more detail with the help of examples provided in the below section.
Examples of JSP format
Some of the examples to demonstrate the use of the JSP format tag in JSP is explained below for a better understanding of the topic.
Example #1
Code: NewFile.JSP
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>JSP Format tag</title>
</head>
<body>
<h2>Grocery information</h2>
<c:set var="material">
<materials>
<material>
<name>Wheat Flour</name>
<price>46/kg</price>
</material>
<material>
<name>Sugar</name>
<price>20/kg</price>
</material>
<material>
<name>Ground Flour</name>
<price>36/kg</price>
</material>
<material>
<material>
<name>Salt</name>
<price>30/kg</price>
</material>
<name>Rice</name>
<price>100/kg</price>
</material>
</materials>
</c:set>
<x:parse xml="${material}" var="output"/>
Here is one of the items on the list, along with its price. The list in the background is a customized list along with its defined pricing style.
<br><br>
<b>Price of the </b><x:out select="$output/materials/material[2]/name" /> <b> is</b>:
<x:out select="$output/materials/material[2]/price" />
</body>
</html>
Output:
Explanation: In this example, an XML tag is used to prepare a database of a customized list of grocery materials and their pricing. The set tage, <c:set var=”material”> is used to store values of material in the variable named “material” along with its prices and name. Once the data is stored in a specific format, then it can be pulled out using the tag. The data which should be displayed on the output screen can be selected via a select attribute of our tag. The command, <x:out select=”$output/materials/material[2]/price” /> pulls out the value from XML database as per the format stored in there. In the same way, anime is extracted by changing the variable name picked up from the database. It is demonstrated by a tag: <b>Price of the </b><x:out select=”$output/materials/material[2]/name” /> <b>
Example #2
Code : NewFile2.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Format tag in JSP- 2</title>
</head>
<body>
<h3>Parsing the Date:</h3>
This format tag represents the date and time in string format receiving customized format as input.
<br>
<br><br>
<c:set var="d" value="31-12-2020" />
The date entered in a format: 31-12-2020 <br>
<fmt:parseDate value="${d}" var="pd" pattern="dd-MM-yyyy" />
<p>The date after format pattern is applied: <c:out value="${pd}" /></p>
</body>
</html>
Output:
Explanation: This is an example of the pre-existing format used to display the inputted information in the applied format. The “fmt:parseDate” helps in identifying the use of formatter in JSP, which is dependent on the JSTL library. “Vale” attributes get the value to be passed into this tag, the “pattern” attribute helps JSP understand the pattern being inputted in the program, and the “var” attribute is used to identify the formatted data, which will be outputted. After the date is passed as input to this tag, then the output is provided in the form of string date and time. This pattern can be seen in the output screen.
Conclusion
JSP format is an easy tag that comes with many functionalities offered by JSP libraries, but it can be complex if looking for a customized, unique data format. Its simplicity does not outweigh the importance attached to this tag. This tag is used very commonly in many websites or businesses linked to finance, export, transportation, agriculture, and many more such sectors.
Recommended Articles
This is a guide to the JSP format. Here we discuss How does the format tag work in JSP and Examples, along with the codes and outputs. You may also have a look at the following articles to learn more –