SlideShare a Scribd company logo
1 of 13
Java Server Pages
2
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " +
"Transitional//EN">n";
out.println(docType +
"<HTML>n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>n" +
"<BODY BGCOLOR="#FDF5E6">n" +
"<H1>Hello World</H1>n" +
"</BODY></HTML>");
}
} This is mostly Java with a little HTML mixed in
 The purpose of a servlet is to create a Web page in response
to a client request
 Servlets are written in Java, with a little HTML mixed in
◦ The HTML is enclosed in out.println( ) statements
 JSP (Java Server Pages) is an alternate way of creating
servlets
◦ JSP is written as ordinary HTML, with a little Java mixed in
◦ The Java is enclosed in special tags, such as <% ... %>
◦ The HTML is known as the template text
 JSP files must have the extension .jsp
◦ JSP is translated into a Java servlet, which is then compiled
◦ Servlets are run in the usual way
◦ The browser or other client sees only the resultant HTML, as usual
 Tomcat knows how to handle servlets and JSP pages
3
 There is more than one type of JSP “tag,” depending on
what you want done with the Java
 <%= expression %>
◦ The expression is evaluated and the result is inserted into the
HTML page
 <% code %>
◦ The code is inserted into the servlet's service method
◦ This construction is called a scriptlet
 <%! declarations %>
◦ The declarations are inserted into the servlet class, not into a
method
4
 <HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>
 Notes:
◦ The <%= ... %> tag is used, because we are computing a
value and inserting it into the HTML
◦ The fully qualified name (java.util.Date) is used,
instead of the short name (Date), because we haven’t
yet talked about how to do import declarations
5
 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() %>
6
 Scriptlets are enclosed in <% ... %> tags
◦ Scriptlets do 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 servlet exactly 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!
<% } %>
7
 Use <%! ... %> for declarations to be added to your
servlet class, not to any particular method
◦ Caution: Servlets 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 int accessCount = 0; %>
Accesses to page since server reboot:
<%= ++accessCount %>
 You can use <%! ... %> to declare methods as easily as
to declare variables
8
 Directives affect the servlet class itself
 A directive has the form:
<%@ directive attribute="value" %>
or
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
 The most useful directive is page, which lets you
import packages
◦ Example: <%@ page import="java.util.*" %>
9
 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
◦ If the URL begins with a slash, it is treated as relative to
the home directory of the Web server
 The include directive is especially useful for
inserting things like navigation bars
10
 Actions are XML-syntax tags used to control the
servlet engine
 <jsp:include page="URL " flush="true" />
◦ 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
11
 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"/>
12
13

More Related Content

What's hot

Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Alex Fernandez
 
Web components
Web componentsWeb components
Web componentsMohd Saeed
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with ReduxVedran Blaženka
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
React for Dummies
React for DummiesReact for Dummies
React for DummiesMitch Chen
 
Test and profile your Windows Phone 8 App
Test and profile your Windows Phone 8 AppTest and profile your Windows Phone 8 App
Test and profile your Windows Phone 8 AppMichele Capra
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and ReduxGlib Kechyn
 
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...DicodingEvent
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring DataCorneil du Plessis
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 

What's hot (19)

2310 b 13
2310 b 132310 b 13
2310 b 13
 
Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Using schemas in parsing xml part 2
Using schemas in parsing xml part 2
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
Web components
Web componentsWeb components
Web components
 
JS basics
JS basicsJS basics
JS basics
 
29 Jsp
29 Jsp29 Jsp
29 Jsp
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
Suggest.js
Suggest.jsSuggest.js
Suggest.js
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
Test and profile your Windows Phone 8 App
Test and profile your Windows Phone 8 AppTest and profile your Windows Phone 8 App
Test and profile your Windows Phone 8 App
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
Dicoding Developer Coaching #19: Android | Menyimpan Database Secara Local di...
 
2310 b 06
2310 b 062310 b 06
2310 b 06
 
Polyglot persistence with Spring Data
Polyglot persistence with Spring DataPolyglot persistence with Spring Data
Polyglot persistence with Spring Data
 
Introduction to ASP.Net Viewstate
Introduction to ASP.Net ViewstateIntroduction to ASP.Net Viewstate
Introduction to ASP.Net Viewstate
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 

Viewers also liked

Petit Club TV Connectée - Présentation Philippe Citroën (Sony)
Petit Club TV Connectée - Présentation Philippe Citroën (Sony)Petit Club TV Connectée - Présentation Philippe Citroën (Sony)
Petit Club TV Connectée - Présentation Philippe Citroën (Sony)Petit Web
 
White Paper: Mobile Banking: How to Balance Opportunities and Threats
White Paper: Mobile Banking: How to Balance Opportunities and ThreatsWhite Paper: Mobile Banking: How to Balance Opportunities and Threats
White Paper: Mobile Banking: How to Balance Opportunities and ThreatsEMC
 
EMC FAST VP for Unified Storage Systems
EMC FAST VP for Unified Storage Systems EMC FAST VP for Unified Storage Systems
EMC FAST VP for Unified Storage Systems EMC
 
Demystifying Big Data
Demystifying Big Data Demystifying Big Data
Demystifying Big Data EMC
 
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...EMC
 
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS EMC
 
Desafio docente 6o interiores
Desafio docente 6o interioresDesafio docente 6o interiores
Desafio docente 6o interioresZona Escolar 415
 

Viewers also liked (7)

Petit Club TV Connectée - Présentation Philippe Citroën (Sony)
Petit Club TV Connectée - Présentation Philippe Citroën (Sony)Petit Club TV Connectée - Présentation Philippe Citroën (Sony)
Petit Club TV Connectée - Présentation Philippe Citroën (Sony)
 
White Paper: Mobile Banking: How to Balance Opportunities and Threats
White Paper: Mobile Banking: How to Balance Opportunities and ThreatsWhite Paper: Mobile Banking: How to Balance Opportunities and Threats
White Paper: Mobile Banking: How to Balance Opportunities and Threats
 
EMC FAST VP for Unified Storage Systems
EMC FAST VP for Unified Storage Systems EMC FAST VP for Unified Storage Systems
EMC FAST VP for Unified Storage Systems
 
Demystifying Big Data
Demystifying Big Data Demystifying Big Data
Demystifying Big Data
 
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...
White paper: EMC Performance Optimization for Microsoft FAST Search Server 20...
 
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS
An IT-as-a-Service Handbook: 10 Key Steps on the Journey to ITaaS
 
Desafio docente 6o interiores
Desafio docente 6o interioresDesafio docente 6o interiores
Desafio docente 6o interiores
 

Similar to Jsp intro (20)

Jsp & struts
Jsp & strutsJsp & struts
Jsp & struts
 
JSP diana y yo
JSP diana y yoJSP diana y yo
JSP diana y yo
 
JavaServer Pages
JavaServer PagesJavaServer Pages
JavaServer Pages
 
Jsp1
Jsp1Jsp1
Jsp1
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Jsp
JspJsp
Jsp
 
Jsp 01
Jsp 01Jsp 01
Jsp 01
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Java serverpages
Java serverpagesJava serverpages
Java serverpages
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Servlets
ServletsServlets
Servlets
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Presentation
PresentationPresentation
Presentation
 
C:\fakepath\jsp01
C:\fakepath\jsp01C:\fakepath\jsp01
C:\fakepath\jsp01
 
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.pptx
JSP.pptxJSP.pptx
JSP.pptx
 

More from husnara mohammad (16)

Ajax
AjaxAjax
Ajax
 
Log4e
Log4eLog4e
Log4e
 
Hibernate
HibernateHibernate
Hibernate
 
J2EE
J2EEJ2EE
J2EE
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
Java intro
Java introJava intro
Java intro
 
Php with my sql
Php with my sqlPhp with my sql
Php with my sql
 
Asp dot net
Asp dot netAsp dot net
Asp dot net
 
Hibernate introduction
Hibernate introductionHibernate introduction
Hibernate introduction
 
Selenium
SeleniumSelenium
Selenium
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Ruby on Rails
Ruby on RailsRuby on Rails
Ruby on Rails
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Ajax basic intro
Ajax basic introAjax basic intro
Ajax basic intro
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Web attacks
Web attacksWeb attacks
Web attacks
 

Recently uploaded

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
 
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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
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
 

Recently uploaded (20)

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...
 
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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
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
 

Jsp intro

  • 2. 2 public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 " + "Transitional//EN">n"; out.println(docType + "<HTML>n" + "<HEAD><TITLE>Hello</TITLE></HEAD>n" + "<BODY BGCOLOR="#FDF5E6">n" + "<H1>Hello World</H1>n" + "</BODY></HTML>"); } } This is mostly Java with a little HTML mixed in
  • 3.  The purpose of a servlet is to create a Web page in response to a client request  Servlets are written in Java, with a little HTML mixed in ◦ The HTML is enclosed in out.println( ) statements  JSP (Java Server Pages) is an alternate way of creating servlets ◦ JSP is written as ordinary HTML, with a little Java mixed in ◦ The Java is enclosed in special tags, such as <% ... %> ◦ The HTML is known as the template text  JSP files must have the extension .jsp ◦ JSP is translated into a Java servlet, which is then compiled ◦ Servlets are run in the usual way ◦ The browser or other client sees only the resultant HTML, as usual  Tomcat knows how to handle servlets and JSP pages 3
  • 4.  There is more than one type of JSP “tag,” depending on what you want done with the Java  <%= expression %> ◦ The expression is evaluated and the result is inserted into the HTML page  <% code %> ◦ The code is inserted into the servlet's service method ◦ This construction is called a scriptlet  <%! declarations %> ◦ The declarations are inserted into the servlet class, not into a method 4
  • 5.  <HTML> <BODY> Hello!  The time is now <%= new java.util.Date() %> </BODY> </HTML>  Notes: ◦ The <%= ... %> tag is used, because we are computing a value and inserting it into the HTML ◦ The fully qualified name (java.util.Date) is used, instead of the short name (Date), because we haven’t yet talked about how to do import declarations 5
  • 6.  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() %> 6
  • 7.  Scriptlets are enclosed in <% ... %> tags ◦ Scriptlets do 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 servlet exactly 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! <% } %> 7
  • 8.  Use <%! ... %> for declarations to be added to your servlet class, not to any particular method ◦ Caution: Servlets 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 int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %>  You can use <%! ... %> to declare methods as easily as to declare variables 8
  • 9.  Directives affect the servlet class itself  A directive has the form: <%@ directive attribute="value" %> or <%@ directive attribute1="value1" attribute2="value2" ... attributeN="valueN" %>  The most useful directive is page, which lets you import packages ◦ Example: <%@ page import="java.util.*" %> 9
  • 10.  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 ◦ If the URL begins with a slash, it is treated as relative to the home directory of the Web server  The include directive is especially useful for inserting things like navigation bars 10
  • 11.  Actions are XML-syntax tags used to control the servlet engine  <jsp:include page="URL " flush="true" /> ◦ 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 11
  • 12.  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"/> 12
  • 13. 13