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.
< %@ 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.
<%! 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.
<% 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.
<% = 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 .
Running the application
Run the tomcat server then write the below line in the URL
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)
After giving the URL a set a listing will come, here only one appears
As Hello.JSP,click it
Thanks for reading