SlideShare une entreprise Scribd logo
1  sur  111
Servlet

Rajeev Gupta
 MTech CS
What does your web server do?
Clients and ser vers know HTML and
                 HTTP
• HTML tells the browser how to display the
  content to the user.

• HTTP is the protocol clients and servers use
  on the web to communicate.

• The server uses HTTP to send HTML to the
  client.
Anatomy of an HTTP GET request
Anatomy of an HTTP POST request
Anatomy of an HTTP response, and what the heck is a “ MIME type”?
Anatomy of URL
Why Server is not enough
• Dynamic Content
• Saving data on the server
What a container?
•   Communication support
•   Lifecycle management
•   Multithreading support
•   Declarative security
•   JSP Support
How a container handle a dynamic request?
Servlet Life Cycle
Servlet inherits the lifecycle methods
Each request runs in a separate
           thread!
Besides parameters, what else can I get
       from a Request object?
dispatching vs. redirecting
ServletContext vs. ServletConfig
Setting ServletConfig
and getting it in Servlet…
Setting and getting ServletContext
Where it fit in JEE context…
Exercise
•   Hello World Servlet
•   Login Application –I
•   Login Application-II
•   Use ServletContext and ServletConfig
MVC
Model
View
Controller

Where it fit all??
Beer Advice Application
MVC Arch
Application flow MVC
• 1- The client makes a request for the form.html page.

• 2 - The Container retrieves the form.html page.

• 3 - The Container returns the page to the browser, where the user
  answers the questions on the form and...

• 4- Browser request data to the container

• 5- container find the correct Servlet based on the URL, an passes the
  request to the servlet

•   6 – The Servlet call BeerServlet for the help
Application flow MVC
• 7- BeerExpert class return an answer, which Servlet add
  to the request object

• 8- The Servlet forward request to the jsp

• 9-Jsp get the answer from the request object

• 10- JSP generate a page for the container

• 11- Container return page to the happy user
The HTML for the initial form page
       <html><body>
       <h1 align=”center”>Beer Selection Page</h1>
       <form method=”POST”
       action=”SelectBeer.do”>
       Select beer characteristics<p>
       Color:
       <select name=”color” size=”1”>
       <option value=”light”> light </option>
       <option value=”amber”> amber </option>
       <option value=”brown”> brown </option>
       <option value=”dark”> dark </option>
       </select>
       <br><br>
       <center>
       <input type=”SUBMIT”>
       </center>
       </form>
       </body></html>
Web.xml
The controller Servlet
Model…..
And finally the view…
Need for listener….
• Init parameter (key, value) are really are
  strings
• What if we need objects ….
She wants a ServletContextListener
We will convert dog string to a real
             dog!!!!
Making and using a context
              listener
• We need three classes and one DD
• The ServletContextListener
  – MyServletContextListener.java
• The attribute class
  – Dog.java
• The Servlet
  – ListenerTester.java
Writing the listener class
Attributes: What is it???
multithreading issues????
What abut multithreading issues????

 Note easy to handle
 But really not rocket science!!!!
How do we make context
         attributes thread-safe?
• Idea 1:
   – Synchronizing the service method is a spectacularly BAD idea



• Idea 2:
   – SingleThreadModel


  – Idea 3:
     » Synchronized Block


     » What to Use????
Synchronizing the service method is a
       spectacularly BAD idea
Solution: You don’t need a lock on the servlet... you
           need the lock on the context!
Are Session attributes thread-safe?
           Not really!!!
request Attribute : thread safe
            but…..
Session Management
Simple Cookies Example: for
   session management
Filters are modular, and configurable in the DD
Three ways filters are like servlets
• The Container knows their API
• Just like servlets, filters have a lifecycle.
            » Like servlets, they have init() and destroy() methods.
              Similar to a servlet’s doGet()/doPost() method, filters
              have a doFilter() method.

• They’re declared in the DD
            » A web app can have lots of filters, and a given request
              can cause more than one filter to execute. The DD is the
              place where you declare which filters will run in
              response to which requests, and in which order.
Building the request tracking filter
Thanks:-

Reference:-
Head First Servlet and Jsp

Contenu connexe

Tendances

Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycleDhruvin Nakrani
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jspAnkit Minocha
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtapVikas Jagtap
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technologyMinal Maniar
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 
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
 
Java - Servlet - Mazenet Solution
Java - Servlet - Mazenet SolutionJava - Servlet - Mazenet Solution
Java - Servlet - Mazenet SolutionMazenetsolution
 

Tendances (20)

JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Servlets
ServletsServlets
Servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servlet
Servlet Servlet
Servlet
 
Servlet ppt by vikas jagtap
Servlet ppt by vikas jagtapServlet ppt by vikas jagtap
Servlet ppt by vikas jagtap
 
Servlets
ServletsServlets
Servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
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
 
Java - Servlet - Mazenet Solution
Java - Servlet - Mazenet SolutionJava - Servlet - Mazenet Solution
Java - Servlet - Mazenet Solution
 

En vedette

En vedette (13)

Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Servlet lifecycle
Servlet lifecycleServlet lifecycle
Servlet lifecycle
 
Servlets - filter, listeners, wrapper, internationalization
Servlets -  filter, listeners, wrapper, internationalizationServlets -  filter, listeners, wrapper, internationalization
Servlets - filter, listeners, wrapper, internationalization
 
Asif
AsifAsif
Asif
 
21servers And Applets
21servers And Applets21servers And Applets
21servers And Applets
 
Applet intro
Applet introApplet intro
Applet intro
 
Servlet Filter
Servlet FilterServlet Filter
Servlet Filter
 
interface in c#
interface in c#interface in c#
interface in c#
 
Applet Vs Servlet
Applet Vs ServletApplet Vs Servlet
Applet Vs Servlet
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Java Applet
Java AppletJava Applet
Java Applet
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Similaire à Servlet Basics and Lifecycle

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
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.pptkstalin2
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: ServletsFahad Golra
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptxssuser92282c
 
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
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...WebStackAcademy
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 
Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3sandeep54552
 
Exploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices💡 Tomasz Kogut
 

Similaire à Servlet Basics and Lifecycle (20)

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
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Servlet1.ppt
Servlet1.pptServlet1.ppt
Servlet1.ppt
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
IP UNIT III PPT.pptx
 IP UNIT III PPT.pptx IP UNIT III PPT.pptx
IP UNIT III PPT.pptx
 
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
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 9...
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Servlet life cycle
Servlet life cycleServlet life cycle
Servlet life cycle
 
servlet_lifecycle.pdf
servlet_lifecycle.pdfservlet_lifecycle.pdf
servlet_lifecycle.pdf
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
J2ee servlet
J2ee servletJ2ee servlet
J2ee servlet
 
Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3Enterprise java unit-1_chapter-3
Enterprise java unit-1_chapter-3
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
Exploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices
 

Plus de Rajiv Gupta

Spring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepSpring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepRajiv Gupta
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with javaRajiv Gupta
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentalsRajiv Gupta
 
Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2Rajiv Gupta
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastRajiv Gupta
 
Lab work servlets and jsp
Lab work servlets and jspLab work servlets and jsp
Lab work servlets and jspRajiv Gupta
 
Spring aop with aspect j
Spring aop with aspect jSpring aop with aspect j
Spring aop with aspect jRajiv Gupta
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injectionRajiv Gupta
 
Java spring framework
Java spring frameworkJava spring framework
Java spring frameworkRajiv Gupta
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jRajiv Gupta
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notesRajiv Gupta
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuffRajiv Gupta
 

Plus de Rajiv Gupta (18)

Spring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by stepSpring5 hibernate5 security5 lab step by step
Spring5 hibernate5 security5 lab step by step
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with java
 
1. java script language fundamentals
1. java script language fundamentals1. java script language fundamentals
1. java script language fundamentals
 
Introduction to jsf2
Introduction to jsf2Introduction to jsf2
Introduction to jsf2
 
Hibernate 3
Hibernate 3Hibernate 3
Hibernate 3
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencast
 
Struts2
Struts2Struts2
Struts2
 
jsf2 Notes
jsf2 Notesjsf2 Notes
jsf2 Notes
 
Java 7
Java 7Java 7
Java 7
 
Struts2 notes
Struts2 notesStruts2 notes
Struts2 notes
 
Lab work servlets and jsp
Lab work servlets and jspLab work servlets and jsp
Lab work servlets and jsp
 
Spring aop with aspect j
Spring aop with aspect jSpring aop with aspect j
Spring aop with aspect j
 
Spring 3.0 dependancy injection
Spring 3.0 dependancy injectionSpring 3.0 dependancy injection
Spring 3.0 dependancy injection
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
 
Jsp Notes
Jsp NotesJsp Notes
Jsp Notes
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
 
Advance C++notes
Advance C++notesAdvance C++notes
Advance C++notes
 
Core java 5 days workshop stuff
Core java 5 days workshop stuffCore java 5 days workshop stuff
Core java 5 days workshop stuff
 

Servlet Basics and Lifecycle