0% found this document useful (0 votes)
64 views

Servlet

The document discusses servlets, including their advantages over CGI, the servlet lifecycle consisting of init(), service(), and destroy() methods, and the Java servlet API packages javax.servlet and javax.servlet.http. Servlets are Java programs that extend the functionality of web servers to enable dynamic content and processing of web requests. The servlet API provides interfaces and classes to handle requests and responses and access configuration information.

Uploaded by

Rahul Negi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Servlet

The document discusses servlets, including their advantages over CGI, the servlet lifecycle consisting of init(), service(), and destroy() methods, and the Java servlet API packages javax.servlet and javax.servlet.http. Servlets are Java programs that extend the functionality of web servers to enable dynamic content and processing of web requests. The servlet API provides interfaces and classes to handle requests and responses and access configuration information.

Uploaded by

Rahul Negi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading

Servlet Parameters The javax.servlet.http Package

Servlets

March 22, 2013

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Advantages of servlets

Servlets are small programs that execute on the server side of a Web connection. Applets extends functionality of web browser, servlets extends functionality of web server. Web browsers communicate to web server via common gateway interface. CGI allowed the separate process to read data from the HTTP request and write data to the HTTP response. Problems with CGI
Expensive in terms of processor and memory resources to create a separate process for each client request. Also expensive to open and close database connections for each client request. Not platform-independent.
Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Advantages of servlets

Advantages of Servlets Performance is signicantly better. Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request. Platform-independent because they are written in Java. Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. Full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

How functions are being called?

Three methods are central to the life cycle of a servlet


init() service() destroy()

They are implemented by every servlet and are invoked at specic times by the server.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

How functions are being called?

1 User enter Uniform Resource Locator (URL) to a Web browser. The browser then generates an HTTP request for this URL. This request is then sent to the appropriate server. 2 HTTP request is received by the Web server. The server maps this request to a particular servlet. The servlet is dynamically retrieved and loaded into the address space of the server. 3 The server invokes the init( ) method of the servlet. This method is invoked only when the servlet is rst loaded into memory. It is possible to pass initialization parameters to the servlet so it may congure itself. 4 The server invokes the service( ) method of the servlet. This method is called to process the HTTP request. It may also formulate an HTTP response for the client. 5 The server may decide to unload the servlet from its memory. The server calls the destroy( ) method to relinquish any Servlets resources such as le handles that are allocated for the servlet.

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Latest servlet specication is 2.3 Recommended by Sun is Tomcat 4.0

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Example

The basic steps are the following.


Create and compile the servlet source code. Start Tomcat. Start a Web browser and request the servlet.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Example

HelloServlet.java

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Example

ServletRequest object enables the servlet to read data that is provided via the client request. ServletResponse object enables the servlet to formulate a response for the client. setContentType establishes the MIME type of the HTTP response. MIME type in this program is text/html. This indicates that the browser should interpret the content as HTML source code. getWriter( ) method obtains a PrintWriter. Anything written to this stream is sent to the client as part of the HTTP response.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

Two packages contain the classes and interfaces that are required to build servlets.
javax.servlet javax.servlet.http

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

Servlet Interface
Must implement.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletCong Interface

It allows a servlet to obtain conguration data when it is loaded.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletContext Interface
It enables servlets to obtain information about their environment.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletRequest Interface
Enables a servlet to obtain information about a client request.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletRespose Interface
It enables a servlet to formulate a response for a client.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

SingleThreadModel Interface

Is used to indicate that only a single thread will execute the service( ) method of a servlet at a given time. If a servlet implements this interface, the server has two options.
it can create several instances of the servlet. When a client request arrives, it is sent to an available instance of the servlet. it can synchronize access to the servlet.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

GenericServlet Class

It enables a servlet to formulate a response for a client. Implements the Servlet and ServletCong interfaces. Method to append a string to the server log le is available.
void log(String s) void log(String s, Throwable e)

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletInputStream Class

Extends InputStream. Is implemented by the server and provides an input stream that a servlet developer can use to read the data from a client request. int readLine(byte[ ] buer, int oset, int size) throws IOException

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

ServletOutputStream Class

Extends OutputStream. It is implemented by the server and provides an output stream that a servlet developer can use to write data to a client response. Denes print() and println() methods.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

The javax.servlet Package Interfaces Class

Servlet Exception Classes

Denees two exceptions.


ServletException, which indicates that a servlet problem has occurred. UnavailableException, which extends ServletException. It indicates that a servlet is unavailable.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

ServletRequest class includes methods that allow you to read the names and values of parameters that are included in a client request.

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Interfaces

Servlets

Introduction The Life Cycle of a Servlet Using Tomcat For Servlet Development A Simple Servlet Servlet API Reading Servlet Parameters The javax.servlet.http Package

Classes

Servlets

You might also like