You are on page 1of 2

Introduction to Java Server Pages JSP Tutorial

Introduction to JSP
JSP is a server side technology which helps to create a webpage dynamically usin
g java as the programming language.
JSP is a specification from Sun Microsystems. It is an extension to Servlet API.
A JSP Example
<%-- JSP comment --%>
<HTML>
<HEAD>
<TITLE>MESSAGE</TITLE>
</HEAD>
<BODY>
<%out.print("Hello, Sample JSP code");%>
</BODY>
</HTML>
Let us start learning with a simple JSP program. All JSP programs are stored as
a .jsp files. Above is a simple JSP code, MyJSP.jsp which prints Hello, Sample J
SP code. As discussed JSP is used for creating dynamic webpages. Dynamic webpage
s can have two types of contents static & dynamic content.
The static contents can have text-based formats such as HTML, XML etc and the d
ynamic contents are generated by JSP elements.
Analysis of the above code
1) The line <%JSP Comment%> represents the JSP element called JSP Comment, While a
dding comments to a JSP page you can use this tag, we will discuss this in detai
l in coming posts.
Note: JSP Comments must starts with a tag <% and ends with %>
2) Head, Title and Body tags are HTML tags They are HTML tags, frequently used f
or static web pages. Whatever content they have is delivered to client(Web brows
er) as such.
3) <%out.print( Hello, Sample JSP code );%> is a JSP element, which is known as Sc
riptlet. Scriptlets can contain Java codes. syntax of scriptlet is: <%Executabl
e java code%>. As the code in Scriptlets is java statement, they must end with a
semicolon(;). out.print( Hello, Sample JSP code ) is a java statement, which prin
ts Hello, Sample JSP code.
Servlet Vs JSP
Like JSP, Servlets are also used for generating dynamic webpages. Below is a com
parison between them
Servlets
Servlets are Java programs which supports HTML tags too.
Generally used for developing business layer of an enterprise application.
Servlets are created and maintained by Java developers.
On the other hand, JSP
JSP program is a HTML code which supports java statements too.
Used for developing presentation layer of an enterprise application
Frequenly used for desiging websites and used for web designers.
Advantages of JSP

JSP has all the advantages that a servlet has, like: Better performance than CG
I Built in session features, it also inherits the the features of java technolo
gy like multithreading, exception handling, Database connectivity,etc.
JSP Enables the separation of content generation from content presentation. Whic
h makes it more flexible.
With the JSP, it is now easy for web designers to show case the information what
is needed.
Web Application Programmers can concentrate on how to process/build the informat
ion.
Architecture of a JSP Application
Based on the location where request processing happens (Servlet OR JSP(java serv
er pages)) there are two architectures for JSP. They are Model1 Architecture & M
odel2 Architecture.
1) Model1 Architecture: In this Model, JSP plays a key role and it is responsibl
e for of processing the request made by client. Client (Web browser) makes a req
uest, JSP then creates a bean object which then fulfills the request and pass th
e response to JSP. JSP then sends the response back to client. Unlike Model2 arc
hitecture in this Model, most of the processing is done by JSP itself.
JSP-architecture-Model1
2) Model2 Architecture: In this Model Servlet plays a major role and it is respo
nsible for processing the clients(web browser) request. Presentation part (GUI pa
rt) will be handled by JSP and it performs this with the help of bean as shown
in below figure. The servlet acts as controller and in charge of request process
ing. It creates the bean objects if required by the jsp page and calls the respe
ctive jsp page. The jsp handles the presentation part by using the bean object.
In this Model, JSP doesnt do any processing, Servlet creates the bean Object and
calls the JSP program as per the request made by client.
JSP-architecture-Model2

You might also like