SlideShare a Scribd company logo
JSP
JAVA SERVER PAGES
JSP
 Java Server Pages (JSP) is a server-side programming technology that enables
the creation of dynamic, platform-independent method for building Web-
based applications.
 JSP have access to the entire family of Java APIs, including the JDBC API to
access enterprise databases. This will teach you how to use Java Server Pages
to develop your web applications in simple and easy steps.
 A JSP page consists of HTML tags and JSP tags.
 The JSP pages are easier to maintain than Servlet because we can separate
designing and development.
 It provides some additional features such as Expression Language, Custom
Tags, etc.
Why to Learn JSP?
 JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway
Interface (CGI). But JSP offers several advantages in comparison with the CGI.
 Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself
instead of having separate CGI files.
 JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server
to load an interpreter and the target script each time the page is requested.
 JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the
powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.
 JSP pages can be used in combination with servlets that handle the business logic, the model supported
by Java servlet template engines.
 Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means
that JSP can play a part in the simplest applications to the most complex and demanding.
Advantages of JSP over Servlet
1) Extension to Servlet
2) Easy to maintain
3) Fast Development
4) Less code than Servlet
JSP ARCHITECTURE
JSP PROCESSING
JSP PROCESSING
The following steps explain how the web server creates the Webpage using JSP −
 As with a normal page, your browser sends an HTTP request to the web server.
 The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by
using the URL or JSP page which ends with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple
in which all template text is converted to println( ) statements and all JSP elements are converted to Java code.
This code implements the corresponding dynamic behavior of the page.
 The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.
 A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the
servlet produces an output in HTML format. The output is furthur passed on to the web server by the servlet
engine inside an HTTP response.
 The web server forwards the HTTP response to your browser in terms of static HTML content.
 Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response exactly as if it
were a static page.
JSP LIFECYCLE
 Compilation
 Initialization
 Execution
 Cleanup
JSP LIFECYCLE
JSP Compilation
 Parsing the JSP.
 Turning the JSP into a servlet.
 Compiling the servlet.
JSP Initialization
 When a container loads a JSP it invokes the jspInit() method before
servicing any requests. If you need to perform JSP-specific initialization,
override the jspInit() method −
public void jspInit(){
// Initialization code...
}
 Typically, 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.
JSP Execution
 This phase of the JSP life cycle represents all interactions with
requests until the JSP is destroyed.
 Whenever a browser requests a JSP and the page has been loaded
and initialized, the JSP engine invokes the _jspService() method in
the JSP.
 The _jspService() method takes an HttpServletRequest and
an HttpServletResponse as its parameters as follows −
void _jspService(HttpServletRequest request, HttpServletResponse
response) {
// Service handling code...
}
JSP Cleanup
 The destruction phase of the JSP life cycle represents when a JSP is
being removed from use by a container.
 The jspDestroy() method is the JSP equivalent of the destroy
method for servlets. Override jspDestroy when you need to
perform any cleanup, such as releasing database connections or
closing open files.
 The jspDestroy() method has the following form −
public void jspDestroy() {
// Your cleanup code goes here.
}
JSP SYNTAX
 Following is the syntax of Scriptlet −
<% code fragment %>
SAMPLE CODE
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
Creating a simple JSP Page
 index.jsp:Let's see the simple example of JSP where we are using the scriptlet tag to
put Java code in the JSP page. We will learn scriptlet tag later.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
 It will print 10 on the browser.
How to run a simple JSP Page?
 Start the server
 Put the JSP file in a folder and deploy on the server
 Visit the browser by the URL http://localhost:portno/contextRoot/jspfile,
for example, http://localhost:8888/myapplication/index.jsp
JSP Declarations
 <%! int i = 0; %>
 <%! int a, b, c; %>
 <%! Circle a = new Circle(2.0); %>
JSP Expression
<html>
<head><title>A Comment Test</title></head>
<body>
<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
</body>
</html>
OUTPUT
Today's date: 25-May-2021 21:24:25
JSP Directives
 A JSP directive affects the overall structure of the servlet class. It usually has the
following form −
 <%@ directive attribute="value" %>
 There are three types of directive tag −
S.No. Directive & Description
1
<%@ page ... %>
Defines page-dependent attributes, such as scripting language, error page, and buffering requirements.
2
<%@ include ... %>
Includes a file during the translation phase.
3
<%@ taglib ... %>
Declares a tag library, containing custom actions, used in the page
JSP Actions
 JSP actions use constructs in XML syntax to control the behavior
of the servlet engine. You can dynamically insert a file, reuse
JavaBeans components, forward the user to another page, or
generate HTML for the Java plugin.
 There is only one syntax for the Action element, as it conforms to
the XML standard −
<jsp:action_name attribute="value" />
JSP Implicit Objects
S.No. Object & Description
1
request
This is the HttpServletRequest object associated with the request.
2
response
This is the HttpServletResponse object associated with the response to the client.
3
out
This is the PrintWriter object used to send output to the client.
4
session
This is the HttpSession object associated with the request.
5
application
This is the ServletContext object associated with the application context.
6
config
This is the ServletConfig object associated with the page.
7
pageContext
This encapsulates use of server-specific features like higher performance JspWriters.
8
page
This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
Exception
JSP - Form Processing
The Methods in Form Processing
GET method
 The GET method sends the encoded user information appended to the page request. The page and the encoded information are
separated by the ? character as follows −
http://www.test.com/hello?key1=value1&key2=value2
 The GET method is the default method to pass information from the browser to the web server and it produces a long string that
appears in your browser's Location:box. It is recommended that the GET method is better not used. if you have password or other
sensitive information to pass to the server.
 The GET method has size limitation: only 1024 characters can be in a request string.
 This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable
which can be handled using getQueryString() and getParameter() methods of request object.
POST method
 A generally more reliable method of passing information to a backend program is the POST method.
 This method packages the information in exactly the same way as the GET method, but instead of sending it as a text string after a ?
in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which
you can parse and use for your processing.
 JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read
binary data stream coming from the client
Reading Form Data using JSP
 getParameter() − You call request.getParameter() method to get the value of a
form parameter.
 getParameterValues() − Call this method if the parameter appears more than
once and returns multiple values, for example checkbox.
 getParameterNames() − Call this method if you want a complete list of all
parameters in the current request.
 getInputStream() -− Call this method to read binary data stream coming from
the client.
Creating JSP in Eclipse IDE with Tomcat server
 Create a Dynamic web project
 create a jsp
 start tomcat server and deploy the project
1) Create the dynamic web project:
 For creating a dynamic web project click on File Menu -> New ->
dynamic web project -> write your project name e.g. first -> Finish.
2) Create the JSP file in eclipse IDE
 For creating a jsp file explore the project by clicking the + icon ->
right click on WebContent -> New -> jsp -> write your jsp file
name e.g. index -> next -> Finish.
3) Start the server and deploy the project:
 For starting the server and deploying the project in one step Right
click on your project -> Run As -> Run on Server -> choose tomcat
server -> next -> addAll -> finish.

More Related Content

What's hot

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
Vipin Yadav
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
Nuha Noor
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
Khan Mac-arther
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
Manisha Keim
 
Jsp
JspJsp
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
Abhishek Kesharwani
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
JavaEE Trainers
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Jsp
JspJsp
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
Tanmoy Barman
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
Sasidhar Kothuru
 
Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
Chitrank Dixit
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
Darshit Metaliya
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
ShahDhruv21
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
BG Java EE Course
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharingvikram singh
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
Sher Singh Bardhan
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
Talha Ocakçı
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
JSP overview
JSP overviewJSP overview
JSP overview
Amisha Narsingani
 

What's hot (20)

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Jsp
JspJsp
Jsp
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Jsp
JspJsp
Jsp
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Jsp (java server page)
Jsp (java server page)Jsp (java server page)
Jsp (java server page)
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
JSP overview
JSP overviewJSP overview
JSP overview
 

Similar to JAVA SERVER PAGES

JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
Yoga Raja
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
Abhishek Kesharwani
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
MathivananP4
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
Kaml Sah
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
Arumugam90
 
Jsp
JspJsp
Jsp
JspJsp
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
vishal choudhary
 
Atul & shubha goswami jsp
Atul & shubha goswami jspAtul & shubha goswami jsp
Atul & shubha goswami jsp
Atul Giri
 
Arpita industrial trainingppt
Arpita industrial trainingpptArpita industrial trainingppt
Arpita industrial trainingppt
ARPITA SRIVASTAVA
 
Jsp
JspJsp
Jsp
JspJsp
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
NishaRohit6
 
The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
Kavitha713564
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP

Similar to JAVA SERVER PAGES (20)

JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
Java server pages
Java server pagesJava server pages
Java server pages
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Atul & shubha goswami jsp
Atul & shubha goswami jspAtul & shubha goswami jsp
Atul & shubha goswami jsp
 
Arpita industrial trainingppt
Arpita industrial trainingpptArpita industrial trainingppt
Arpita industrial trainingppt
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
The Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptxThe Java Server Page in Java Concept.pptx
The Java Server Page in Java Concept.pptx
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 

Recently uploaded

MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 

Recently uploaded (20)

MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 

JAVA SERVER PAGES

  • 2. JSP  Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web- based applications.  JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. This will teach you how to use Java Server Pages to develop your web applications in simple and easy steps.  A JSP page consists of HTML tags and JSP tags.  The JSP pages are easier to maintain than Servlet because we can separate designing and development.  It provides some additional features such as Expression Language, Custom Tags, etc.
  • 3. Why to Learn JSP?  JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI.  Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.  JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.  JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.  JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.  Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.
  • 4. Advantages of JSP over Servlet 1) Extension to Servlet 2) Easy to maintain 3) Fast Development 4) Less code than Servlet
  • 7. JSP PROCESSING The following steps explain how the web server creates the Webpage using JSP −  As with a normal page, your browser sends an HTTP request to the web server.  The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html.  The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code. This code implements the corresponding dynamic behavior of the page.  The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.  A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format. The output is furthur passed on to the web server by the servlet engine inside an HTTP response.  The web server forwards the HTTP response to your browser in terms of static HTML content.  Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response exactly as if it were a static page.
  • 8. JSP LIFECYCLE  Compilation  Initialization  Execution  Cleanup
  • 10. JSP Compilation  Parsing the JSP.  Turning the JSP into a servlet.  Compiling the servlet.
  • 11. JSP Initialization  When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need to perform JSP-specific initialization, override the jspInit() method − public void jspInit(){ // Initialization code... }  Typically, 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.
  • 12. JSP Execution  This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.  Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the _jspService() method in the JSP.  The _jspService() method takes an HttpServletRequest and an HttpServletResponse as its parameters as follows − void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service handling code... }
  • 13. JSP Cleanup  The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container.  The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files.  The jspDestroy() method has the following form − public void jspDestroy() { // Your cleanup code goes here. }
  • 14. JSP SYNTAX  Following is the syntax of Scriptlet − <% code fragment %>
  • 15. SAMPLE CODE <html> <head><title>Hello World</title></head> <body> Hello World!<br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html>
  • 16. Creating a simple JSP Page  index.jsp:Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page. We will learn scriptlet tag later. <html> <body> <% out.print(2*5); %> </body> </html>  It will print 10 on the browser.
  • 17. How to run a simple JSP Page?  Start the server  Put the JSP file in a folder and deploy on the server  Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example, http://localhost:8888/myapplication/index.jsp
  • 18. JSP Declarations  <%! int i = 0; %>  <%! int a, b, c; %>  <%! Circle a = new Circle(2.0); %>
  • 19. JSP Expression <html> <head><title>A Comment Test</title></head> <body> <p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p> </body> </html> OUTPUT Today's date: 25-May-2021 21:24:25
  • 20. JSP Directives  A JSP directive affects the overall structure of the servlet class. It usually has the following form −  <%@ directive attribute="value" %>  There are three types of directive tag − S.No. Directive & Description 1 <%@ page ... %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. 2 <%@ include ... %> Includes a file during the translation phase. 3 <%@ taglib ... %> Declares a tag library, containing custom actions, used in the page
  • 21. JSP Actions  JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.  There is only one syntax for the Action element, as it conforms to the XML standard − <jsp:action_name attribute="value" />
  • 22. JSP Implicit Objects S.No. Object & Description 1 request This is the HttpServletRequest object associated with the request. 2 response This is the HttpServletResponse object associated with the response to the client. 3 out This is the PrintWriter object used to send output to the client. 4 session This is the HttpSession object associated with the request. 5 application This is the ServletContext object associated with the application context. 6 config This is the ServletConfig object associated with the page. 7 pageContext This encapsulates use of server-specific features like higher performance JspWriters. 8 page This is simply a synonym for this, and is used to call the methods defined by the translated servlet class. Exception
  • 23. JSP - Form Processing The Methods in Form Processing GET method  The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows − http://www.test.com/hello?key1=value1&key2=value2  The GET method is the default method to pass information from the browser to the web server and it produces a long string that appears in your browser's Location:box. It is recommended that the GET method is better not used. if you have password or other sensitive information to pass to the server.  The GET method has size limitation: only 1024 characters can be in a request string.  This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable which can be handled using getQueryString() and getParameter() methods of request object. POST method  A generally more reliable method of passing information to a backend program is the POST method.  This method packages the information in exactly the same way as the GET method, but instead of sending it as a text string after a ? in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing.  JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client
  • 24. Reading Form Data using JSP  getParameter() − You call request.getParameter() method to get the value of a form parameter.  getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for example checkbox.  getParameterNames() − Call this method if you want a complete list of all parameters in the current request.  getInputStream() -− Call this method to read binary data stream coming from the client.
  • 25. Creating JSP in Eclipse IDE with Tomcat server  Create a Dynamic web project  create a jsp  start tomcat server and deploy the project
  • 26. 1) Create the dynamic web project:  For creating a dynamic web project click on File Menu -> New -> dynamic web project -> write your project name e.g. first -> Finish.
  • 27. 2) Create the JSP file in eclipse IDE  For creating a jsp file explore the project by clicking the + icon -> right click on WebContent -> New -> jsp -> write your jsp file name e.g. index -> next -> Finish.
  • 28. 3) Start the server and deploy the project:  For starting the server and deploying the project in one step Right click on your project -> Run As -> Run on Server -> choose tomcat server -> next -> addAll -> finish.