0% found this document useful (0 votes)
30 views

Java Server Pages (JSP)

- Java Server Pages (JSP) is a server-side technology that is used to create dynamic web content. It allows Java code to be embedded within HTML pages. - The JSP lifecycle involves translating a JSP page into a servlet, compiling the servlet, loading and initializing the servlet class, processing requests by invoking the service method, and finally destroying the servlet instance. - JSP syntax includes scripting elements like scriptlets, declarations, and expressions to embed Java code. It also uses directives, comments, and control flow statements. Common directives include page, include, and taglib.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Server Pages (JSP)

- Java Server Pages (JSP) is a server-side technology that is used to create dynamic web content. It allows Java code to be embedded within HTML pages. - The JSP lifecycle involves translating a JSP page into a servlet, compiling the servlet, loading and initializing the servlet class, processing requests by invoking the service method, and finally destroying the servlet instance. - JSP syntax includes scripting elements like scriptlets, declarations, and expressions to embed Java code. It also uses directives, comments, and control flow statements. Common directives include page, include, and taglib.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

Java Server Pages(JSP)

Java Server Pages


• It stands for Java Server Pages.
• It is a server side technology.
• It is used for creating web application.
• It is used to create dynamic web content.
• In this JSP tags are used to insert JAVA code
into HTML pages.
• JSP architecture is a 3 tier architecture.
• It has a Client, Web Server, and Database.
• The client is the web browser or application on the
user side. Web Server uses a JSP Engine i.e; a container
that processes JSP.
• For example, Apache Tomcat has a built-in JSP Engine.
JSP Engine intercepts the request for JSP and provides
the runtime environment for the understanding and
processing of JSP files.
• It reads, parses, build Java Servlet, Compiles and
Executes Java code, and returns the HTML page to the
client.
• JSP is first converted into a servlet by JSP container
before processing the client’s request
• The webserver has access to the Database.
• The client navigates to a file ending with the 
.jsp extension and the browser initiates an
HTTP request to the webserver. For example,
the user enters the login details and submits
the button. The browser requests a status.jsp
page from the webserver.
• If the compiled version of JSP exists in the web
server, it returns the file. Otherwise, the
request is forwarded to the JSP Engine. This is
done by recognizing the URL ending
with .jsp extension.
• The JSP Engine loads the JSP file and translates
the JSP to Servlet(Java code). This is done by
converting all the template text into println()
statements and JSP elements to Java code.
This process is called translation.
• The JSP engine compiles the Servlet to an
executable .class file. It is forwarded to the
Servlet engine. This process is
called compilation or request processing
phase.
• The .class file is executed by the Servlet
engine which is a part of the Web Server. The
output is an HTML file. The Servlet engine
passes the output as an HTTP response to the
webserver.
• The web server forwards the HTML file to the
client’s browser.
Life cycle of JSP

• A Java Server Page life cycle is defined as the


process that started with its creation which
later translated to a servlet and afterward
servlet lifecycle comes into play.
• This is how the process goes on until its
destruction. 
• Translation of JSP page to Servlet : 
• This is the first step of the JSP life cycle. This
translation phase deals with the Syntactic
correctness of JSP. Here test.jsp file is
translated to test.java. 
• Compilation of JSP page : 
• Here the generated java servlet file (test.java)
is compiled to a class file (test.class). 
• Classloading : 
• Servlet class which has been loaded from the
JSP source is now loaded into the container. 
• Instantiation : 
• Here an instance of the class is generated. The
container manages one or more instances by
providing responses to requests. 
• Initialization : 
• jspInit() method is called only once during the life
cycle immediately after the generation of Servlet
instance from JSP. 
• initialization is performed only once and as with
the servlet init method, you generally initialize
database connections, open files, and create
lookup tables in the jspInit method.
• public void jspInit(){ // Initialization code... }
• Request processing : 
• _jspService() method is used to serve the
raised requests by JSP. It takes request and
response objects as parameters. This method
cannot be overridden. 
• void _jspService(HttpServletRequest request,
HttpServletResponse response) { // Service
handling code... }
• JSP Cleanup : 
• In order to remove the JSP from the use by
the container or to destroy the method for
servlets jspDestroy()method is used. This
method is called once, if you need to perform
any cleanup task like closing open files,
releasing database connections jspDestroy()
can be overridden.
• public void jspDestroy() { // Your cleanup code
goes here. }
JSP Syntax 

• Syntax – Syntax is the coding structure to


represent elements.
• JSP Elements
• JSP scripting elements
• Directives
• Actions
JSP Scripting Elements

• Scriptlets
• Declarations
• Expressions
Scriptlets

• Scriptlets embed Java code in HTML for JSP


code. 
• is written between <%—-%> are called
scriptlet tags.
• <%  java source code %>  
• <html>  
• <body>  
• <% out.print("welcome to jsp"); %>  
• </body>  
• </html>  
• Ex 2:<% int a=1;
• int b=2;
• int c=3;
• out.println("The number is " + a*b*c);
• %>
• Ex 3:<%  
• String name=request.getParameter("uname");
• out.print("welcome "+name);  
• %>
JSP Declarations
• A declaration declares one or more variables
or methods that you can use in Java code later
in the JSP file. You must declare the variable
or method before you use it in the JSP file.
• <%!  field or method declaration %> 
• <%! int i = 0; %>
• <%! int a, b, c; %>
• <%! Circle a = new Circle(2.0); %>  
• <html>  
• <body>  
• <%! int data=50; %>  
• <%= "Value of the variable is:"+data %>  
• </body>  
• </html>  
• <html>  
• <body>  
• <%!   
• int cube(int n){  
• return n*n*n*;  
• }  
• %>  
• <%= "Cube of 3 is:"+cube(3) %>  
• </body>  
• </html>  
JSP expression tag
• The code placed within JSP expression
tag is written to the output stream of the
response.
• So you need not write out.print() to write
data.
• It is mainly used to print the values of variable
or method.
• <%=  statement %>  
• <html>  
• <body>  
• <%= "welcome to jsp" %>  
• </body>  
• </html> 
• <html>  
• <body>  
• <
%= "Welcome "+request.getParameter("unam
e") %>  
• </body>  
• </html>  
JSP Comments:
• JSP comment marks text or statements that
the JSP container should ignore. A JSP
comment is useful when you want to hide or
"comment out" part of your JSP page.
• <%-- This is JSP com m ent --%>
• <htm l>
• <head><title>A Com m ent
Test</title></head>
• <body>
• <h2>A Test of Com m ents</h2>
• <%-- This com m ent will not be visible in the
page source --%>
• </body>
• </htm l>
Control-Flow Statements:
• <%! int day = 3; %>
• <htm l>
• <head><title>IF...ELSE Exam ple</title></head>
• <body>
• <% if (day == 1 | day == 7) { %>
• <p> Today is weekend</p>
• <% } else { %>
• <p> Today is not weekend</p>
• <% } %> </body> </htm l>
• <%! int day = 1; %>
• <htm l>
• <head><title>SWITCH...CASE Exam
ple</title></head>
• <body>
• <%
• switch(day) {
• case 0:
• out.println("It\'s Sunday.");
• break;
• case 1:
• out.println("It\'s Monday.");
• break;
• case 2:
• out.println("It\'s Tuesday.");
• break;
• default:
• out.println("It's Saturday.");
• }
• %> </body> </htm l>
• <htm l>
• <head><title>FOR LOOP Exam
ple</title></head>
• <body>
• <%for ( fontSize = 1; fontSize <= 3; fontSize++)
{ %>
• <font color="green" size="<%= fontSize %>">
• JSP Tutorial
• </font><br />
• <%}%> </body> </html>
• <%! int fontSize; %>
• <htm l>
• <head><title>WHILE LOOP Exam
ple</title></head>
• <body>
• <%while ( fontSize <= 3){ %>
• <font color="green" size="<%= fontSize %>">
• JSP Tutorial
• </font><br />
• <%fontSize++;%>
• <%}%> </body> </htm l>
JSP directives
• JSP directives are the elements of a JSP source
code that guide the web container on how to
translate the JSP page into it’s respective
servlet.
• Syntax :
• <%@ directive attribute = "value"%>
• Directives can have a number of attributes
which you can list down as key-value
pairs and separated by commas. 
Different types of JSP directives
• Page Directives
• Include directive
• taglib directive
Page Directives 
• JSP page directive is used to define the
properties applying the JSP page, such as the
size of the allocated buffer, imported
packages and classes/interfaces, defining
what type of page it is etc..
• <%@page attribute = "value"%>
Different properties/attributes : 
• import: This tells the container what
packages/classes are needed to be imported
into the program.
• <%@page import = "value"%>
• <%-- JSP code to demonstrate how to use
page
• directive to import a package --%>

• <%@page import = "java.util.Date"%>


• <%Date d = new Date();%>
• <%=d%>
• contentType: This defines the format of data
that is being exchanged between the client
and the server. 
• <%@page contentType="value"%>
• <%-- JSP code to demonstrate how to use
• page directive to set the type of content --%>

• <%@page contentType = "text/html" %>


• <% = "This is sparta" %>
• buffer: Defines the size of the buffer that is
allocated for handling the JSP page. The size is
defined in Kilo Bytes.
• <%@page buffer = "size in kb"%>
• language: Defines the scripting language used
in the page. By default, this attribute contains
the value ‘java’.
• errorPage: Defines which page to redirect to, in
case the current page encounters an
exception. 
• <%@page errorPage = "true/false"%>
• isErrorPage: It classifies whether the page is
an error page or not. By classifying a page as
an error page, it can use the implicit object
‘exception’ which can be used to display
exceptions that have occured.
Include directive : 

• JSP include directive is used to include other


files into the current jsp page. These files can
be html files, other sp files etc. The advantage
of using an include directive is that it allows
code re-usability.
• <@%include file = "file location"%>
a.html
• <h1>This is the content of a.html</h1>
index.jsp 

• <% = Local content%>


• <%@include file = "a.html"%>
• <% = local content%>
Taglib Directive :
• The taglib directive is used to mention the
library whose custom-defined tags are being
used in the JSP page.
• <%@ taglib uri="uri" prefix="value"%>
• <%@ taglib prefix="gurutag"
uri="http://java.sun.com/jsp/jstl/core" %>
• <gurutag:hello/>

You might also like