SlideShare ist ein Scribd-Unternehmen logo
1 von 36
JSP introduction Trainer = Subhasis Nayak CMC
What is JSP ? It is a textual document that describes how to create a response object from a request object for given protocol. The processing of JSP page may involve: Creating objects Using objects. The default request & response objects are HttpServletRequest HttpServletResponse
What it provides? Capability to create both static & dynamic components. Dynamic capability that Servlet can provide. Integration of content. Manipulating data.
Information about JSP Java Server pages has these features: Language for developing java server pages. Constructs for accessing server-side objects. Defining extensions to the JSP language. It is a text based document that contains two types of text. Static template data JSP elements which construct dynamic data.
JSP life cycle Some how similar to Servlet life cycle. When any request mapped to a JSP, it is handled by a special Servlet event. Servlet checks the java Server  Page’s Servlet is older than the java Server  Page. If so it translates to java Server  Page  into a Servlet class & compiles the class. Next the Servlet sends response to the java server page.
Advantages of JSP Build process is performed automatically. Translation phase treat each data type in java Server Pages differently. Template data is transformed into code. This code emits that data stream that returns data to the client.  It is faster than Servlet.
JSP Architecture Model According to Sun  there 2 architectural model for building application using JSP & Servlet technology. Those are: JSP model 1 JSP model 2
JSP model 1 Not good for Complex In this model the every request to a JSP page. The requested page is completed  responsible for doing all the tasks required for fulfilling the request. Suite able for small application. In big application we put huge logic code to JSP. JSP Browser 1.request EIS/DB 2.Create Java beans 4.response Java Beans 3.Retrieve data
JSP model 2 This architecture follows MVC design pattern. Servlet act as Controller JSP act as View Java Bean act as Model All requests to Servlet. They analyze the request and collect data required to generate response into java beans objects. Then Servlet dispatch the request to JSP. JSP use data stored in java beans to generate a presentable response.
Cont’d ……  ,[object Object]
Easy maintainability Servlet (Controller) 2.Create Java beans EIS/DB 3.Retrieve data Java Beans(Model) Browser 1.request 4.Invoke JSP 2.Use Java beans 6.response JSP (view)
JSP life Cycle. For 1st time when we accessed the JSP page server is slower after that it will faster. When we access JSP it is converted to it’s Servlet class before it can be used to service client side request. For each request the JSP engine checks the timestamps of source JSP page and corresponding Servlet if JSP is newer then it will be again converted to it’s equivalent Servlet. This process consists of 7-phases.
Cont’d …..
Elements of JSP
A simple JSP page <html>  <body>  <% out.println("<h1>Hello World!</h1>");%>  </body>  </html>  Scriptlet
The processing of JSP When the browser asks the Web server for a JSP, the Web server passes control to a JSP container. A container works with the Web server to provide the runtime environment and other services a JSP needs.  It knows how to understand the special elements that are part of JSPs. Because this is the first time this JSP has been invoked, the JSP container converts it into an executable unit called a Servlet.
Cont’d ……  The entire page, including the parts that are in HTML, is translated into source code. After code translation, the JSP container compiles the Servlet, loads it automatically, and then executes it. Typically, the JSP container checks to see whether a Servlet for a JSP file already exists . If not it do the translation process If version not match do the translation process
Let’s see the translated code of a JSP JSP Servlet Don’t worry about Servlet  file of JSP. JSP, why? <html>  <body> <h1>Hello World!</h1> </body>  </html> out.write("<html>"); out.write("<body>"); out.println("<h1>Hello World!</h1>"); out.write(""); out.write("</body>"); out.write("</html>");
Let’s display Something Output swill same The second one is the short hand code
Program displaying date
variables You can declare your own variables, as usual JSP provides several predefined variables request :  The HttpServletRequest parameter response :  The HttpServletResponse parameter session :  The HttpSession associated with the request, or null if there is none out :  A JspWriter (like a PrintWriter) used to send output to the client Example: Your hostname: <%= request.getRemoteHost() %>
Scriptlets Scriptlets are enclosed in <% ... %>tags Scriptletsdo not produce a value that is inserted directly into the HTML (as is done with <%= ... %>) Scriptlets are Java code that may write into the HTML Example:<% String queryData = request.getQueryString();out.println("Attached GET data: " + queryData); %> Scriptlets are inserted into the Servletexactly as written, and are not compiled until the entire Servlet is compiled Example:<% if (Math.random() < 0.5) { %>Have a <B>nice</B> day!<% } else { %>          Have a <B>lousy</B> day!<% } %>
Declarations Use <%! ... %>for declarations to be added to your Servlet class, not to any particular method Caution: Servlet are multithreaded, so nonlocal variables must be handled with extreme care If declared with<% ... %>, variables are local and OK Data can also safely be put in the request or session objects Example:<%! private intaccessCount = 0; %>      Accesses to page since server reboot: <%= ++accessCount %> You can use<%! ... %>to declare methods as easily as to declare variables
Directives Directives affect the Servlet class itself A directive has the form: <%@ directiveattribute="value" %>or<%@ directiveattribute1="value1" attribute2="value2"                          ...attributeN="valueN" %> The most useful directive is page, which lets you import packages Example:<%@ page import="java.util.*" %>
JSP Comments Different from HTML comments. HTML comments are visible to client. <!-- an HTML comment --> JSP comments are used for documenting JSP code . JSP comments are not visible client-side. <%-- a JSP comment --%>
The include directive The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax:  <%@ include file="URL" %> The URL is treated as relative to the JSP page The include directive is especially useful for inserting things like navigation bars
Actions Actions are XML-syntax tags used to control the Servlet engine <jsp:include page="URL" /> Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data <jsp:forward page="URL" /><jsp:forward page="<%= JavaExpression %>" /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL
JSP in XML JSP can be embedded in XML as well as in HTML Due to XML’s syntax rules, the tags must be different (but they do the same things) HTML: <%= expression %>XML: <jsp:expression>expression</jsp:expression> HTML: <% code %>XML: <jsp:scriptlet>code</jsp:scriptlet> HTML: <%! declarations %>XML: <jsp:declaration>declarations</jsp:declaration> HTML: <%@ include file=URL %>XML: <jsp:directive.include file="URL"/>
Action – useBean tag The useBean action tag is the most commonly used tag because of its powerful features. It allows a JSP to create an instance or receive an instance of a Java Bean. It is used for creating or instantiating a bean with a specific name and scope. Examples <jsp:useBean id=“time" scope="session" class="com.time.CurrentTimeBean" />
Session in jsp In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. Session management done by: Session Object Cookies Hidden Form Fields URL Rewriting
Program for math calculation Declarative tag Expression tag
Program to display the string
XML style
Compare XML tag with normal Normal JSP tag XML tag of the JSP tag
Program to write scriptlet tag Scriptlet tag
Lab questions? Declare an integer type variable and display it . Write the XMl style tag to declare and display the variable. Write a program to compare a string is equal to “welcome” .if equal display something else display something. Write a program to compare two strings values: 1st string = “my fname” 2nd sting = “my lname” If both strings are true then display “both are true” else display “one of them not true or no one is true”

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
 

Was ist angesagt? (20)

Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
JSP
JSPJSP
JSP
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
 
Implicit objects advance Java
Implicit objects advance JavaImplicit objects advance Java
Implicit objects advance Java
 
Jsp
JspJsp
Jsp
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
 
Jsp
JspJsp
Jsp
 
Jsp element
Jsp elementJsp element
Jsp element
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Web Applications and Deployment
Web Applications and DeploymentWeb Applications and Deployment
Web Applications and Deployment
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Implicit object.pptx
Implicit object.pptxImplicit object.pptx
Implicit object.pptx
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 

Andere mochten auch (8)

Pablo Pineda Work Sampler
Pablo Pineda Work SamplerPablo Pineda Work Sampler
Pablo Pineda Work Sampler
 
working with database using mysql
working with database using mysql working with database using mysql
working with database using mysql
 
c++ part1
c++ part1c++ part1
c++ part1
 
Introduction to network
Introduction to networkIntroduction to network
Introduction to network
 
Php, mysqlpart2
Php, mysqlpart2Php, mysqlpart2
Php, mysqlpart2
 
C++ arrays part1
C++ arrays part1C++ arrays part1
C++ arrays part1
 
Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)Php, mysq lpart5(mysql)
Php, mysq lpart5(mysql)
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 

Ähnlich wie C:\fakepath\jsp01

Ähnlich wie C:\fakepath\jsp01 (20)

JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 
Jsp
JspJsp
Jsp
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
Learning jsp
Learning jspLearning jsp
Learning jsp
 
Jsp
JspJsp
Jsp
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
Servlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servletsServlet/JSP course chapter 1: Introduction to servlets
Servlet/JSP course chapter 1: Introduction to servlets
 
The java server pages
The java server pagesThe java server pages
The java server pages
 
JSP overview
JSP overviewJSP overview
JSP overview
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"Jsp Presentation +Mufix "3"
Jsp Presentation +Mufix "3"
 
Server-side Technologies in Java
Server-side Technologies in JavaServer-side Technologies in Java
Server-side Technologies in Java
 

Mehr von Subhasis Nayak

Mehr von Subhasis Nayak (13)

Php, mysq lpart3
Php, mysq lpart3Php, mysq lpart3
Php, mysq lpart3
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
Jsp 02(jsp directives)2003
Jsp 02(jsp directives)2003Jsp 02(jsp directives)2003
Jsp 02(jsp directives)2003
 
Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
Servlet & jsp
Servlet  &  jspServlet  &  jsp
Servlet & jsp
 
J2ee connector architecture
J2ee connector architectureJ2ee connector architecture
J2ee connector architecture
 
how to create object
how to create objecthow to create object
how to create object
 
Pointer in c++ part3
Pointer in c++ part3Pointer in c++ part3
Pointer in c++ part3
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
C++ arrays part2
C++ arrays part2C++ arrays part2
C++ arrays part2
 
Flow control in c++
Flow control in c++Flow control in c++
Flow control in c++
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
Text mode Linux Installation Part 01
Text mode Linux Installation Part 01Text mode Linux Installation Part 01
Text mode Linux Installation Part 01
 

Kürzlich hochgeladen

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

C:\fakepath\jsp01

  • 1. JSP introduction Trainer = Subhasis Nayak CMC
  • 2. What is JSP ? It is a textual document that describes how to create a response object from a request object for given protocol. The processing of JSP page may involve: Creating objects Using objects. The default request & response objects are HttpServletRequest HttpServletResponse
  • 3. What it provides? Capability to create both static & dynamic components. Dynamic capability that Servlet can provide. Integration of content. Manipulating data.
  • 4. Information about JSP Java Server pages has these features: Language for developing java server pages. Constructs for accessing server-side objects. Defining extensions to the JSP language. It is a text based document that contains two types of text. Static template data JSP elements which construct dynamic data.
  • 5. JSP life cycle Some how similar to Servlet life cycle. When any request mapped to a JSP, it is handled by a special Servlet event. Servlet checks the java Server Page’s Servlet is older than the java Server Page. If so it translates to java Server Page into a Servlet class & compiles the class. Next the Servlet sends response to the java server page.
  • 6. Advantages of JSP Build process is performed automatically. Translation phase treat each data type in java Server Pages differently. Template data is transformed into code. This code emits that data stream that returns data to the client. It is faster than Servlet.
  • 7. JSP Architecture Model According to Sun there 2 architectural model for building application using JSP & Servlet technology. Those are: JSP model 1 JSP model 2
  • 8. JSP model 1 Not good for Complex In this model the every request to a JSP page. The requested page is completed responsible for doing all the tasks required for fulfilling the request. Suite able for small application. In big application we put huge logic code to JSP. JSP Browser 1.request EIS/DB 2.Create Java beans 4.response Java Beans 3.Retrieve data
  • 9. JSP model 2 This architecture follows MVC design pattern. Servlet act as Controller JSP act as View Java Bean act as Model All requests to Servlet. They analyze the request and collect data required to generate response into java beans objects. Then Servlet dispatch the request to JSP. JSP use data stored in java beans to generate a presentable response.
  • 10.
  • 11. Easy maintainability Servlet (Controller) 2.Create Java beans EIS/DB 3.Retrieve data Java Beans(Model) Browser 1.request 4.Invoke JSP 2.Use Java beans 6.response JSP (view)
  • 12. JSP life Cycle. For 1st time when we accessed the JSP page server is slower after that it will faster. When we access JSP it is converted to it’s Servlet class before it can be used to service client side request. For each request the JSP engine checks the timestamps of source JSP page and corresponding Servlet if JSP is newer then it will be again converted to it’s equivalent Servlet. This process consists of 7-phases.
  • 15. A simple JSP page <html> <body> <% out.println("<h1>Hello World!</h1>");%> </body> </html> Scriptlet
  • 16. The processing of JSP When the browser asks the Web server for a JSP, the Web server passes control to a JSP container. A container works with the Web server to provide the runtime environment and other services a JSP needs. It knows how to understand the special elements that are part of JSPs. Because this is the first time this JSP has been invoked, the JSP container converts it into an executable unit called a Servlet.
  • 17. Cont’d …… The entire page, including the parts that are in HTML, is translated into source code. After code translation, the JSP container compiles the Servlet, loads it automatically, and then executes it. Typically, the JSP container checks to see whether a Servlet for a JSP file already exists . If not it do the translation process If version not match do the translation process
  • 18. Let’s see the translated code of a JSP JSP Servlet Don’t worry about Servlet file of JSP. JSP, why? <html> <body> <h1>Hello World!</h1> </body> </html> out.write("<html>"); out.write("<body>"); out.println("<h1>Hello World!</h1>"); out.write(""); out.write("</body>"); out.write("</html>");
  • 19. Let’s display Something Output swill same The second one is the short hand code
  • 21. variables You can declare your own variables, as usual JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter) used to send output to the client Example: Your hostname: <%= request.getRemoteHost() %>
  • 22. Scriptlets Scriptlets are enclosed in <% ... %>tags Scriptletsdo not produce a value that is inserted directly into the HTML (as is done with <%= ... %>) Scriptlets are Java code that may write into the HTML Example:<% String queryData = request.getQueryString();out.println("Attached GET data: " + queryData); %> Scriptlets are inserted into the Servletexactly as written, and are not compiled until the entire Servlet is compiled Example:<% if (Math.random() < 0.5) { %>Have a <B>nice</B> day!<% } else { %> Have a <B>lousy</B> day!<% } %>
  • 23. Declarations Use <%! ... %>for declarations to be added to your Servlet class, not to any particular method Caution: Servlet are multithreaded, so nonlocal variables must be handled with extreme care If declared with<% ... %>, variables are local and OK Data can also safely be put in the request or session objects Example:<%! private intaccessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> You can use<%! ... %>to declare methods as easily as to declare variables
  • 24. Directives Directives affect the Servlet class itself A directive has the form: <%@ directiveattribute="value" %>or<%@ directiveattribute1="value1" attribute2="value2" ...attributeN="valueN" %> The most useful directive is page, which lets you import packages Example:<%@ page import="java.util.*" %>
  • 25. JSP Comments Different from HTML comments. HTML comments are visible to client. <!-- an HTML comment --> JSP comments are used for documenting JSP code . JSP comments are not visible client-side. <%-- a JSP comment --%>
  • 26. The include directive The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax: <%@ include file="URL" %> The URL is treated as relative to the JSP page The include directive is especially useful for inserting things like navigation bars
  • 27. Actions Actions are XML-syntax tags used to control the Servlet engine <jsp:include page="URL" /> Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data <jsp:forward page="URL" /><jsp:forward page="<%= JavaExpression %>" /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL
  • 28. JSP in XML JSP can be embedded in XML as well as in HTML Due to XML’s syntax rules, the tags must be different (but they do the same things) HTML: <%= expression %>XML: <jsp:expression>expression</jsp:expression> HTML: <% code %>XML: <jsp:scriptlet>code</jsp:scriptlet> HTML: <%! declarations %>XML: <jsp:declaration>declarations</jsp:declaration> HTML: <%@ include file=URL %>XML: <jsp:directive.include file="URL"/>
  • 29. Action – useBean tag The useBean action tag is the most commonly used tag because of its powerful features. It allows a JSP to create an instance or receive an instance of a Java Bean. It is used for creating or instantiating a bean with a specific name and scope. Examples <jsp:useBean id=“time" scope="session" class="com.time.CurrentTimeBean" />
  • 30. Session in jsp In session management whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. Session management done by: Session Object Cookies Hidden Form Fields URL Rewriting
  • 31. Program for math calculation Declarative tag Expression tag
  • 32. Program to display the string
  • 34. Compare XML tag with normal Normal JSP tag XML tag of the JSP tag
  • 35. Program to write scriptlet tag Scriptlet tag
  • 36. Lab questions? Declare an integer type variable and display it . Write the XMl style tag to declare and display the variable. Write a program to compare a string is equal to “welcome” .if equal display something else display something. Write a program to compare two strings values: 1st string = “my fname” 2nd sting = “my lname” If both strings are true then display “both are true” else display “one of them not true or no one is true”
  • 37. Hands on lab Next session