SlideShare uma empresa Scribd logo
1 de 23
Chapter 11 Servlets and Java Server Pages
11.1 Overview of Servlets - A servlet is a compiled Java class - Servlets are executed on the server system under the control of the Web server - Servlets are managed by the  servlet container , or servlet engine - Servlets are called through HTML - Servlets receive requests and return responses,  both of which are supported by the HTTP protocol - When the Web server receives a request that is for a servlet, the request is passed to the servlet container - The container makes sure the servlet is loaded and calls it - The servlet call has two parameter objects, one  with the request and one for the response - When the servlet is finished, the container  reinitializes itself and returns control to the Web server
11.1 Overview of Servlets  (continued) - Servlets are used 1) as alternatives to CGI, and 2) as alternatives to Apache modules -  Servlet Advantages : - Can be faster than CGI, because they are run in the server process - Have direct access to Java APIs - Because they continue to run (unlike CGI  programs), they can save state information - Have the usual benefits of being written in Java (platform independence, ease of programming)
11.2 Servlet Details - All servlets are classes that either implement the Servlet  interface or extend a class that implements the  Servlet  interface - The  Servlet  interface provides the interfaces for  the methods that manage servlets and their  interactions with clients - The Servlet interface declares three methods that are called by the servlet container (the  life-cycle  methods) -  init  - initializes the servlet and prepares it to  respond to client requests  -  service  - controls how the servlet responds to  requests -  destroy  - takes the servlet out of service
11.2 Servlet Details  (continued) - The  Servlet  interface declares two methods that  are used by the servlet: -  getServletConfig  - to get initialization and  startup parameters for itself -  getServletInfo  - to allow the servlet to return  info about itself (author, version #, etc.) to clients - Most user-written servlet classes are extensions to  HttpServlet  (which is an extension of  GenericServlet , which implements the  Servlet Interface) - Two other necessary interfaces:  -  ServletResponse  – to encapsulate the  communications, client to server -  ServletRequest  – to encapsulate the  communications, server to client - Provides servlet access to  ServletOutputStream
11.2 Servlet Details  (continued) -  HttpServlet  – an abstract class - Extends  GenericServlet - Every subclass of  HttpServlet  MUST override at least one of the methods of  HttpServlet doGet * doPost * doPut * doDelete * init destroy getServletInfo * Called by the server
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
11.3 A Survey Example --> Show  conelec2.html  and its display (w/browser) -  The servlet : - To accumulate voting totals, it must write a file on the server - The file will be read and written as an object (the array of vote totals) using ObjectInputStream - An object of this class is created with its constructor, passing an object of class FileInputStream , whose constructor is called with the file variable name as a parameter ObjectInputStream indat =  new ObjectInputStream( new FileInputStream( File_variable_name )); - On input, the contents of the file will be cast to integer array
11.3 A Survey Example  (continued) - The servlet must access the form data from the client - This is done with the  getParameter  method of the request object, passing a literal string with the name of the form element e.g., if the form has an element named  zip zip = request.getParameter("zip"); - If an element has no value and its value is  requested by  getParameter , the returned value is  null - If a form value is not a string, the returned string must be parsed to get the value - e.g., suppose the value is an integer literal - A string that contains an integer literal can be converted to an integer with the  parseInt method of the wrapper class for  int ,  Integer   price = Integer.parseInt( request.getParameter("price"));
11.3 A Survey Example  (continued) - The file structure is an array of 14 integers, 7 votes for females and 7 votes for males -  Servlet actions : If the votes data array exists read the votes array from the data file else create the votes array Get the gender form value Get the form value for the new vote and convert it to an integer Add the vote to the votes array Write the votes array to the votes file Produce the return HTML document that shows the current results of the survey - Every voter will get the current totals --> Show the servlet,  Survey.java --> Show Figure 11.4
11.4 Storing Information about Clients - A  session  is the collection of all of the requests  made by a particular browser from the time the  browser is started until the user exits the browser - The HTTP protocol is stateless - But, there are several reasons why it is useful for the server to relate a request to a session - Shopping carts for many different simultaneous customers - Customer profiling for advertising - Customized interfaces for specific clients -  Approaches to storing client information: - Store it on the server – too much to store! - Store it on the client machine - this works -  Cookies - A cookie is an object sent by the server to the client
11.4 Storing Information about Clients (continued) - Every HTTP communication between the browser and the server includes information in its header about the message  - At the time a cookie is created, it is given a  lifetime - Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included - A browser can be set to reject all cookies - A cookie object has data members and methods - Data members to store lifetime, name, and a  value (the cookies’ value) - Methods:  setComment ,  setMaxAge ,  setValue , getMaxAge ,  getName , and  getValue - Cookies are created with the  Cookie  constructor Cookie newCookie =  new Cookie(gender, vote);
11.4 Storing Information about Clients (continued) - By default, a cookie’s lifetime is the current  session - If you want it to be longer, use  setMaxAge - A cookie is attached to the response with  addCookie -  Order in which the response must be built : 1. Add cookies 2. Set content type 3. Get response output stream  4. Place info in the response - The browser does nothing with cookies, other  than storing them and passing them back - A servlet gets a cookie from the browser with the  getCookies  method Cookie theCookies []; … theCookies = request.getCookies(); - A Vote Counting Example    Show  ballot.html  and display
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
11.4 Storing Information about Clients (continued) - Create a  Session  object - Put value in the session object with  putValue mySession.putValue("iVoted", "true"); - A session can be killed with the  invalidate   method - A value can be removed with  removeValue - A value can be gotten with  getValue( name ) - All names of values can be gotten with getValueNames    SHOW  VoteCounter2.java
11.5 Java Server Pages - Motivation - Servlets require mixing of XHTML into Java - JSP mixes code into XHTML, although the code can be in a separate file  - Servlets are more appropriate when most of the document to be returned is dynamically  generated - JSP is more appropriate when most of the  document to be returned is predefined - JSP Documents - Are converted to servlets - Consist of four different kinds of elements: 1. Directives – messages to the JSP container 2. XHTML or XML markup – called  template text - The static part of the document 3. Action elements 4. Scriptlets
11.5 Java Server Pages  (continued) -  Action elements - Dynamically create content - The output of a JSP document is a combination of its template text and the output of its action elements -  Appear in three different categories: 1. Standard – defined by the JSP spec; limited scope and value 2. Custom – defined by an organization for  their particular needs 3. JSP Standard Tag Library (JSTL) – created to meet the frequent needs not met by the standard action elements - Consists of five libraries -  Differences between JSTL action elements and a programming language : 1. The syntax is different 2. Action elements are much easier to use  than a programming language
11.5 Java Server Pages  (continued) -  Directives - Tags that use  <%@  and  %>  delimiters - The most common directives are  page  and taglib -  page  is used to specify attributes, such as contentType <%@ page contentType =  ″ text/html ″  %> -  taglib  is used to specify a library of action  elements <%@ taglib prefix =  ″ c ″ uri =  ″ http:// java.sun.com/jsp/jstl/core ″  %> -  Scriptlets - Java code scripts that are embedded in JSP documents - Scriptlets are copied into the output of a JSP document
11.5 Java Server Pages  (continued) -  Scriptlets  (continued) - Four kinds of things that can appear in a  scriptlet: 1. Comments (in Java form) 2. Scriptlet code (Java code in a  <%  ..  %>  tag) 3. Expressions 4. Declarations (not discussed here) - Expressions are used to insert values into the response <%=  expression  %>    SHOW  tempconvert0.html  and tempconvert0.jsp - These can be combined - Need to be able to determine which call it is - One way: use  getParameter  and test against null    SHOW  tempconvert1.jsp
11.5 Java Server Pages  (continued) -  Scriptlets  (continued) - In JSP 1.1, all dynamic parts were created with scriptlets, but that puts lots of Java in  documents – not better than servlets - Since the Expression Language and JSTL were added to JSP, scriptlets are no longer needed -  JSP Expression Language - Similar to the expressions of JavaScript - For example, arithmetic between a string and a number - Has no control statements - Syntax:  ${  expression  } - Consist of literals, arithmetic operators, implicit variables (for form data), and normal variables - EL is used to set the attribute values of action elements (always strings)
11.5 Java Server Pages  (continued) -  JSP Expression Language  (continued) - EL data often comes from forms - The implicit variable,  param , stores a  collection of all form data values ${param.address} - If the form data name has special characters: ${param[ ′ cust-address ′ ]} - Another implicit variable:  pageContext - Has lots of info about the request e.g.,  contentType ,  contentLength ,  remoteAddr - Output is usually created with  out <c:out value =  ″ ${param.address} ″  />    SHOW  tempconvert2.html  and  tempconvert2.jsp
11.5 Java Server Pages  (continued) -  JSTL Control Action Elements - Flow control elements – the Core library of JSTL - Selection –  if  element - Often used to choose whether it is the first call  of a combined document <c:if test =  ″ ${pageContext.request.method ==  ′ POST ′ } ″ > … </c:if>    SHOW  tempconvert3.jsp - Loops –  forEach  element (an iterator) - Often used for checkboxes and menus to  determine the values of the parts - The  parmValues  implicit variable has an array of the values in checkboxes and menus
11.5 Java Server Pages  (continued) -  JSTL Control Action Elements  (continued) -  forEach  has two attributes,  items  and  var ,  which get the specific item and its value - If we had a collection of checkboxes named topping <c:forEach items =  ″ ${paramValues.topping} ″ var =  ″ top ″ > <c:out value =  ″ ${top} ″>  <br /> </c:forEach> -  forEach  can also be used for counting loops <c:forEach begin =  ″ 1 ″   end =  ″ 10 ″ > … </c:forEach> - The  choose  element – to build switch constructs -  choose , which has no attributes, uses two  other elements,  when  and  otherwise -  when  has the  test  attribute, which has  the control expression - Radio buttons require a switch construct  SHOW  testradio.jsp

Mais conteúdo relacionado

Mais procurados

Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface pptTaha Malampatti
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet backdoor
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
 
Lecture 3: Servlets - Session Management
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session ManagementFahad Golra
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtapVikas Jagtap
 
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 servletsJavaEE Trainers
 
.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8aminmesbahi
 
Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Kasun Madusanke
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
Box connector Mule ESB Integration
Box connector Mule ESB IntegrationBox connector Mule ESB Integration
Box connector Mule ESB IntegrationAnilKumar Etagowni
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
Database connect
Database connectDatabase connect
Database connectYoga Raja
 

Mais procurados (20)

Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
 
Servlets
ServletsServlets
Servlets
 
Lecture 3: Servlets - Session Management
Lecture 3:  Servlets - Session ManagementLecture 3:  Servlets - Session Management
Lecture 3: Servlets - Session Management
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
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
 
.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8.NET Core, ASP.NET Core Course, Session 8
.NET Core, ASP.NET Core Course, Session 8
 
Database Connection Pooling With c3p0
Database Connection Pooling With c3p0Database Connection Pooling With c3p0
Database Connection Pooling With c3p0
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
Connection Pooling
Connection PoolingConnection Pooling
Connection Pooling
 
Box connector Mule ESB Integration
Box connector Mule ESB IntegrationBox connector Mule ESB Integration
Box connector Mule ESB Integration
 
L12: REST Service
L12: REST ServiceL12: REST Service
L12: REST Service
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Database connect
Database connectDatabase connect
Database connect
 

Destaque (14)

08052917365603
0805291736560308052917365603
08052917365603
 
Jsp
JspJsp
Jsp
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
 
29 Jsp
29 Jsp29 Jsp
29 Jsp
 
Jsp
JspJsp
Jsp
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
 
Analytical Investments Trading System Presentation
Analytical Investments   Trading System PresentationAnalytical Investments   Trading System Presentation
Analytical Investments Trading System Presentation
 
5 To Dos for Recruiters in a Weak Economy
5 To Dos for Recruiters in a Weak Economy5 To Dos for Recruiters in a Weak Economy
5 To Dos for Recruiters in a Weak Economy
 
Erica Lehman Emmer Presentation
Erica Lehman Emmer PresentationErica Lehman Emmer Presentation
Erica Lehman Emmer Presentation
 
Zamchick Presentation Tech
Zamchick Presentation TechZamchick Presentation Tech
Zamchick Presentation Tech
 
Guatemala 2013(rev1)
Guatemala 2013(rev1)Guatemala 2013(rev1)
Guatemala 2013(rev1)
 
Social Networking and the Church
Social Networking and the ChurchSocial Networking and the Church
Social Networking and the Church
 
ACH 121 Lecture 02 (Arch. Basic Services )
ACH 121 Lecture 02 (Arch. Basic Services )ACH 121 Lecture 02 (Arch. Basic Services )
ACH 121 Lecture 02 (Arch. Basic Services )
 
Jsf 2 slideshare
Jsf 2 slideshareJsf 2 slideshare
Jsf 2 slideshare
 

Semelhante a W3 C11

Servlet.ppt
Servlet.pptServlet.ppt
Servlet.pptkstalin2
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteTushar B Kute
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentjoearunraja2
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0megrhi haikel
 
Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Anas Sa
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivitypkaviya
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: ServletsFahad Golra
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technologyvikram singh
 

Semelhante a W3 C11 (20)

Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Basics Of Servlet
Basics Of ServletBasics Of Servlet
Basics Of Servlet
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
Servlets
ServletsServlets
Servlets
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
 
Servlets
ServletsServlets
Servlets
 
Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Servlets Java Slides & Presentation
Servlets Java Slides & Presentation
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
19servlets
19servlets19servlets
19servlets
 

Último

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Último (20)

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

W3 C11

  • 1. Chapter 11 Servlets and Java Server Pages
  • 2. 11.1 Overview of Servlets - A servlet is a compiled Java class - Servlets are executed on the server system under the control of the Web server - Servlets are managed by the servlet container , or servlet engine - Servlets are called through HTML - Servlets receive requests and return responses, both of which are supported by the HTTP protocol - When the Web server receives a request that is for a servlet, the request is passed to the servlet container - The container makes sure the servlet is loaded and calls it - The servlet call has two parameter objects, one with the request and one for the response - When the servlet is finished, the container reinitializes itself and returns control to the Web server
  • 3. 11.1 Overview of Servlets (continued) - Servlets are used 1) as alternatives to CGI, and 2) as alternatives to Apache modules - Servlet Advantages : - Can be faster than CGI, because they are run in the server process - Have direct access to Java APIs - Because they continue to run (unlike CGI programs), they can save state information - Have the usual benefits of being written in Java (platform independence, ease of programming)
  • 4. 11.2 Servlet Details - All servlets are classes that either implement the Servlet interface or extend a class that implements the Servlet interface - The Servlet interface provides the interfaces for the methods that manage servlets and their interactions with clients - The Servlet interface declares three methods that are called by the servlet container (the life-cycle methods) - init - initializes the servlet and prepares it to respond to client requests - service - controls how the servlet responds to requests - destroy - takes the servlet out of service
  • 5. 11.2 Servlet Details (continued) - The Servlet interface declares two methods that are used by the servlet: - getServletConfig - to get initialization and startup parameters for itself - getServletInfo - to allow the servlet to return info about itself (author, version #, etc.) to clients - Most user-written servlet classes are extensions to HttpServlet (which is an extension of GenericServlet , which implements the Servlet Interface) - Two other necessary interfaces: - ServletResponse – to encapsulate the communications, client to server - ServletRequest – to encapsulate the communications, server to client - Provides servlet access to ServletOutputStream
  • 6. 11.2 Servlet Details (continued) - HttpServlet – an abstract class - Extends GenericServlet - Every subclass of HttpServlet MUST override at least one of the methods of HttpServlet doGet * doPost * doPut * doDelete * init destroy getServletInfo * Called by the server
  • 7.
  • 8. 11.3 A Survey Example --> Show conelec2.html and its display (w/browser) - The servlet : - To accumulate voting totals, it must write a file on the server - The file will be read and written as an object (the array of vote totals) using ObjectInputStream - An object of this class is created with its constructor, passing an object of class FileInputStream , whose constructor is called with the file variable name as a parameter ObjectInputStream indat = new ObjectInputStream( new FileInputStream( File_variable_name )); - On input, the contents of the file will be cast to integer array
  • 9. 11.3 A Survey Example (continued) - The servlet must access the form data from the client - This is done with the getParameter method of the request object, passing a literal string with the name of the form element e.g., if the form has an element named zip zip = request.getParameter(&quot;zip&quot;); - If an element has no value and its value is requested by getParameter , the returned value is null - If a form value is not a string, the returned string must be parsed to get the value - e.g., suppose the value is an integer literal - A string that contains an integer literal can be converted to an integer with the parseInt method of the wrapper class for int , Integer price = Integer.parseInt( request.getParameter(&quot;price&quot;));
  • 10. 11.3 A Survey Example (continued) - The file structure is an array of 14 integers, 7 votes for females and 7 votes for males - Servlet actions : If the votes data array exists read the votes array from the data file else create the votes array Get the gender form value Get the form value for the new vote and convert it to an integer Add the vote to the votes array Write the votes array to the votes file Produce the return HTML document that shows the current results of the survey - Every voter will get the current totals --> Show the servlet, Survey.java --> Show Figure 11.4
  • 11. 11.4 Storing Information about Clients - A session is the collection of all of the requests made by a particular browser from the time the browser is started until the user exits the browser - The HTTP protocol is stateless - But, there are several reasons why it is useful for the server to relate a request to a session - Shopping carts for many different simultaneous customers - Customer profiling for advertising - Customized interfaces for specific clients - Approaches to storing client information: - Store it on the server – too much to store! - Store it on the client machine - this works - Cookies - A cookie is an object sent by the server to the client
  • 12. 11.4 Storing Information about Clients (continued) - Every HTTP communication between the browser and the server includes information in its header about the message - At the time a cookie is created, it is given a lifetime - Every time the browser sends a request to the server that created the cookie, while the cookie is still alive, the cookie is included - A browser can be set to reject all cookies - A cookie object has data members and methods - Data members to store lifetime, name, and a value (the cookies’ value) - Methods: setComment , setMaxAge , setValue , getMaxAge , getName , and getValue - Cookies are created with the Cookie constructor Cookie newCookie = new Cookie(gender, vote);
  • 13. 11.4 Storing Information about Clients (continued) - By default, a cookie’s lifetime is the current session - If you want it to be longer, use setMaxAge - A cookie is attached to the response with addCookie - Order in which the response must be built : 1. Add cookies 2. Set content type 3. Get response output stream 4. Place info in the response - The browser does nothing with cookies, other than storing them and passing them back - A servlet gets a cookie from the browser with the getCookies method Cookie theCookies []; … theCookies = request.getCookies(); - A Vote Counting Example  Show ballot.html and display
  • 14.
  • 15. 11.4 Storing Information about Clients (continued) - Create a Session object - Put value in the session object with putValue mySession.putValue(&quot;iVoted&quot;, &quot;true&quot;); - A session can be killed with the invalidate method - A value can be removed with removeValue - A value can be gotten with getValue( name ) - All names of values can be gotten with getValueNames  SHOW VoteCounter2.java
  • 16. 11.5 Java Server Pages - Motivation - Servlets require mixing of XHTML into Java - JSP mixes code into XHTML, although the code can be in a separate file - Servlets are more appropriate when most of the document to be returned is dynamically generated - JSP is more appropriate when most of the document to be returned is predefined - JSP Documents - Are converted to servlets - Consist of four different kinds of elements: 1. Directives – messages to the JSP container 2. XHTML or XML markup – called template text - The static part of the document 3. Action elements 4. Scriptlets
  • 17. 11.5 Java Server Pages (continued) - Action elements - Dynamically create content - The output of a JSP document is a combination of its template text and the output of its action elements - Appear in three different categories: 1. Standard – defined by the JSP spec; limited scope and value 2. Custom – defined by an organization for their particular needs 3. JSP Standard Tag Library (JSTL) – created to meet the frequent needs not met by the standard action elements - Consists of five libraries - Differences between JSTL action elements and a programming language : 1. The syntax is different 2. Action elements are much easier to use than a programming language
  • 18. 11.5 Java Server Pages (continued) - Directives - Tags that use <%@ and %> delimiters - The most common directives are page and taglib - page is used to specify attributes, such as contentType <%@ page contentType = ″ text/html ″ %> - taglib is used to specify a library of action elements <%@ taglib prefix = ″ c ″ uri = ″ http:// java.sun.com/jsp/jstl/core ″ %> - Scriptlets - Java code scripts that are embedded in JSP documents - Scriptlets are copied into the output of a JSP document
  • 19. 11.5 Java Server Pages (continued) - Scriptlets (continued) - Four kinds of things that can appear in a scriptlet: 1. Comments (in Java form) 2. Scriptlet code (Java code in a <% .. %> tag) 3. Expressions 4. Declarations (not discussed here) - Expressions are used to insert values into the response <%= expression %>  SHOW tempconvert0.html and tempconvert0.jsp - These can be combined - Need to be able to determine which call it is - One way: use getParameter and test against null  SHOW tempconvert1.jsp
  • 20. 11.5 Java Server Pages (continued) - Scriptlets (continued) - In JSP 1.1, all dynamic parts were created with scriptlets, but that puts lots of Java in documents – not better than servlets - Since the Expression Language and JSTL were added to JSP, scriptlets are no longer needed - JSP Expression Language - Similar to the expressions of JavaScript - For example, arithmetic between a string and a number - Has no control statements - Syntax: ${ expression } - Consist of literals, arithmetic operators, implicit variables (for form data), and normal variables - EL is used to set the attribute values of action elements (always strings)
  • 21. 11.5 Java Server Pages (continued) - JSP Expression Language (continued) - EL data often comes from forms - The implicit variable, param , stores a collection of all form data values ${param.address} - If the form data name has special characters: ${param[ ′ cust-address ′ ]} - Another implicit variable: pageContext - Has lots of info about the request e.g., contentType , contentLength , remoteAddr - Output is usually created with out <c:out value = ″ ${param.address} ″ />  SHOW tempconvert2.html and tempconvert2.jsp
  • 22. 11.5 Java Server Pages (continued) - JSTL Control Action Elements - Flow control elements – the Core library of JSTL - Selection – if element - Often used to choose whether it is the first call of a combined document <c:if test = ″ ${pageContext.request.method == ′ POST ′ } ″ > … </c:if>  SHOW tempconvert3.jsp - Loops – forEach element (an iterator) - Often used for checkboxes and menus to determine the values of the parts - The parmValues implicit variable has an array of the values in checkboxes and menus
  • 23. 11.5 Java Server Pages (continued) - JSTL Control Action Elements (continued) - forEach has two attributes, items and var , which get the specific item and its value - If we had a collection of checkboxes named topping <c:forEach items = ″ ${paramValues.topping} ″ var = ″ top ″ > <c:out value = ″ ${top} ″> <br /> </c:forEach> - forEach can also be used for counting loops <c:forEach begin = ″ 1 ″ end = ″ 10 ″ > … </c:forEach> - The choose element – to build switch constructs - choose , which has no attributes, uses two other elements, when and otherwise - when has the test attribute, which has the control expression - Radio buttons require a switch construct  SHOW testradio.jsp