SlideShare une entreprise Scribd logo
1  sur  19
Java Servlets
Web Engineering
12/21/2015University of Education, Okara
1
Faraz Hashmi, BSIT-5th Eve, 3011
 Servlets are protocol- and platform-independent server side components,
written in Java, which dynamically extend Java-enabled servers.
 They have become more and more popular as they benefit from all the
advantages of the Java programming language in particular Architecture and
Platform Independence and the ability to build robust and secure
applications.
Servlet
12/21/2015University of Education, Okara
2
 A Servlet, in simple terms, is a Java program running under a web server
taking a 'request' object as an input and responding back by a 'response'
object. Typically a web browser will send the request in HTTP format. The
Servlet container will convert that into a request object. Similarly the
response object - populated by the Servlet is converted into an HTTP
response by the Servlet container.
 This mechanism makes the browser - web server interaction very easy.
Java Servlets are more efficient, easier to use, more powerful, more portable,
and cheaper than traditional CGI and many alternative CGI-like technologies.
(More importantly, Servlet developers tend to get paid more than Perl
programmers)
12/21/2015University of Education, Okara
3
Servlet
 Servlet Mechanism
A servlet is a Java class and thus needs to be executed in a Java Virtual Machine
by a service called a servlet engine. The servlet engine loads the servlet before it
can be used. The servlet then stays loaded until it is unloaded or the servlet
engine is shut down.
12/21/2015University of Education, Okara
4
 Servlet Mechanism
12/21/2015University of Education, Okara
5
Client/
Browser
Server Servlet
JVM
Request
Response
Extends the functionality of
the server by
Generating HTML pages
dynamically
 Servlet Types
 Servlets are based on two main packages
 javax.servlet
 javax.servlet.http
 GenericServlet
 For writing protocol independent servlets
 HttpServlet
 Extends from GenericServlet class
 Adds functionality for writing HTTP specific servlets
12/21/2015University of Education, Okara
6
 Servlet class hierarchy
Object
GenericServlet ServletResponse
HttpServletRequest
ServerRequest
HttpServlet HttpServletResponse
javax.servlet
javax.servlet.http
 Software Requirements
 To use Java servlets, following softwares will be needed
 J2SE
 Additional J2EE based libraries for servlets such as servlet-api.jar, jsp-
api.jar. You can download these APIs separately but they are also
available with the web server you’ll be using.
 A capable servlet web engine (web server)
12/21/2015University of Education, Okara
8
 Environment Setup
Steps
1. Download the Apache Tomcat Server
2. Install Tomcat
3. Set the JAVA_HOME variable
4. Set the CATALINA_HOME variable
5. Set the CLASSPATH variable
6. Test the Server
12/21/2015University of Education, Okara
9
 Servlet Advantages & Disadvantages
The advantage of Servlets is,
 Portability
o Portable across operating systems and across web servers
 Power
o Harness the full power of the core Java APIs: networking and URL access,
multithreading, image manipulation, data compression, JDBC, object
serialization, internationalization
 Efficiency & Endurance
o Memory resident, so invocation highly efficient—no process to spawn or
interpreter to invoke
12/21/2015University of Education, Okara
10
 Servlet Advantages & Disadvantages
 Safety
o Support safe programming since inherit Java’s strong type safety, exception-
handling mechanism
 Elegance
o Code is clean, object-oriented, modular, and simple (i.e.. Session tracking, cookie)
 Integration
o Tightly integrated with the server—translate file paths, perform logging, check
authorization, and MIME type mapping
12/21/2015University of Education, Okara
11
 Servlet Advantages & Disadvantages
The disadvantage of Servlets is,
 Web Administrator will need to learn how to install and maintain Java
Servlets
 Tedious uses of out.println() statements
o Can be remedied by using Java Server Page (JSP)
12/21/2015University of Education, Okara
12
 Types of HTTP Requests
 Get
 Post
 Delete
 Options
 Put
 Trace
12/21/2015University of Education, Okara
13
 How HTTP Sends Request
Client
Server
Servlets
Some HTTP request types
• Get -- Attr/Val pairs attached after ? of URL
E.g. http://www.gmail.com/register?name=ali
• Post -- Attr/Val pairs attatched with the request body
 HTTP Request Example
12/21/2015University of Education, Okara
15
Request parameters etc.
 Servlet Model
12/21/2015University of Education, Okara
16
 Servlet Life Cycle
12/21/2015University of Education, Okara
17
 Initialize
 Service
 Destroy
HelloWorld Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(“Hello World!”);
}
}
12/21/2015University of Education, Okara
18
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/myfirstservlet</url-pattern>
</servlet-mapping>
</web-app>
12/21/2015University of Education, Okara
19

Contenu connexe

Tendances (20)

Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Jdbc connectivity in java
Jdbc connectivity in javaJdbc connectivity in java
Jdbc connectivity in java
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Spring boot
Spring bootSpring boot
Spring boot
 
4.C#
4.C#4.C#
4.C#
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
servlet in java
servlet in javaservlet in java
servlet in java
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
 
Software architectural patterns - A Quick Understanding Guide
Software architectural patterns - A Quick Understanding GuideSoftware architectural patterns - A Quick Understanding Guide
Software architectural patterns - A Quick Understanding Guide
 
React js
React jsReact js
React js
 
React native
React nativeReact native
React native
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 

Similaire à Java Servlets

Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxkarthiksmart21
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGPrabu U
 
JEE Course - The Web Tier
JEE Course - The Web TierJEE Course - The Web Tier
JEE Course - The Web Tierodedns
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and ServletsRaghu nath
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music storeADEEBANADEEM
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technologyMinal Maniar
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
Liit tyit sem 5 enterprise java unit 1 notes 2018
Liit tyit sem 5 enterprise java  unit 1 notes 2018 Liit tyit sem 5 enterprise java  unit 1 notes 2018
Liit tyit sem 5 enterprise java unit 1 notes 2018 tanujaparihar
 
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.pptsindhu991994
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servletssbd6985
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06Ankit Dubey
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Rajesh Moorjani
 
Lecture 19 - Dynamic Web - JAVA - Part 1.ppt
Lecture 19 - Dynamic Web - JAVA - Part 1.pptLecture 19 - Dynamic Web - JAVA - Part 1.ppt
Lecture 19 - Dynamic Web - JAVA - Part 1.pptKalsoomTahir2
 

Similaire à Java Servlets (20)

Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
 
Weblogic
WeblogicWeblogic
Weblogic
 
JEE Course - The Web Tier
JEE Course - The Web TierJEE Course - The Web Tier
JEE Course - The Web Tier
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
Ppt for Online music store
Ppt for Online music storePpt for Online music store
Ppt for Online music store
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Liit tyit sem 5 enterprise java unit 1 notes 2018
Liit tyit sem 5 enterprise java  unit 1 notes 2018 Liit tyit sem 5 enterprise java  unit 1 notes 2018
Liit tyit sem 5 enterprise java unit 1 notes 2018
 
nagavarthini ppt.pptx
nagavarthini ppt.pptxnagavarthini ppt.pptx
nagavarthini ppt.pptx
 
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Java servlets
Java servletsJava servlets
Java servlets
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
 
Coursejspservlets00
Coursejspservlets00Coursejspservlets00
Coursejspservlets00
 
J servlets
J servletsJ servlets
J servlets
 
Lecture 19 - Dynamic Web - JAVA - Part 1.ppt
Lecture 19 - Dynamic Web - JAVA - Part 1.pptLecture 19 - Dynamic Web - JAVA - Part 1.ppt
Lecture 19 - Dynamic Web - JAVA - Part 1.ppt
 

Plus de university of education,Lahore

Plus de university of education,Lahore (20)

Activites and Time Planning
 Activites and Time Planning Activites and Time Planning
Activites and Time Planning
 
Steganography
SteganographySteganography
Steganography
 
Classical Encryption Techniques
Classical Encryption TechniquesClassical Encryption Techniques
Classical Encryption Techniques
 
Activites and Time Planning
Activites and Time PlanningActivites and Time Planning
Activites and Time Planning
 
OSI Security Architecture
OSI Security ArchitectureOSI Security Architecture
OSI Security Architecture
 
Network Security Terminologies
Network Security TerminologiesNetwork Security Terminologies
Network Security Terminologies
 
Project Scheduling, Planning and Risk Management
Project Scheduling, Planning and Risk ManagementProject Scheduling, Planning and Risk Management
Project Scheduling, Planning and Risk Management
 
Software Testing and Debugging
Software Testing and DebuggingSoftware Testing and Debugging
Software Testing and Debugging
 
ePayment Methods
ePayment MethodsePayment Methods
ePayment Methods
 
SEO
SEOSEO
SEO
 
A Star Search
A Star SearchA Star Search
A Star Search
 
Enterprise Application Integration
Enterprise Application IntegrationEnterprise Application Integration
Enterprise Application Integration
 
Uml Diagrams
Uml DiagramsUml Diagrams
Uml Diagrams
 
eDras Max
eDras MaxeDras Max
eDras Max
 
RAD Model
RAD ModelRAD Model
RAD Model
 
Microsoft Project
Microsoft ProjectMicrosoft Project
Microsoft Project
 
Itertaive Process Development
Itertaive Process DevelopmentItertaive Process Development
Itertaive Process Development
 
Computer Aided Software Engineering Nayab Awan
Computer Aided Software Engineering Nayab AwanComputer Aided Software Engineering Nayab Awan
Computer Aided Software Engineering Nayab Awan
 
Lect 2 assessing the technology landscape
Lect 2 assessing the technology landscapeLect 2 assessing the technology landscape
Lect 2 assessing the technology landscape
 
system level requirements gathering and analysis
system level requirements gathering and analysissystem level requirements gathering and analysis
system level requirements gathering and analysis
 

Dernier

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Dernier (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Java Servlets

  • 1. Java Servlets Web Engineering 12/21/2015University of Education, Okara 1 Faraz Hashmi, BSIT-5th Eve, 3011
  • 2.  Servlets are protocol- and platform-independent server side components, written in Java, which dynamically extend Java-enabled servers.  They have become more and more popular as they benefit from all the advantages of the Java programming language in particular Architecture and Platform Independence and the ability to build robust and secure applications. Servlet 12/21/2015University of Education, Okara 2
  • 3.  A Servlet, in simple terms, is a Java program running under a web server taking a 'request' object as an input and responding back by a 'response' object. Typically a web browser will send the request in HTTP format. The Servlet container will convert that into a request object. Similarly the response object - populated by the Servlet is converted into an HTTP response by the Servlet container.  This mechanism makes the browser - web server interaction very easy. Java Servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI and many alternative CGI-like technologies. (More importantly, Servlet developers tend to get paid more than Perl programmers) 12/21/2015University of Education, Okara 3 Servlet
  • 4.  Servlet Mechanism A servlet is a Java class and thus needs to be executed in a Java Virtual Machine by a service called a servlet engine. The servlet engine loads the servlet before it can be used. The servlet then stays loaded until it is unloaded or the servlet engine is shut down. 12/21/2015University of Education, Okara 4
  • 5.  Servlet Mechanism 12/21/2015University of Education, Okara 5 Client/ Browser Server Servlet JVM Request Response Extends the functionality of the server by Generating HTML pages dynamically
  • 6.  Servlet Types  Servlets are based on two main packages  javax.servlet  javax.servlet.http  GenericServlet  For writing protocol independent servlets  HttpServlet  Extends from GenericServlet class  Adds functionality for writing HTTP specific servlets 12/21/2015University of Education, Okara 6
  • 7.  Servlet class hierarchy Object GenericServlet ServletResponse HttpServletRequest ServerRequest HttpServlet HttpServletResponse javax.servlet javax.servlet.http
  • 8.  Software Requirements  To use Java servlets, following softwares will be needed  J2SE  Additional J2EE based libraries for servlets such as servlet-api.jar, jsp- api.jar. You can download these APIs separately but they are also available with the web server you’ll be using.  A capable servlet web engine (web server) 12/21/2015University of Education, Okara 8
  • 9.  Environment Setup Steps 1. Download the Apache Tomcat Server 2. Install Tomcat 3. Set the JAVA_HOME variable 4. Set the CATALINA_HOME variable 5. Set the CLASSPATH variable 6. Test the Server 12/21/2015University of Education, Okara 9
  • 10.  Servlet Advantages & Disadvantages The advantage of Servlets is,  Portability o Portable across operating systems and across web servers  Power o Harness the full power of the core Java APIs: networking and URL access, multithreading, image manipulation, data compression, JDBC, object serialization, internationalization  Efficiency & Endurance o Memory resident, so invocation highly efficient—no process to spawn or interpreter to invoke 12/21/2015University of Education, Okara 10
  • 11.  Servlet Advantages & Disadvantages  Safety o Support safe programming since inherit Java’s strong type safety, exception- handling mechanism  Elegance o Code is clean, object-oriented, modular, and simple (i.e.. Session tracking, cookie)  Integration o Tightly integrated with the server—translate file paths, perform logging, check authorization, and MIME type mapping 12/21/2015University of Education, Okara 11
  • 12.  Servlet Advantages & Disadvantages The disadvantage of Servlets is,  Web Administrator will need to learn how to install and maintain Java Servlets  Tedious uses of out.println() statements o Can be remedied by using Java Server Page (JSP) 12/21/2015University of Education, Okara 12
  • 13.  Types of HTTP Requests  Get  Post  Delete  Options  Put  Trace 12/21/2015University of Education, Okara 13
  • 14.  How HTTP Sends Request Client Server Servlets Some HTTP request types • Get -- Attr/Val pairs attached after ? of URL E.g. http://www.gmail.com/register?name=ali • Post -- Attr/Val pairs attatched with the request body
  • 15.  HTTP Request Example 12/21/2015University of Education, Okara 15 Request parameters etc.
  • 16.  Servlet Model 12/21/2015University of Education, Okara 16
  • 17.  Servlet Life Cycle 12/21/2015University of Education, Okara 17  Initialize  Service  Destroy
  • 18. HelloWorld Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(“Hello World!”); } } 12/21/2015University of Education, Okara 18