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

Intro JSPTechnology

Uploaded by

Kripa Pattiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Intro JSPTechnology

Uploaded by

Kripa Pattiwala
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Java Server Pages

V.M.Vasava
Lect. IT,GPG
Agenda
Introduction about JSP
JSP Lifecycle
JSP vs Servlet
Important tags of JSP
JSP
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.
It is an advanced version of Servlet Technology.
It is a Web based technology helps us to create
dynamic and platform independent web pages.
In this, Java code can be inserted in HTML/ XML pages
or both.
JSP is first converted into servlet by JSP container
before processing the client’s request.
Java Server Pages
Servlets are pure Java programs. They introduce
dynamism into web pages by using programmatic
content.
JSP technology is an extension/wrapper over the Java
servlet technology.
JSP are text based documents.
We will focus only on JSP since it subsumes the servlet
technology.
Two major components of JSP:
 Static content: provided by HTML or XML

 Dynamic content: generated by JSP tags and

scriplets written in Java language to encapsulate


the application logic.
JSP Lifecycle
JSP Lifecycle
Following steps are involved in JSP life cycle:
1. Translation of JSP page to Servlet
2. Compilation of JSP page(Compilation of JSP into test.java)
3. Classloading (test.java to test.class)
4. Instantiation(Object of the generated Servlet is created)
5. Initialization(jspInit() method is invoked by the container)
6. Request processing(_jspService()is invoked by the container)
7. JSP Cleanup (jspDestroy() method is invoked by the container)
Translation of JSP page to Servlet :
This is the first step of JSP life cycle. This translation phase
deals with Syntactic correctness of JSP. Here test.jsp file is
transllated to test.java.
Compilation of JSP page :
Here the generated java servlet file (test.java) is compiled to a
class file (test.class).
JSP LifeCycle
Continued..
Classloading :
Servlet class which has been loaded from JSP source is now
loaded into container.
Instantiation :
Here instance of the class is generated. The container
manages one or more instance by providing response to
requests.
Initialization :
jspInit() method is called only once during the life cycle
immediately after the generation of Servlet instance from JSP.
Request processing :
_jspService() method is used to serve the raised requests by
JSP.It takes request and response object as parameters.This
method cannot be overridden.
Features of JSP
Coding in JSP is easy :- As it is just adding JAVA code to
HTML/XML.
Reduction in the length of Code :- In JSP we use action tags,
custom tags etc.
Connection to Database is easier :-It is easier to connect
website to database and allows to read or write data easily to
the database.
Make Interactive websites :- In this we can create dynamic
web pages which helps user to interact in real time environment.
Portable, Powerful, flexible and easy to maintain :- as
these are browser and server independent.
No Redeployment and No Re-Compilation :- It is dynamic,
secure and platform independent so no need to re-compilation.
Extension to Servlet :- as it has all features of servlets,
implicit objects and custom tags
JSP Vs Servlet
Servlet JSP
Servlet is a java code. JSP is a html based code.
Writing code for servlet is harder than JSP is easy to code as it is java in
JSP html.
Servlet plays a controller role in MVC JSP is the view in MVC approach for
approach. showing output.
JSP is slower than Servlet because the
first step in JSP lifecycle is the
Servlet is faster than JSP.
translation of JSP to java code and
then compile.
Servlet can accept all protocol
JSP only accept http requests.
requests.
In Servlet, we can override the In JSP, we cannot override its
service() method. service() method.
In Servlet by default session
In JSP session management is
management is not enabled, user
automatically enabled.
have to enable it explicitly.
Modification in Servlet is a time
consuming task because it includes JSP modification is fast, just need to
reloading, recompiling and restarting click the refresh button.
Directory structure JSP
More on JSP syntax and
contents
HTML code for user interface lay out
JSP tags: declarations, actions,
directives, expressions, scriplets
JSP implicit objects: a request object,
response object, session object, config
object
Javabeans: for logic that can be taken
care of at the JSP level.
We will examine only JSP tags here.
JSP Syntax
1. Declaration Tag :-It is used to
declare variables.
Syntax:- <%! Dec var %>
Example:- <%! int var=10; %>
2. Java Scriplets :- It allows us to
add any number of JAVA code,
variables and expressions.
Syntax:- <% java code %>
JSP Syntax
3. JSP Expression :- It evaluates and
convert the expression to a string.
Syntax:- <%= expression %>
Example:- <% num1 = num1+num2 %>
4. JAVA Comments :- It contains the
text that is added for information which
has to be ignored.
Syntax:- <% -- JSP Comments %>
JSP Tags
Declaration: variable declaration
<%! int age = 56 %>
Directive: ex: import classes
<%@ page import = “java.util.*” %>
Scriplet: Java code
<% if password(“xyz”) {
%>
<H1> Welcome <\H1>
Expression: regular expression using variables and
constants
 <%= param[3]+4 %>

Action: <jsp:usebean name =“cart”


class=“com.sun.java.Scart”
Java Server Pages by
Examples
JSPs combine static markup (HTML,
XML) with special dynamic scripting
tags.
Each JSP is translated into a servlet the
first time it is invoked. Then on, the
requests are serviced by the servlets.
Lets understand the building blocks of a
JSP, namely, directives, scripting
elements, and actions through a series
of examples.
How to prepare and run
the examples?
Simple JSPs can be typed into .jsp type files
using your favorite editor.
Create a directory called JSPExamples in the
public_html directory of J2EE. Store the
example JSPs here.
Start the J2EE server.
Run the JSP from your browser using the
command:
http://localhost:8000/JSPExamples/xyz.jsp
For complex examples with actions and beans
you will have to create web component (WAR).
Examples: Directives
<%@ %>
A directive configures the code generation
that container will perform in creating a
servlet.
Simple JSP showing access to a Java API
class Date: simple.jsp
Using Page directives to define various
page attributes: pageDirective.jsp
Directive to include other JSPs:
includeDirective1.jsp, includeDirective2.jsp
Examples: Scripting
Elements
Declaration:
<%! %>
A declaration is a block of code in
a JSP that is used to define class-
wide variables and methods in the
generated servlet.
Declaring a piece of code:
declaration.jsp
Examples: Scripting
Elements (contd.)
Scriplets: <% %>
A scriplet is a block of Java code that is
executed during the request-processing
time.
See scriplet.jsp
Expressions: sends a value of a Java
expression back to the client.
<%= %>
See expression.jsp
Any
Questions????

You might also like