Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Java Servlets

servlet notes

Uploaded by

21ucs002
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java Servlets

servlet notes

Uploaded by

21ucs002
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Server-side Programming:

Java Servlets
What is a Servlet?

• Servlet is a technology i.e. used to create web application.


• Servlet is an API that provides many interfaces and
classes including documentations.
• Servlet is an interface that must be implemented for
creating any servlet.
• Servlet is a class that extends the capabilities of the
servers and responds to the incoming requests. It can
respond to any type of requests.
• Servlet is a web component that is deployed on the server
to create dynamic web page.
CGI(Commmon Gateway Interface)

• CGI technology enables the web server to call


an external program and pass HTTP request
information to the external program to process
the request.

• For each request, it starts a new process.


Disadvantages of CGI

There are many problems in CGI technology:

• If number of clients increases, it takes more time


for sending response.
• For each request, it starts a process and Web
server is limited to start processes.
• It uses platform dependent language e.g. C, C+
+, perl.
Advantage of Servlet

• The web container creates threads for handling


the multiple requests to the servlet.

• Threads have a lot of benefits over the


Processes such as they share a common
memory area, lightweight, cost of
communication between the threads are low.
Benefits
The basic benefits of servlet are as follows:
• Better performance: because it creates a thread for
each request not process.

• Portability: because it uses java language.

• Robust: Servlets are managed by JVM so we don't need


to worry about memory leak, garbage collection etc.

• Secure: because it uses java language


Benefits of servlet

•Powerful
•Efficiency
•Safety
•Integration
•Extensibility
•Inexpensive
•Secure
•Performance
Java Servlet
• Javax.servlet package can be extended for use
with any application layer protocol
– http is the most popularly used protocol
– Javax.servlet.http package is extension of the
javax.servlet package for http protocol
• The Servlet spec allows you to implement separate Java
methods implementing each HTTP method in your subclass
of HttpServlet.
– Override the doGet() and/or doPost() method to provide normal
servlet functionality.
– Override doPut() or doDelete() if you want to implement these
methods.
– There's no need to override doOptions() or doTrace().
– The superclass handles the HEAD method all on its own.
Anatomy of a Servlet
• init()
• destroy()
• service()
• doGet()
• doPost()
• Servlet API life cycle methods
– init(): called when servlet is instantiated; must
return before any other methods will be called

– service(): method called directly by server when


an HTTP request is received; default service()
method calls doGet() (or related methods covered
later)

– destroy(): called when server shuts down


Servlet
Container

Create Thread Pool Thread


Thread

Instantiate servlet
Servlet
Call init ( ) method Perform
HTTP Initialization
Allocate request to thread Call service ( ) method
Request 1

HTTP
Allocate request to thread Call service ( ) method Perform Service
Request 2
Shutdown
Initiated
Block all further requests Wait
HTTP for active threads to end Perform Service
Response 1
Terminate thread pool

HTTP call destroy ( ) method


Perform
Response 2 cleanup
terminate servlet
Servlet destroyed
& garbage collected
Container shutdown
Anatomy of a Servlet
• HTTPServletRequest object
• Information about an HTTP request
• Headers
• Query String
• Session
• Cookies
• HTTPServletResponse object
• Used for formatting an HTTP response
• Headers
• Status codes
• Cookies
Server-side Programming
• The combination of
– HTML
– JavaScript
– DOM
is sometimes referred to as Dynamic HTML (DHTML)
• Web pages that include scripting are often called
dynamic pages (vs. static)
Server-side Programming
• Similarly, web server response can be static or
dynamic
– Static: HTML document is retrieved from the file
system and returned to the client
– Dynamic: HTML document is generated by a program
in response to an HTTP request
• Java servlets are one technology for producing
dynamic server responses
– Servlet is a class instantiated by the server to produce
a dynamic response
Servlet Overview
Servlet Overview
Reading Data from a Client

1. When server starts it instantiates servlets


2. Server receives HTTP request, determines
need for dynamic response
3. Server selects the appropriate servlet to
generate the response, creates
request/response objects, and passes them to
a method on the servlet instance
4. Servlet adds information to response object via
method calls
5. Server generates HTTP response based on
information stored in response object
• The browser uses two methods to pass this information
to web server. These methods are GET Method and
POST Method.

• GET Method (doGet())

• The GET method sends the encoded user information


appended to the page request. The page and the
encoded information are separated by the ?(question
mark) symbol as follows −
http://www.test.com/hello?key1 = value1&key2 = value2
• POST Method
• A generally more reliable method of passing information to
a backend program is the POST method.
• This packages the information in exactly the same way as
GET method, but instead of sending it as a text string after
a ? (question mark) in the URL it sends it as a separate
message.
• This message comes to the backend program in the form
of the standard input which you can parse and use for your
processing.
• Servlet handles this type of requests
using doPost() method.
• Servlets handles form data parsing automatically using the
following methods depending on the situation −

• getParameter() − You call request.getParameter() method to


get the value of a form parameter.

• getParameterValues() − Call this method if the parameter


appears more than once and returns multiple values, for
example checkbox.

• getParameterNames() − Call this method if you want a


complete list of all parameters in the current request.
Servlets vs. Java Applications
• Servlets do not have a main()
– The main() is in the server
– Entry point to servlet code is via call to a
method (doGet() in the example)
• Servlet interaction with end user is indirect
via request/response object APIs
– Actual HTTP request/response processing is
handled by the server
• Primary servlet output is typically HTML
Servlet Life Cycle
• Servlet API life cycle methods
– init(): called when servlet is instantiated;
must return before any other methods will be
called
– service(): method called directly by server
when an HTTP request is received; default
service() method calls doGet() (or
related methods covered later)
– destroy(): called when server shuts down
Writing HTTP Response Header

• when a Web server responds to an HTTP


request, the response typically consists of a
status line, some response headers, a blank
line, and the document. A typical response looks
like this −
More Servlet Methods
• In addition to doGet() and doPost(),
servlets have methods corresponding to
other HTTP request methods
– doHead(): automatically defined if doGet()
is overridden
– doOptions(), doTrace(): useful default
methods provided
– doDelete(), doPut(): override to support
these methods

You might also like