KEMBAR78
JSP Notes1 | PDF | Java (Programming Language) | World Wide Web
0% found this document useful (0 votes)
12 views4 pages

JSP Notes1

The document provides an overview of JavaServer Pages (JSP), highlighting its role in creating web applications by separating presentation and business logic. It contrasts JSP with Servlets, noting that JSP is more suited for web designers while Servlets are for Java developers. Additionally, it outlines the JSP lifecycle, architecture models, and the translation of JSP into Servlets.

Uploaded by

Riya Yohannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

JSP Notes1

The document provides an overview of JavaServer Pages (JSP), highlighting its role in creating web applications by separating presentation and business logic. It contrasts JSP with Servlets, noting that JSP is more suited for web designers while Servlets are for Java developers. Additionally, it outlines the JSP lifecycle, architecture models, and the translation of JSP into Servlets.

Uploaded by

Riya Yohannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Advanced Java

Introduction to JSP
JSP
JSP technology is used to create web application. It focuses more on presentation logic of
the web apllication.JSP pages are easier to maintain then a Servlet. JSP pages are opposite of
Servlets. Servlet adds HTML code inside Java code while JSP adds Java code inside HTML.
Everything a Servlet can do, a JSP page can also do it.
JSP enables us to write HTML pages containing tags that run powerful Java programs. JSP
separates presentation and business logic as Web designer can design and update JSP pages
without learning the Java language and Java Developer can also write code without
concerning the web design.

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 JSP code. As discussed JSP
is used for creating dynamic web pages. Dynamic web pages can have two types of contents
– static & dynamic content.
The static contents can have text-based formats such as HTML, XML etc and the dynamic
contents are generated by JSP elements.
Analysis of the above code –
1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While
adding comments to a JSP page you can use this tag, we will discuss this in detail 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 for static
web pages. Whatever content they have is delivered to client(Web browser) as such.
3) <%out.print(“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet.
Scriptlets can contain Java codes. syntax of scriptlet is:<%Executable 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 prints“ Hello, Sample JSP code”.

Difference between JSP and Servlets


Like JSP, Servlets are also used for generating dynamic webpages. Below is
a comparison between them –
Servlets –
1. Servlets are Java programs which supports HTML tags too.
2. Generally used for developing business layer of an enterprise application.
3. Servlets are created and maintained by Java developers.
On the other hand, JSP –

Contact: enquiry@vertexitservices.com Call:9763 9763 33, 020-65009763 | 1


www.vertexitservices.com
Advanced Java

1. JSP program is a HTML code which supports java statements too.


2. Used for developing presentation layer of an enterprise application
3. Frequently used for designing websites and used for web designers.

Advantages of JSP
1. JSP has all the advantages that a servlet has, like: Better performance than CGI Built
in session features, it also inherits the the features of java technology like –
multithreading, exception handling, Database connectivity,etc.
2. JSP Enables the separation of content generation from content presentation. Which
makes it more flexible.
3. With the JSP, it is now easy for web designers to show case the information what is
needed.
4. Web Application Programmers can concentrate on how to process/build the
information.

Architecture of a JSP Application


Based on the location where request processing happens (Servlet OR JSP(java server pages))
there are two architectures for JSP. They are – Model1 Architecture & Model2 Architecture.
1) Model1 Architecture: In this Model, JSP plays a key role and it is responsible for of
processing the request made by client. Client (Web browser) makes a request, JSP then
creates a bean object which then fulfills the request and pass the response to JSP. JSP then
sends the response back to client. Unlike Model2 architecture in this Model, most of the
processing is done by JSP itself.

2) Model2 Architecture: In this Model Servlet plays a major role and it is responsible for
processing the client’s(web browser) request. Presentation part (GUI part) 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 processing. It creates the bean objects if required by
the jsp page and calls the respective jsp page. The jsp handles the presentation part by using
the bean object. In this Model, JSP doesn’t do any processing, Servlet creates the bean
Object and calls the JSP program as per the request made by client.

Contact: enquiry@vertexitservices.com Call:9763 9763 33, 020-65009763 | 2


www.vertexitservices.com
Advanced Java

Lifecycle of JSP
A JSP page is converted into Servlet in order to service requests. The translation of a JSP
page to a Servlet is called Lifecycle of JSP. JSP Lifecycle consists of following steps.
1. Translation of JSP to Servlet code.
2. Compilation of Servlet to bytecode.
3. Loading Servlet class.
4. Creating servlet instance.
5. Initialization by calling jspInit() method
6. Request Processing by calling _jspService() method
7. Destroying by calling jspDestroy() method

Web Container translates JSP code into a servlet class source(.java) file, then compiles that
into a java servlet class. In the third step, the servlet class bytecode is loaded using
classloader. The Container then creates an instance of that servlet class.
Contact: enquiry@vertexitservices.com Call:9763 9763 33, 020-65009763 | 3
www.vertexitservices.com
Advanced Java

The initialized servlet can now service request. For each request the Web Container call
the _jspService()method. When the Container removes the servlet instance from service, it
calls the jspDestroy() method to perform any required clean up.

What happens to a JSP when it is translated into Servlet


Let's see what really happens to JSP code when it is translated into Servlet
<html>
<head>
<title>My First JSP Page</title>
</head>
<%
int count = 0;
%>
<body>
Page Count is:
<% out.println(++count); %>
</body>
</html>
The above JSP page becomes this Servlet
public class hello_jsp extends HttpServlet
{
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out = response.getWriter();
response.setContenType("text/html");
out.write("<html><body>");
int count=0;
out.write("Page count is:");
out.print(++count);
out.write("</body></html>");

}
}

Contact: enquiry@vertexitservices.com Call:9763 9763 33, 020-65009763 | 4


www.vertexitservices.com

You might also like