It is a mechanism to create dynamic web pages by using servlet behind it.
Whenever a user comes to access the JSP then the web server converts the JSP
into a servlet for processing the request and providing response.
Features of JSP
- Interpreted:- A JSP does not need
explicit compilation. A programmer needs to create the JSP and store it into
the context folder. Whenever the JSP gets accessed by the first user then
the web server creates a servlet and complies it to create required class
file. The generated servlet gets loaded into the servlet container for
processing request and for providing reponse. Hence the JSP does not need
explicit compilation.
- Object based:- JSP cannot implement
inheritance and polymorphism. It can use a set of predefined objects such as
request, response and out without creating those explicitly.
- Tag based:- It can use a set of
tags to perform any task in the server. These tags can be predefined or user
defined.
- User friendly:- A JSP contains java
code inside html tags. The required HTML tags can be generated with the help
of any HTML editor. The required java code can be inserted into the
generated html tags. This makes the JSP creation faster and easier.
Possible content of a JSP
- Directive:- This can be used in a
JSP to include or import any external file or package into the JSP.
Syntax:
< %@ directiveName attribute="value" %>
Ex:- <%@ page import="java.sql.*" %>
- Declaration section:- This section
can be used in a JSP to declare page scope variables and methods. This
section can appear before any html tags.
Syntax:
<%! Declaration of variables or methods %>
Ex:- <%! int x=100; %>
- HTML tags:- These tags appear in a
JSP like any ordinary HTML file. These can be used to represent any content
to the user.
- Scriptlets:- This section contains
executable java code. This can appear in any place for any number of times
in a JSP.
Syntax:
<% Java code %>
- JSP tags:- This tags can appear to
perform any task in the server. This tags cannot appear inside
Scriptlets.This tags follow xml syntax.
Ex:- <JSP:forward page="./test.JSP" />
- Expression:- This can appear in a
JSP to print value of a variable or result of an expression into the
response.
Syntax:
<% = Expression or variable %>
Ex:- <H1>Welcome<% = str %></H1>
Creation process of JSP
- Create a file by using .JSP extension in
the context folder.
- Provide the required HTML tags as any
ordinary HTML file.
- Provide the required java code inside the HTML tags by using the following syntax below
<% Java code %>
Ex:- Program to show Hello JSP 5 times .
Hello.JSP
<html>
<body bgcolor="pink">
<%
for(int i=0;i<5;i++)
{
out.println("<h2>Hi:</h2>");
%>
<h2>Hello JSP</h2>
<%
}
%>
</body>
</html>
Running the application
Run the tomcat server then write the below line in the URL
http://localhost:8081/JSP/
Here JSP is the Context path, which we mentioned in the server.xml file, which
is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)
directory.
After giving the URL a set a listing will come, here only one appears
As Hello.JSP,click it
http://localhost:8081/JSP/Hello.JSP
Output
Thanks for reading