SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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.

Weitere ähnliche Inhalte

Was ist angesagt?

JSP Scope variable And Data Sharing
JSP Scope variable And Data SharingJSP Scope variable And Data Sharing
JSP Scope variable And Data Sharing
vikram singh
 
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çı
 

Was ist angesagt? (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
 

Ähnlich wie JAVA SERVER PAGES

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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Kürzlich hochgeladen (20)

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

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.