Servlet
Servlet
Servlets
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
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
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
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
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
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
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
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
ServletCong Interface
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
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
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
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
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
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
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
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
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
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