SlideShare a Scribd company logo
Servlet

Rajeev Gupta
 MTech CS
Java Servlet
Java Servlet
Java Servlet
Java Servlet
What does your web server do?
Java Servlet
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
Java Servlet
Anatomy of an HTTP POST request
Java Servlet
Anatomy of an HTTP response, and what the heck is a “ MIME type”?
Java Servlet
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?
Java Servlet
Servlet Life Cycle
Java Servlet
Servlet inherits the lifecycle methods
Java Servlet
Each request runs in a separate
           thread!
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Besides parameters, what else can I get
       from a Request object?
Java Servlet
dispatching vs. redirecting
Java Servlet
Java Servlet
Java Servlet
ServletContext vs. ServletConfig
Java Servlet
Java Servlet
Java Servlet
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??
Java Servlet
Beer Advice Application
MVC Arch
Java Servlet
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…
Java Servlet
Need for listener….
• Init parameter (key, value) are really are
  strings
• What if we need objects ….
Java Servlet
She wants a ServletContextListener
Java Servlet
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
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Attributes: What is it???
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
multithreading issues????
What abut multithreading issues????

 Note easy to handle
 But really not rocket science!!!!
Java Servlet
Java Servlet
Java Servlet
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!!!
Java Servlet
request Attribute : thread safe
            but…..
Session Management
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Java Servlet
Simple Cookies Example: for
   session management
Java Servlet
Java Servlet
Java Servlet
Java Servlet
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
Java Servlet
Thanks:-

Reference:-
Head First Servlet and Jsp

More Related Content

What's hot (20)

PPT
JAVA Servlets
deepak kumar
 
DOC
Java Servlets & JSP
Manjunatha RK
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PPTX
Servlets
Akshay Ballarpure
 
PPT
Java Servlets
Nitin Pai
 
PPTX
Chapter 3 servlet & jsp
Jafar Nesargi
 
PPT
Java Servlets
BG Java EE Course
 
PPT
Java servlets
lopjuan
 
PPT
1 java servlets and jsp
Ankit Minocha
 
PPTX
java Servlet technology
Tanmoy Barman
 
DOCX
Servlet
Dhara Joshi
 
PPT
Servlet ppt by vikas jagtap
Vikas Jagtap
 
PPT
Servlets
Sasidhar Kothuru
 
PPTX
Java Servlets
KushagraChadha1
 
PDF
Java servlet technology
Minal Maniar
 
PPTX
Core web application development
Bahaa Farouk
 
PPT
Web Tech Java Servlet Update1
vikram singh
 
PDF
java servlet and servlet programming
Kumar
 
PPT
Servlet/JSP course chapter 1: Introduction to servlets
JavaEE Trainers
 
PPT
Java - Servlet - Mazenet Solution
Mazenetsolution
 
JAVA Servlets
deepak kumar
 
Java Servlets & JSP
Manjunatha RK
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
Java Servlets
Nitin Pai
 
Chapter 3 servlet & jsp
Jafar Nesargi
 
Java Servlets
BG Java EE Course
 
Java servlets
lopjuan
 
1 java servlets and jsp
Ankit Minocha
 
java Servlet technology
Tanmoy Barman
 
Servlet
Dhara Joshi
 
Servlet ppt by vikas jagtap
Vikas Jagtap
 
Java Servlets
KushagraChadha1
 
Java servlet technology
Minal Maniar
 
Core web application development
Bahaa Farouk
 
Web Tech Java Servlet Update1
vikram singh
 
java servlet and servlet programming
Kumar
 
Servlet/JSP course chapter 1: Introduction to servlets
JavaEE Trainers
 
Java - Servlet - Mazenet Solution
Mazenetsolution
 

Viewers also liked (13)

PPT
Java servlet life cycle - methods ppt
kamal kotecha
 
RTF
Servlet lifecycle
Durga Devi Thulluri
 
PPTX
Servlets - filter, listeners, wrapper, internationalization
susant sahu
 
PPTX
Asif
Shaik Asif
 
PDF
21servers And Applets
Adil Jafri
 
PPTX
Applet intro
Nitin Birari
 
PDF
Servlet Filter
AshishSingh Bhatia
 
PPTX
interface in c#
Deepti Pillai
 
PPSX
Applet Vs Servlet
Bharat Sahu
 
PPT
J2EE - JSP-Servlet- Container - Components
Kaml Sah
 
PPT
Applet life cycle
myrajendra
 
PPTX
Slideshare ppt
Mandy Suzanne
 
Java servlet life cycle - methods ppt
kamal kotecha
 
Servlet lifecycle
Durga Devi Thulluri
 
Servlets - filter, listeners, wrapper, internationalization
susant sahu
 
21servers And Applets
Adil Jafri
 
Applet intro
Nitin Birari
 
Servlet Filter
AshishSingh Bhatia
 
interface in c#
Deepti Pillai
 
Applet Vs Servlet
Bharat Sahu
 
J2EE - JSP-Servlet- Container - Components
Kaml Sah
 
Applet life cycle
myrajendra
 
Slideshare ppt
Mandy Suzanne
 
Ad

Similar to Java Servlet (20)

ODP
servlet 2.5 & JSP 2.0
megrhi haikel
 
PPT
Basics Of Servlet
Shubhani Jain
 
ODP
Servlets
ramesh kumar
 
PPT
Servlets
Manav Prasad
 
PPTX
UNIT-3 Servlet
ssbd6985
 
PDF
Servlets
Abdalla Mahmoud
 
PDF
Servlets intro
vantinhkhuc
 
PPTX
IP UNIT III PPT.pptx
ssuser92282c
 
DOCX
Html servlet example
rvpprash
 
PDF
Dynamic content generation
Eleonora Ciceri
 
PPTX
SCWCD : The servlet container : CHAP : 4
Ben Abdallah Helmi
 
PPTX
BITM3730Week12.pptx
MattMarino13
 
PDF
Bt0083 server side programing
Techglyphs
 
PDF
Servlets
Ravi Kant Sahu
 
PPTX
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
PPTX
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
PPT
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
PPTX
CS8651 IP Unit 3.pptx
Vigneshkumar Ponnusamy
 
PDF
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
PPTX
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
servlet 2.5 & JSP 2.0
megrhi haikel
 
Basics Of Servlet
Shubhani Jain
 
Servlets
ramesh kumar
 
Servlets
Manav Prasad
 
UNIT-3 Servlet
ssbd6985
 
Servlets
Abdalla Mahmoud
 
Servlets intro
vantinhkhuc
 
IP UNIT III PPT.pptx
ssuser92282c
 
Html servlet example
rvpprash
 
Dynamic content generation
Eleonora Ciceri
 
SCWCD : The servlet container : CHAP : 4
Ben Abdallah Helmi
 
BITM3730Week12.pptx
MattMarino13
 
Bt0083 server side programing
Techglyphs
 
Servlets
Ravi Kant Sahu
 
Servlets-UNIT3and introduction to servlet
RadhikaP41
 
21CS642 Module 4_1 Servlets PPT.pptx VI SEM CSE Students
VENKATESHBHAT25
 
Module 4.pptModule 4.pptModule 4.pptModule 4.ppt
tahirnaquash2
 
CS8651 IP Unit 3.pptx
Vigneshkumar Ponnusamy
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
pkaviya
 
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
Ad

More from Rajiv Gupta (18)

PDF
Spring5 hibernate5 security5 lab step by step
Rajiv Gupta
 
PDF
GOF Design pattern with java
Rajiv Gupta
 
PPTX
1. java script language fundamentals
Rajiv Gupta
 
PDF
Introduction to jsf2
Rajiv Gupta
 
PDF
Hibernate 3
Rajiv Gupta
 
PDF
Weblogic 11g admin basic with screencast
Rajiv Gupta
 
PDF
Struts2
Rajiv Gupta
 
PDF
jsf2 Notes
Rajiv Gupta
 
PDF
Java 7
Rajiv Gupta
 
PDF
Struts2 notes
Rajiv Gupta
 
PDF
Lab work servlets and jsp
Rajiv Gupta
 
PDF
Spring aop with aspect j
Rajiv Gupta
 
PDF
Spring 3.0 dependancy injection
Rajiv Gupta
 
PDF
Java spring framework
Rajiv Gupta
 
TXT
Jsp Notes
Rajiv Gupta
 
PDF
Java Logging discussion Log4j,Slf4j
Rajiv Gupta
 
TXT
Advance C++notes
Rajiv Gupta
 
PDF
Core java 5 days workshop stuff
Rajiv Gupta
 
Spring5 hibernate5 security5 lab step by step
Rajiv Gupta
 
GOF Design pattern with java
Rajiv Gupta
 
1. java script language fundamentals
Rajiv Gupta
 
Introduction to jsf2
Rajiv Gupta
 
Hibernate 3
Rajiv Gupta
 
Weblogic 11g admin basic with screencast
Rajiv Gupta
 
Struts2
Rajiv Gupta
 
jsf2 Notes
Rajiv Gupta
 
Java 7
Rajiv Gupta
 
Struts2 notes
Rajiv Gupta
 
Lab work servlets and jsp
Rajiv Gupta
 
Spring aop with aspect j
Rajiv Gupta
 
Spring 3.0 dependancy injection
Rajiv Gupta
 
Java spring framework
Rajiv Gupta
 
Jsp Notes
Rajiv Gupta
 
Java Logging discussion Log4j,Slf4j
Rajiv Gupta
 
Advance C++notes
Rajiv Gupta
 
Core java 5 days workshop stuff
Rajiv Gupta
 

Java Servlet