Unit 5 - JSP
Unit 5 - JSP
Unit 5 - JSP
Reinitialization
Subsequent
User Requests
Response Document
Advantages of JSP over Servlet
There are many advantages of JSP over the Servlet. They are as follows:
1. Extension to Servlet
2. JSP technology is the extension to Servlet technology. We can use
all the features of the Servlet in JSP. In addition to, we can use
implicit objects, predefined tags, expression language and Custom
tags in JSP, that makes JSP development easy.
3. Easy to maintain
4. JSP can be easily managed because we can easily separate our
business logic with presentation logic. In Servlet technology, we mix
our business logic with the presentation logic.
5. Fast Development: No need to recompile and redeploy
6. If JSP page is modified, we don't need to recompile and redeploy
the project.
The Servlet code needs to be updated and recompiled if we
have to change the look and feel of the application.
7. Less code than Servlet
8. In JSP, we can use many tags such as action tags, JSTL, custom tags,
etc. that reduces the code. Moreover, we can use EL, implicit
objects, etc.
Applications of JSP
As mentioned before, JSP is one of the most widely used language over
the web. I'm going to list few of them here:
•JSP vs. Active Server Pages (ASP)
The advantages of JSP are twofold. First, the dynamic part is written in
Java, not Visual Basic or other MS specific language, so it is more
powerful and easier to use. Second, it is portable to other operating
systems and non-Microsoft Web servers.
•JSP vs. Pure Servlets
It is more convenient to write (and to modify!) regular HTML than to
have plenty of println statements that generate the HTML.
•JSP vs. Server-Side Includes (SSI)
SSI is really only intended for simple inclusions, not for "real" programs
that use form data, make database connections, and the like.
•JSP vs. JavaScript
JavaScript can generate HTML dynamically on the client but can hardly
interact with the web server to perform complex tasks like database
access and image processing etc.
•JSP vs. Static HTML
Regular HTML, of course, cannot contain dynamic information.
The Lifecycle of a JSP Page
The JSP pages follow these phases:
Translation of JSP Page
Compilation of JSP Page
Classloading (the classloader loads class file)
Instantiation (Object of the Generated Servlet is created).
Initialization ( the container invokes jspInit() method).
Request processing ( the container invokes _jspService()
method).
Destroy ( the container invokes jspDestroy() method).
Lifecycle of a JSP Page
Translation – JSP pages doesn’t look like normal java classes,
actually JSP container parse the JSP pages and translate them to
generate corresponding servlet source code. If JSP file name is
home.jsp, usually its named as home_jsp.java.
Compilation – If the translation is successful, then container
compiles the generated servlet source file to generate class file.
Class Loading – Once JSP is compiled as servlet class, its lifecycle
is similar to servlet and it gets loaded into memory.
Instance Creation – After JSP class is loaded into memory, its
object is instantiated by the container.
Initialization – The JSP class is then initialized and it transforms
from a normal class to servlet. After initialization, ServletConfig
and ServletContext objects become accessible to JSP class.
Request Processing – For every client request, a new thread is
spawned with ServletRequest and ServletResponse to process
and generate the HTML response.
Destroy – Last phase of JSP life cycle where it’s unloaded into
memory.
As depicted in the above diagram, JSP page is
translated into Servlet by the help of JSP
translator.
The JSP translator is a part of the web server
which is responsible for translating the JSP page
into Servlet.
After that, Servlet page is compiled by the
compiler and gets converted into the class file.
Moreover, all the processes that happen in
Servlet are performed on JSP later like
initialization, committing response to the
browser and destroy.
Creating a simple JSP Page
To create the first JSP page, write some HTML code as
given below, and save it by .jsp extension. We have saved
this file as index.jsp. Put it in a folder and paste the folder
in the web-apps directory in apache tomcat to run the JSP
page.
index.jsp
Let's see the simple example of JSP where we are using
the scriptlet tag to put Java code in the JSP page. We will
learn scriptlet tag later.
<html>
<body>
<% out.print(2*5); %>
</body> </html>
It will print 10 on the browser.
1.JSP Implicit Objects
These objects are created by JSP Engine
during translation phase (while translating
JSP to Servlet).
They are being created inside service method
so we can directly use them
within Scriptlet without initializing and
declaring them.
There are total 9 implicit objects available in
JSP.
out javax.servlet.jsp.JspWriter
request javax.servlet.http.HttpServletRequest
response javax.servlet.http.HttpServletResponse
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
exception javax.servlet.jsp.JspException
page java.lang.Object
pageContext javax.servlet.jsp.PageContext
config javax.servlet.ServletConfig
1. Out: This is used for writing content to the client (browser). It
has several methods which can be used for properly
formatting output message to the browser and for dealing
with the buffer.
<html>
<body>
<%! int data=50; %>
<% out.print(“Data=“+data); %>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
JSP Expressions
Using the JSP Expression you can compute a small expression,
always a single line, and get the result included in the HTML
which is returned to the browser. Using the code we have
previously written, let’s explore expressions.
Code
The time on the server is <%= new java.util.Date() %>
Output
The time on the server is Thursday January 21 07:21:43 GMT
2016.
Explanation
Here the “new java.util.Date()” is processed into the actual date
and time shown through HTML on the browser. Let’s explore
expressions through a couple of more examples.
The Expression: 25 multiplied to 4: <%= 25*4 %>
Using simple mathematical expressions you can
always compute and get answers to your
mathematical problems embedded in your HTML.
Syntax:
<jsp:action_name attribute="value" />
JSP Action Tags
JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
</jsp:include>
jsp:include action vs JSP include directive
jsp:useBean action
JSP useBean action lets you load a JavaBean component into the page
and use it later.
JSP useBean action allows you to reuse other Java classes.
The jsp:useBean action tag is used to locate or instantiate a bean class.
The syntax of JSP useBean action is as follows:
<jsp:useBean id= "instanceName" scope= "page | request | session | application"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= expression >" >
</jsp:useBean>
jsp:useBean action..
1.id: is used to identify the bean in the specified scope.
2.scope: represents the scope of the bean. It may be page, request, session or
application. The default scope is page.
1. page: specifies that you can use this bean within the JSP page. The
default scope is page.
2. request: specifies that you can use this bean from any JSP page that
processes the same request. It has wider scope than page.
3. session: specifies that you can use this bean from any JSP page in the
same session whether processes the same request or not. It has wider
scope than request.
4. application: specifies that you can use this bean from any JSP page in the
same application. It has wider scope than session.
3.class: instantiates the specified bean class (i.e. creates an object of the bean
class) but it must have no-arg or no constructor and must not be abstract.
4.type: provides the bean a data type if the bean already exists in the scope. It is
mainly used with class or beanName attribute. If you use it without class or
beanName, no bean is instantiated.
5.beanName: instantiates the bean using the java.beans.Beans.instantiate()
method.
jsp:setProperty action tags
Tags are used for developing web application with Java Bean
</body>
</html>
4.JSP - Directives
What are JSP Directives?
• JSP directives are the messages to JSP container. They
provide global information about an entire JSP page.
• JSP directives are used to give special instruction to a
container for translation of JSP to servlet code.
• In JSP life cycle phase, JSP has to be converted to a
servlet which is the translation phase.
• They give instructions to the container on how to
handle certain aspects of JSP processing
• Directives can have many attributes by comma
separated as key-value pairs.
• In JSP, directive is described in <%@ %> tags.
Syntax of Directive:
<%@ directive attribute="" %>
2.Include directive
3.Taglib directive
JSP Page directive
Syntax of Page directive:
<%@ page…%>
• It provides attributes that get applied to entire JSP page.
• It defines page dependent attributes, such as scripting language, error page, and
buffering requirements.
• It is used to provide instructions to a container that pertains to current JSP page.
Following are its list of attributes associated with page directive:
1. Language
2. Extends
3. Import
4. contentType
5. info
6. session
7. isThreadSafe
8. autoflush
9. buffer
10.IsErrorPage
11.pageEncoding
12.errorPage
13.isELIgonored
1.language:
It defines the programming language (underlying
language) being used in the page.
Syntax of language:
<%@ page language="value" %>
Here value is the programming language (underlying
language)
Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
Explanation of code: In the above example, attribute
language value is Java which is the underlying language
in this case. Hence, the code in expression tags would
be compiled using java compiler.
2.Extends: This attribute is used to extend (inherit) the
class like JAVA does
Syntax of extends:
<%@ page extends="value" %>
Here the value represents class from which it has to be
inherited.
Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page extends="demotest.DemoClass" %>
Explanation of the code: In the above code JSP is
extending DemoClass which is within demotest package,
and it will extend all class features.
3.Import: This attribute is most used attribute in page
directive attributes.It is used to tell the container to import
other java classes, interfaces, enums, etc. while generating
servlet code.It is similar to import statements in java classes,
interfaces.
Syntax of import:
<%@ page import="value" %>
Here value indicates the classes which have to be imported.
Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" import="java.util.Date"
pageEncoding="ISO-8859-1"%>
Explanation of the code:
In the above code, we are importing Date class from java.util
package (all utility classes), and it can use all methods of the
following class.
4.contentType:
•It defines the character encoding scheme i.e. it is used to
set the content type and the character set of the response
•The default type of contentType is "text/html;
charset=ISO-8859-1".
Syntax of the contentType:
<%@ page contentType="value" %>
Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
Explanation of the code:
In the above code, the content type is set as text/html, it
sets character encoding for JSP and for generated response
page
5.info
•It defines a string which can be accessed by
getServletInfo() method.
•This attribute is used to set the servlet description.
Syntax of info:
<%@ page info="value" %>
Here, the value represents the servlet information.
Example:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1" info="Guru Directive JSP"
pageEncoding="ISO-8859-1"%>
Explanation of the code:
In the above code, string "Guru Directive JSP" can be
retrieved by the servlet interface using getServletInfo()
6.Session
•JSP page creates session by default.
•Sometimes we don't need a session to be created in JSP, and hence, we
can set this attribute to false in that case.The default value of the session
attribute is true, and the session is created.
When it is set to false, then we can indicate the compiler to not create the
session by default.
Syntax of session:
<%@ page session="true/false"%>
Here in this case session attribute can be set to true or false
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-
1" session="false"%>
Explanation of code:
In the above example, session attribute is set to "false" hence we are
indicating that we don't want to create any session in this JSP
7.isThreadSafe:
•It defines the threading model for the generated servlet.
•It indicates the level of thread safety implemented in the page.
•Its default value is true so simultaneous
•We can use this attribute to implement SingleThreadModel interface in
generated servlet.
•If we set it to false, then it will implement SingleThreadModel and can
access any shared objects and can yield inconsistency.
Syntax of isThreadSafe:
<% @ page isThreadSafe="true/false" %>
Here true or false represents if synchronization is there then set as true
and set it as false.
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-
1" isThreadSafe="true"%>
Explanation of the code:
In the above code, isThreadSafe is set to "true" hence synchronization
will be done, and multiple threads can be used.
8.AutoFlush:
This attribute specifies that the buffered output should be flushed
automatically or not and default value of that attribute is true.
If the value is set to false the buffer will not be flushed automatically and
if its full, we will get an exception.
When the buffer is none then the false is illegitimate, and there is no
buffering, so it will be flushed automatically.
Syntax of autoFlush:
<% @ page autoFlush="true/false" %>
Here true/false represents whether buffering has to be done or not
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-
1" autoFlush="false"%>
Explanation of the code:
In the above code, the autoflush is set to false and hence buffering won't
be done and it has manually flush the output.
9.Buffer:
•Using this attribute the output response object may be buffered.
•We can define the size of buffering to be done using this attribute and
default size is 8KB.
•It directs the servlet to write the buffer before writing to the response
object.
Syntax of buffer:
<%@ page buffer="value" %>
Here the value represents the size of the buffer which has to be defined.
If there is no buffer, then we can write as none, and if we don't mention
any value then the default is 8KB
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-
1" buffer="16KB"%>
Explanation of the code:
In the above code, buffer size is mentioned as 16KB wherein the buffer
would be of that size
10.isErrorPage:
•It indicates that JSP Page that has an errorPage will be checked in
another JSP page
•Any JSP file declared with "isErrorPage" attribute is then capable to
receive exceptions from other JSP pages which have error pages.
•Exceptions are available to these pages only.
•The default value is false.
Syntax of isErrorPage:
<%@ page isErrorPage="true/false"%>
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-
1" isErrorPage="true"%>
Explanation of the code:
In the above code, isErrorPage is set as true. Hence, it will check any
other JSPs has errorPage (described in the next attribute) attribute set and
it can handle exceptions.
index.jsp
<form action="process.jsp">
No1:<input type="text" name="n1" /><br/><br/>
No1:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/>
</form>
process.jsp error.jsp
<%@ page errorPage="error.jsp" %> <%@ page isErrorPage="true" %>
<% <h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
index.jsp
<form action="process.jsp">
No1:<input type="text" name="n1" /><br/><br/>
No1:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/>
</form>
process.jsp error.jsp
<%@ page errorPage="error.jsp" %> <%@ page isErrorPage="true" %>
<% <h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
11.PageEncoding:
The "pageEncoding" attribute defines the character
encoding for JSP page.
The default is specified as "ISO-8859-1" if any other is
not specified.
Syntax of pageEncoding:
<%@ page pageEncoding="vaue" %>
Here value specifies the charset value for JSP
Example:
<%@ page language="java" contentType="text/html;"
pageEncoding="ISO-8859-1" isErrorPage="true"%>
Explanation of the code:
In the above code "pageEncoding" has been set to default
charset ISO-8859-1
12.errorPage:
This attribute is used to set the error page for the JSP page if
JSP throws an exception and then it redirects to the exception
page.
Syntax of errorPage:
<%@ page errorPage="value" %>
Here value represents the error JSP page value
Example:
<%@ page language="java" contentType="text/html;"
pageEncoding="ISO-8859-1"
errorPage="errorHandler.jsp"%>
Explanation of the code:
In the above code, to handle exceptions we have
errroHandler.jsp
13.isELIgnored:
•IsELIgnored is a flag attribute where we have to decide whether to
ignore EL tags or not.
•Its datatype is java enum, and the default value is false hence EL is
enabled by default.
Syntax of isELIgnored:
<%@ page isELIgnored="true/false" %>
Here, true/false represents the value of EL whether it should be ignored
or not.
Example:
<%@ page language="java" contentType="text/html;"
pageEncoding="ISO-8859-1" isELIgnored="true"%>
Explanation of the code:
In the above code, isELIgnored is true and hence Expression Language
(EL) is ignored here.
In the below example we are using four attributes(code line 1-2)
Example with four attributes
1.<%@ page language="java" contentType="text/html;"
pageEncoding="ISO-8859-1"
2.isELIgnored="false"%>
3.<%@page import="java.util.Date" %>
5.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6.<html>
7.<head>
8.<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
9.<title>Directive Guru JSP1</title>
10.</head>
11.<body>
12.<a>Date is:</a>
13.<%= new java.util.Date() %>
14.</body>
15.</html>
Explanation of the code:
Code Line 1-2: Here we have defined four attributes i.e.
•Language: It is set as Java as programming language
•contentType: set as text/html to tell the compiler that html has to be
format
•pageEncoding: default charset is set in this attribute
•isELIgnored: Expression Tag is false hence it is not ignored
Code Line 3: Here we have used import attribute, and it is importing
"Date class" which is from Java util package, and we are trying to
display current date in the code.
When you execute the above code, you will get the following output
JSP Include directive
• The include directive is used to include a file during the
translation phase.
• This directive tells the container to merge the content
of other external files with the current JSP during the
translation phase.
• You may code include directives anywhere in your JSP
page.
• The filename in the include directive is actually a
relative URL.
• Syntax
– <%@ include file = "relative url" >
– <jsp:directive.include file = "relative url" />
Example: Include directive
• include directive
• A common header and footer with multiple pages of content.
– header.jsp
– footer.jsp and
– main.jsp
main.jsp
Main.jsp
<%@ include file = "header.jsp" %>
<center>
<p>Thanks for visiting my page.</p>
</center>
<%@ include file = "footer.jsp" %>
JSP Taglib directive
• The JSP taglib directive is used to define a tag
library that defines many tags.
• We use the TLD (Tag Library Descriptor) file to
define the tags.
• In the custom tag section we will use this tag
so it will be better to learn it in custom tag.
• Syntax JSP Taglib directive
– <%@ taglib uri="uriofthetaglibrary"
prefix="prefixoftaglibrary" %>
Example: Taglib directive
<html>
<body>
<mytag:currentDate/>
</body>
</html>
Custom Tags in JSP
Custom Tags
• Custom tags are user-defined tags.
• They eliminates the possibility of scriptlet tag and separates the
business logic from the JSP page.
• The same business logic can be used many times by the use of
custom tag.
Advantages of Custom Tags: The key advantages of Custom tags are
as follows
1. Eliminates the need of scriptlet tag The custom tags eliminates
the need of scriptlet tag which is considered bad programming
approach in JSP.
2. Separation of business logic from JSP The custom tags separate
the the business logic from the JSP page so that it may be easy to
maintain.
3. Re-usability The custom tags makes the possibility to reuse the
same business logic again and again.
Syntax to use custom tag
There are two ways to use the custom tag. They
are given below:
<prefix:tagname attr1=value1....attrn=valuen />
public static int SKIP_PAGE it skips the JSP page content after the
custom tag.
Methods of Tag interface
The methods of the Tag interface are as follows:
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<uri>http://tomcat.apache.org/example-taglib</uri>
<tag>
<name>today</name>
<tag-class>com.javatpoint. mytag.MyTagHandler</tag-class>
</tag>
</taglib>
3) Create the JSP file
Let's use the tag in our jsp file. Here, we are specifying
the path of tld file directly. But it is recommended to
use the uri name instead of full path of tld file. We will
learn about uri later.
It uses taglib directive to use the tags defined in the tld
file.
File: index.jsp
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
Current Date and Time is: <m:today/>
OUTPUT
Attributes in JSP Custom Tag
There can be defined too many attributes for any
custom tag. To define the attribute, you need to
perform two tasks:
Define the property in the TagHandler class with the
attribute name and define the setter method define the
attribute element inside the tag element in the TLD file
Let's understand the attribute by the tag given below:
<m:cube number="4"></m:cube>
return SKIP_BODY;
}
}
mytags.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
index.jsp
<uri>http://tomcat.apache.org/example-taglib</uri>
<%@ taglib uri="W
<description>A simple tab library for the examples</description>
EB-
INF/mytags.tld" pre
<tag>
fix="m" %>
<name>cube</name>
Cube of 4 is: <m:cu
<tag-class>com.javatpoint.taghandler.CubeNumber</tag-class>
be number="4"></
<attribute>
m:cube>
<name>number</name>
<required>true</required>
</attribute>
</tag>
</taglib>
Iteration using JSP Custom Tag
We can iterate the body content of any tag using
the doAfterBody() method of IterationTag interface.
Here we are going to use the TagSupport class which
implements the IterationTag interface.
For iterating the body content, we need to use
the EVAL_BODY_AGAIN constant in the doAfterBody()
method.
Example of Iteration using JSP Custom Tag
In this example, we are going to use the attribute in the
custom tag, which returns the power of any given
number. We have created three files here
•index.jsp
•PowerNumber.java
•mytags.tld
index.jsp
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>
729
PowerNumber.java
package com.javatpoint.taghandler;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name> <attribute>
<uri>http://tomcat.apache.org/example-taglib</uri> <name>number</name>
<description>A simple tab library for the examples <required>true</required>
</description> </attribute>
<tag> <attribute>
<name>power</name> <name>power</name>
<tag-class>com.javatpoint.taghandler.PowerNumber <required>true</required>
</tag-class> </attribute>
</tag>
</taglib>
Looping using Iteration Tag (creating tag for loop)
Let's create a loop tag that iterates the body content of this tag.
File: index.jsp
<!DOCTYPE html PUBLIC "-
//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
</body>
</html>
File: mytags.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-
//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<attribute>
<short-name>abc</short-name>
<name>start</name>
<required>true</required>
<uri>sssuri</uri>
</attribute>
<tag>
<name>loop</name>
<attribute>
<tag-class>com.javatpoint.customtag.Loop</tag-class>
<name>end</name>
<required>true</required>
</attribute>
</tag>
</taglib>
File: Loop.java
package com.javatpoint.customtag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
}
File: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="h
ttp://java.sun.com/xml/ns/javaee/web-
app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/java
ee http://java.sun.com/xml/ns/javaee/web-
app_3_0.xsd" id="WebApp_ID" version="3.0">
<jsp-config>
<taglib>
<taglib-uri>sssuri</taglib-uri>
<taglib-location>/WEB-INF/mytags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
OUTPUT
Custom URI in JSP Custom Tag
We can use the custom URI, to tell the web container about the
tld file. In such case, we need to define the taglib element in the
web.xml. The web container gets the information about the tld
file from the web.xml file for the specified URI.
In this example, we are going to use the custom uri in the JSP file.
For this application, we need to focus on 4 files.
1. index.jsp
2. web.xml
3. mytags.tld
4. PrintDate.java
index.jsp
1.<%@ taglib uri="mytags" prefix="m" %>
2.Today is: <m:today></m:today>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<jsp-config>
<taglib>
<taglib-uri>mytags</taglib-uri>
<taglib-location>/WEB-INF/mytags.tld</taglib-location>
</taglib>
</jsp-config>
</web-app> `
mytags.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<uri>mytags</uri>
<description>A simple tab library for the examples</description>
<tag>
<name>today</name>
<tag-class>com.javatpoint.taghandler.PrintDate</tag-class>
</tag>
</taglib>
PrintDate.java
package com.javatpoint.taghandler;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
return SKIP_BODY;
}
}