SlideShare a Scribd company logo
1 of 17
Scripting Element in JSP
Agenda
• Static v/s dynamic text
• Directives
• JSP Scriptlets
• JSP declarations
• Expression
• Action
Static v/s Dynamic Text
• JSP is an html or xhtml page which contains some server side
scripting
• Static contents are directly placed inside _jspService method
• Dynamic contents basically control how the resulting servlets should
be generated
• We can also override the jspInit() and jsDestroy() method using
scripting elements
• We cannot override _jspService method()
Types of scripting
• Expressions
– Format: <%= expression %>
– Evaluated and inserted into the servlet’s output.
I.e., results in something like out.print(expression)
• Scriptlets
– Format: <% code %>
– Inserted verbatim into the servlet’s _jspService method
(called by service)
• Declarations
– Format: <%! code %>
– Inserted verbatim into the body of the servlet class,
outside of any existing methods
JSP Expression
• Format
- <%= Java Expression %>
• Result
- Expression evaluated, converted to String, and placed into HTML page at the place it
occurred in JSP page That is, expression placed in _jspService inside out.print
• Examples
- Current time: <%= new java.util.Date() %>
- Your hostname: <%= request.getRemoteHost() %>
• XML-compatible syntax
- <jsp:expression>Java Expression</jsp:expression>
- You cannot mix versions within a single page. You must use XML for entire page if
you use jsp:expression
JSP Expression with translation
Original JSP
<H1>A Random Number</H1>
<%= Math.random() %>
Representative resulting servlet code
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
JspWriter out = response.getWriter();
out.println("<H1>A Random Number</H1>");
out.println(Math.random())
...
}
Predefined Variables
• Request
- The HttpServletRequest (1st argument to service/doGet)
• response
- The HttpServletResponse (2nd arg to service/doGet)
• out
- The Writer (a buffered version of type JspWriter) used to send output to
the client
• session
- The HttpSession associated with the request (unless disabled with the
session attribute of the page directive)
• application
- The ServletContext (for sharing data) as obtained via
getServletContext().
JSP Scriptlets
• Format
- <% Java Code %>
• Result
- Code is inserted verbatim into servlet's _jspService
• Example
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
<% response.setContentType("text/plain"); %>
• XML-compatible syntax
<jsp:scriptlet>Java Code</jsp:scriptlet>
JSP Scriptlets translation
Original JSP
<H2>foo</H2>
<%= bar() %>
<% baz(); %>
Representative resulting servlet code
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
JspWriter out = response.getWriter();
out.println("<H2>foo</H2>");
out.println(bar());
baz(); }
JSP Scriptlets example
<!DOCTYPE …><HTML><HEAD><TITLE>Color
Testing</TITLE></HEAD>
<%
String bgColor = request.getParameter("bgColor");
if ((bgColor == null)||(bgColor.trim().equals(""))){
bgColor = "WHITE";
}
%>
<BODY BGCOLOR="<%= bgColor %>">
<H2 ALIGN="CENTER">Testing a Background of
"<%= bgColor %>"</H2>
</BODY></HTML>
Scriptlets with condition
• Point
- Scriplets are inserted into servlet exactly as written
- Need not be complete Java expressions
- Complete expressions are usually clearer and easier to maintain, however
Example
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
Representative result
if (Math.random() < 0.5) {
out.println("Have a <B>nice</B>
day!");
} else {
out.println("Have a <B>lousy</B>
day!");
}
JSP Declarartions
• Format- <%! Java Code %>
• Result- Code is inserted verbatim into servlet's class definition,outside of
any existing methods
• Examples
<%! private int someField = 5; %>
<%! private void someMethod(...) {...} %>
• Design consideration
Fields are clearly useful. For methods, it is usually better to define the
method in a separate Java class.
• XML-compatible syntax
<jsp:declaration>Java Code</jsp:declaration>
JSP declarations
<H1>Some Heading</H1>
<%!
private String randomHeading() {
return("<H2>" + Math.random() + "</H2>");
}
%>
<%= randomHeading() %>
(Alternative: make randomHeading a
static method in a separate Java class)
Using jspInit and jspDestroy
• JSP pages, like regular servlets, sometimes want to use init and
destroy
• Problem: the servlet that gets built from the JSP page might
already use init and destroy
- Overriding them would cause problems.
- Thus, it is illegal to use JSP declarations to declare init or destroy
• Solution: use jspInit and jspDestroy.
- The auto-generated servlet is guaranteed to call these methods from init
and destroy, but the standard versions of jspInit and jspDestroy are
empty (placeholders for you to override).
Predefined variable in Declarations
• Problem
• The predefined variables (request, response, out, session, etc.) are local to
the _jspService method. Thus, they are not available to methods defined by
JSP declarations or to methods in helper classes. What can you do about
this?
• Solution: pass them as arguments. E.g.
<%!private void someMethod(HttpSession s) {
doSomethingWith(s);
}%>
<% someMethod(session); %>
Note that the println method of JspWriter throws IOException
Use “throws IOException” for methods that use println
Summary
• JSP Expressions
- Format: <%= expression %>
- Wrapped in out.print and inserted into _jspService
• JSP Scriptlets
- Format: <% code %>
- Inserted verbatim into the servlet’s _jspService method
• JSP Declarations
- Format: <%! code %>
- Inserted verbatim into the body of the servlet class
Summary
• Predefined variables
- request, response, out, session, application
• Limit the Java code that is directly in page
- Use helper classes, beans, servlet/JSP combo (MVC),
- JSP expression language, custom tags

More Related Content

What's hot

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
Getting Started with Angular JS
Getting Started with Angular JSGetting Started with Angular JS
Getting Started with Angular JSAkshay Mathur
 
Thinking in Components
Thinking in ComponentsThinking in Components
Thinking in ComponentsAnton Ivanov
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
Angularjs architecture
Angularjs architectureAngularjs architecture
Angularjs architectureMichael He
 
Advance java session 13
Advance java session 13Advance java session 13
Advance java session 13Smita B Kumar
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile appsIvano Malavolta
 
Java Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 BasicsJava Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 BasicsIMC Institute
 
KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7phuphax
 
An Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersAn Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersHuffPost Code
 
The Lumber Mill Xslt For Your Templates
The Lumber Mill   Xslt For Your TemplatesThe Lumber Mill   Xslt For Your Templates
The Lumber Mill Xslt For Your TemplatesThomas Weinert
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-HibernateJay Shah
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...WebStackAcademy
 

What's hot (20)

MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Getting Started with Angular JS
Getting Started with Angular JSGetting Started with Angular JS
Getting Started with Angular JS
 
Thinking in Components
Thinking in ComponentsThinking in Components
Thinking in Components
 
Angular js 1.3 basic tutorial
Angular js 1.3 basic tutorialAngular js 1.3 basic tutorial
Angular js 1.3 basic tutorial
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
JavaScripts & jQuery
JavaScripts & jQueryJavaScripts & jQuery
JavaScripts & jQuery
 
Angularjs architecture
Angularjs architectureAngularjs architecture
Angularjs architecture
 
Advance java session 13
Advance java session 13Advance java session 13
Advance java session 13
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps
 
Handlebars & Require JS
Handlebars  & Require JSHandlebars  & Require JS
Handlebars & Require JS
 
Spring Mvc Rest
Spring Mvc RestSpring Mvc Rest
Spring Mvc Rest
 
Java Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 BasicsJava Web Programming [7/9] : Struts2 Basics
Java Web Programming [7/9] : Struts2 Basics
 
IGears: Template Architecture and Principles
IGears: Template Architecture and PrinciplesIGears: Template Architecture and Principles
IGears: Template Architecture and Principles
 
KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7KMUTNB - Internet Programming 5/7
KMUTNB - Internet Programming 5/7
 
An Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersAn Intro to Scala for PHP Developers
An Intro to Scala for PHP Developers
 
The Lumber Mill Xslt For Your Templates
The Lumber Mill   Xslt For Your TemplatesThe Lumber Mill   Xslt For Your Templates
The Lumber Mill Xslt For Your Templates
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-Hibernate
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
 

Similar to Advance java session 10

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSbharathiv53
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
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 Ayes Chinmay
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsKaml Sah
 
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
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
 

Similar to Advance java session 10 (20)

JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODSJSP AND XML USING JAVA WITH GET AND POST METHODS
JSP AND XML USING JAVA WITH GET AND POST METHODS
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
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
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
10 jsp-scripting-elements
10 jsp-scripting-elements10 jsp-scripting-elements
10 jsp-scripting-elements
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
 
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
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Jsp
JspJsp
Jsp
 

More from Smita B Kumar

Advance java session 20
Advance java session 20Advance java session 20
Advance java session 20Smita B Kumar
 
Advance java session 19
Advance java session 19Advance java session 19
Advance java session 19Smita B Kumar
 
Advance java session 18
Advance java session 18Advance java session 18
Advance java session 18Smita B Kumar
 
Advance java session 17
Advance java session 17Advance java session 17
Advance java session 17Smita B Kumar
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16Smita B Kumar
 
Advance java session 15
Advance java session 15Advance java session 15
Advance java session 15Smita B Kumar
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14Smita B Kumar
 
Advance java session 12
Advance java session 12Advance java session 12
Advance java session 12Smita B Kumar
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11Smita B Kumar
 
Advance java session 9
Advance java session 9Advance java session 9
Advance java session 9Smita B Kumar
 
Advance java session 8
Advance java session 8Advance java session 8
Advance java session 8Smita B Kumar
 
Advance java session 7
Advance java session 7Advance java session 7
Advance java session 7Smita B Kumar
 
Advance java session 6
Advance java session 6Advance java session 6
Advance java session 6Smita B Kumar
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5Smita B Kumar
 
Advance java session 4
Advance java session 4Advance java session 4
Advance java session 4Smita B Kumar
 
Advance java session 3
Advance java session 3Advance java session 3
Advance java session 3Smita B Kumar
 
Advance java session 2
Advance java session 2Advance java session 2
Advance java session 2Smita B Kumar
 
01 introduction to struts2
01 introduction to struts201 introduction to struts2
01 introduction to struts2Smita B Kumar
 

More from Smita B Kumar (19)

Advance java session 20
Advance java session 20Advance java session 20
Advance java session 20
 
Advance java session 19
Advance java session 19Advance java session 19
Advance java session 19
 
Advance java session 18
Advance java session 18Advance java session 18
Advance java session 18
 
Advance java session 17
Advance java session 17Advance java session 17
Advance java session 17
 
Advance java session 16
Advance java session 16Advance java session 16
Advance java session 16
 
Advance java session 15
Advance java session 15Advance java session 15
Advance java session 15
 
Advance java session 14
Advance java session 14Advance java session 14
Advance java session 14
 
Advance java session 12
Advance java session 12Advance java session 12
Advance java session 12
 
Advance java session 11
Advance java session 11Advance java session 11
Advance java session 11
 
Advance java session 9
Advance java session 9Advance java session 9
Advance java session 9
 
Advance java session 8
Advance java session 8Advance java session 8
Advance java session 8
 
Advance java session 7
Advance java session 7Advance java session 7
Advance java session 7
 
Advance java session 6
Advance java session 6Advance java session 6
Advance java session 6
 
Advance java session 5
Advance java session 5Advance java session 5
Advance java session 5
 
Advance java session 4
Advance java session 4Advance java session 4
Advance java session 4
 
Advance java session 3
Advance java session 3Advance java session 3
Advance java session 3
 
Advance java session 2
Advance java session 2Advance java session 2
Advance java session 2
 
JEE session 1
JEE session 1JEE session 1
JEE session 1
 
01 introduction to struts2
01 introduction to struts201 introduction to struts2
01 introduction to struts2
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Advance java session 10

  • 2. Agenda • Static v/s dynamic text • Directives • JSP Scriptlets • JSP declarations • Expression • Action
  • 3. Static v/s Dynamic Text • JSP is an html or xhtml page which contains some server side scripting • Static contents are directly placed inside _jspService method • Dynamic contents basically control how the resulting servlets should be generated • We can also override the jspInit() and jsDestroy() method using scripting elements • We cannot override _jspService method()
  • 4. Types of scripting • Expressions – Format: <%= expression %> – Evaluated and inserted into the servlet’s output. I.e., results in something like out.print(expression) • Scriptlets – Format: <% code %> – Inserted verbatim into the servlet’s _jspService method (called by service) • Declarations – Format: <%! code %> – Inserted verbatim into the body of the servlet class, outside of any existing methods
  • 5. JSP Expression • Format - <%= Java Expression %> • Result - Expression evaluated, converted to String, and placed into HTML page at the place it occurred in JSP page That is, expression placed in _jspService inside out.print • Examples - Current time: <%= new java.util.Date() %> - Your hostname: <%= request.getRemoteHost() %> • XML-compatible syntax - <jsp:expression>Java Expression</jsp:expression> - You cannot mix versions within a single page. You must use XML for entire page if you use jsp:expression
  • 6. JSP Expression with translation Original JSP <H1>A Random Number</H1> <%= Math.random() %> Representative resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println("<H1>A Random Number</H1>"); out.println(Math.random()) ... }
  • 7. Predefined Variables • Request - The HttpServletRequest (1st argument to service/doGet) • response - The HttpServletResponse (2nd arg to service/doGet) • out - The Writer (a buffered version of type JspWriter) used to send output to the client • session - The HttpSession associated with the request (unless disabled with the session attribute of the page directive) • application - The ServletContext (for sharing data) as obtained via getServletContext().
  • 8. JSP Scriptlets • Format - <% Java Code %> • Result - Code is inserted verbatim into servlet's _jspService • Example <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %> <% response.setContentType("text/plain"); %> • XML-compatible syntax <jsp:scriptlet>Java Code</jsp:scriptlet>
  • 9. JSP Scriptlets translation Original JSP <H2>foo</H2> <%= bar() %> <% baz(); %> Representative resulting servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(); JspWriter out = response.getWriter(); out.println("<H2>foo</H2>"); out.println(bar()); baz(); }
  • 10. JSP Scriptlets example <!DOCTYPE …><HTML><HEAD><TITLE>Color Testing</TITLE></HEAD> <% String bgColor = request.getParameter("bgColor"); if ((bgColor == null)||(bgColor.trim().equals(""))){ bgColor = "WHITE"; } %> <BODY BGCOLOR="<%= bgColor %>"> <H2 ALIGN="CENTER">Testing a Background of "<%= bgColor %>"</H2> </BODY></HTML>
  • 11. Scriptlets with condition • Point - Scriplets are inserted into servlet exactly as written - Need not be complete Java expressions - Complete expressions are usually clearer and easier to maintain, however Example <% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>lousy</B> day! <% } %> Representative result if (Math.random() < 0.5) { out.println("Have a <B>nice</B> day!"); } else { out.println("Have a <B>lousy</B> day!"); }
  • 12. JSP Declarartions • Format- <%! Java Code %> • Result- Code is inserted verbatim into servlet's class definition,outside of any existing methods • Examples <%! private int someField = 5; %> <%! private void someMethod(...) {...} %> • Design consideration Fields are clearly useful. For methods, it is usually better to define the method in a separate Java class. • XML-compatible syntax <jsp:declaration>Java Code</jsp:declaration>
  • 13. JSP declarations <H1>Some Heading</H1> <%! private String randomHeading() { return("<H2>" + Math.random() + "</H2>"); } %> <%= randomHeading() %> (Alternative: make randomHeading a static method in a separate Java class)
  • 14. Using jspInit and jspDestroy • JSP pages, like regular servlets, sometimes want to use init and destroy • Problem: the servlet that gets built from the JSP page might already use init and destroy - Overriding them would cause problems. - Thus, it is illegal to use JSP declarations to declare init or destroy • Solution: use jspInit and jspDestroy. - The auto-generated servlet is guaranteed to call these methods from init and destroy, but the standard versions of jspInit and jspDestroy are empty (placeholders for you to override).
  • 15. Predefined variable in Declarations • Problem • The predefined variables (request, response, out, session, etc.) are local to the _jspService method. Thus, they are not available to methods defined by JSP declarations or to methods in helper classes. What can you do about this? • Solution: pass them as arguments. E.g. <%!private void someMethod(HttpSession s) { doSomethingWith(s); }%> <% someMethod(session); %> Note that the println method of JspWriter throws IOException Use “throws IOException” for methods that use println
  • 16. Summary • JSP Expressions - Format: <%= expression %> - Wrapped in out.print and inserted into _jspService • JSP Scriptlets - Format: <% code %> - Inserted verbatim into the servlet’s _jspService method • JSP Declarations - Format: <%! code %> - Inserted verbatim into the body of the servlet class
  • 17. Summary • Predefined variables - request, response, out, session, application • Limit the Java code that is directly in page - Use helper classes, beans, servlet/JSP combo (MVC), - JSP expression language, custom tags