Java Server Page(JSP)
Introduction to JSP
• JSP is a server side technology. It is used for creating dynamic web
applications, using java as programming language.
• This is mainly used for implementing presentation layer (GUI Part) of
an application. A complete JSP code is more like a HTML with bits of
java code in it.
• A JSP page consists of HTML tags(generates Static Content) and JSP
tags (generates Dynamic Content).
• The JSP pages are easier to maintain than Servlet because we can
separate designing( Presentation Logic) and development( Business
Logic).
• JSP pages are opposite of Servlets as a servlet adds HTML code inside
Java code, while JSP adds Java code inside HTML using JSP tags.
• Easy to maintain
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.
• Fast Development: No need to recompile and redeploy
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.
• Less code than Servlet
In JSP, we can use many tags such as action tags, implicit objects,
custom tags, etc. that reduces the code.
To Work with JSP: (Environment Setup)
1. We need to install Java Software Development Kit (SDK)
2. After successful installation we need to set the PATH and
JAVA_HOME environment variables.
PATH=“C:\Program Files\Java\jdk1.8.0_191\bin;.;”
JAVA_HOME=“C:\Program Files\Java\jdk1.8.0_191”.
3. After that, we need to install Tomcat server( ignore this if
XAMPP already installed)
The Anatomy of JSP
• The JSP is almost like a regular web page with the inclusion of
dynamic behavior through JSP Elements.
• A JSP page consists of HTML tags(generates Static Content) and
JSP tags (generates Dynamic Content).
• Hence We can say that a JSP page has two components
JSP Elements(tags):
• These are the elements that are understood and translated by
JSP Container. These elements are used to generate dynamic
content.
Template Data(HTML tags):
• These are the elements that are not understood and translated
by JSP Container. The template data is used to generate static
content.
A Simple JSP Program:
1. Open text editor ( notepad,edit+,notepad++,..)
2. Write jsp program and save it with .jsp extension in the
following location
In Windows
C:/xampp/tomcat/webapps/foldername/filename.jsp
In Linux( ubuntu )
var/lib/tomcat/webapps/foldername/filename.jsp
3. To execute, Open any browser and give following as URL
Port No of Tomcat Server
localhost:8080/foldername/filename.jsp
Example:
“sample.jsp”
<html>
<head>
<title>Sample JSP</title>
</head>
<body>
<% out.println("Welcome to JSP"); %>
</body>
</html>
Output:
Example: To display Current System Date and Time
“printdate.jsp”
<%@page import="java.util.Date"%>
<html>
<body>
<%
Date d=new Date();
out.println("Today Date is :"+d);
%>
</body>
</html>
Output:
JSP Processing
• Any Web server needs a container to run any web component
(or) to provide interface to run web components.
• The JSP also needs a JSP container to execute or process a JSP
page.
• In general, a JSP container acts as an interface between JSP
page and Server. i.e. it receives all JSP requests and generates
the responses.
Java class
JSP Page Translated Servlet Code Complied
to to file
(.jsp file) (.java file)
(.class file)
JSP Processing
• In JSP processing there are two phases ,those are
– Translation Phase
– Request Processing Phase
• In translation phase, The JSP page converted into servlet
code or into ( .java file).
• In request processing phase, Servlet code can be converted
into .class file to process the request.
Fig: JSP Processing
Sample.jsp
2.Reads
S
1.Request E 3.Generate
R .java file Sample.java
CLIENT
V
6.Response
E 4.Converted into .class file
R
5.Execute request
& Sample.class
Generate response