Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server side technology which handles client request and give appropriate response to original client. Web browser: It is a client side technology which knows how to connect with web server on internet and how to request a page.
CGI(Common Gateway Interface) clientserverCGI DB request response request response process response
servlet client servlet DB request response request response server
Servlet Basic Servlet is a java class which extends the functionality of web server by dynamically generating web pages. A servlet is a java programming language class that is used to extend the capabilities of server that host application accessed by means of a request. Servlet can respond to any type of request, they are commonly used to extend the applications hosted by web server. A runtime environment known as a servlet container manages servlet loading and unloading and works with the web server to direct requests to servlets and to send output back to web clients. javax.servlet javax.servlet.http Packages provide interface and classes for writing servlets.
Architecture of servlet The javax.servlet package provides interfaces and classes for writing servlets. The architecture of the package is described below: Servlet GenericServlet HttpServlet MyServlet
Javax.Servlet This package contains classes and interfaces not specific to any particular protocol. These classes define the relationship between a servlet and the runtime environment provided by the servlet container.
Javax.servlet.GenericServlet A base class for servlets that do not use HTTP protocol-specific features. GenericServlet implements the basic features of all servlets, including initialization, request handling and termination.
Javax.servlet.http.HttpServlet Abstract base class Operate in an HTTP environment HttpServlet provides specific method GET POST PUT DELETE HEAD OPTIONS TRACE request HttpServlet override doGet(), doPost().
MyServlet MyServlet class is our own java class in that class we can write our code of program.
LifeCycle of servlet The servlet engine does the following: Loads a servlet when it is first requested. Calls the servlet’s init() method. Handles any number of requests by calling the servlet’s service() method. When shutting down, calls the destroy() method of each active servlet.
init() Syntax: public void init(ServletConfig config)throws ServletException When reqest for a servlet is received by the servlet engine, it checks to see if the servlet is already loaded. If not, the servlet engine uses a class loader to get the particular servlet class required, and then invokes its constructor to get an instance of the servlet. After the servlet is loaded, but before it services any requests, the servlet engine calls an init method. This method is called only once just before the servlet place into service.
service() Syntax: Public void service(ServletRequest req,ServletResponse res) After the init() complete successfully the servlet is able to accept request. By default, only single instance of the servlet is created, and the servlet container dispatches each request.
doGet(),doPost() Syntax: Pubic doGet/doPost(HttpServletRequest req, HttpServeltResponse res) GET requests are handled by doGet(). POST requests are handled by doPost().
Destroy() Syntax: Public void destroy() The servlet specification allows a servlet container to unload a servlet at any time. This may be done to conserve system resources or in preparation for servlet container shutdown. Release any resources allocated during init().
First Example of GenericServlet import java.io.*; import javax.servlet.*; public class SimpleServlet extends GenericServlet { public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println(" Example of generic servlet "); out.close(); }
Servlet Life Cycle Example servlet Life cycle Example
Reading Request Headers(1) Reading headers is straightforward; just call the getHeader method of HttpServletRequest with the name of the header. This call returns a String if the specified header was supplied in the current request, null otherwise. getMethod() The getMethod method returns the main request method (normally, GET or POST, but methods like HEAD, PUT, and DELETE are possible).
Reading Request Headers(2) getRequestURI() The getRequestURI method returns the part of the URL that comes after the host and port but before the form data. For example, for a URL of getRequestURI would return "/servlet/search.BookSearch". getQueryString() The getQueryString method returns the form data. For example, with getQueryString would return "subject=jsp".
Reading Request Headers(3) getProtocol() The getProtocol method returns the third part of the request line, which is generally HTTP/1.0 or HTTP/1.1. getHeaders() In most cases, each header name appears only once in the request. Occasionally, however, a header can appear multiple times, with each occurrence listing a separate value. Accept-Language is one such example. You can use getHeaders to obtain an Enumeration of the values of all occurrences of the header.
getHeaders() Accept This header specifies the MIME types that the browser or other clients can handle. Accept-Language This header specifies the client's preferred languages in case the servlet can produce results in more than one language. The value of the header should be one of the standard language codes such as en, en-us, da, etc. User-Agent This header identifies the browser or other client making the request and can be used to return different content to different types of browsers. Most Internet Explorer versions list a "Mozilla" (Netscape) version.
getHeaders() Accept-Encoding This header designates the types of encodings that the client knows how to handle. If the server receives this header, it is free to encode the page by using one of the formats specified (usually to reduce transmission time), sending the Content-Encoding response header to indicate that it has done so. Host browsers and other clients are required to specify this header, which indicates the host and port as given in the original URL. Connection This header indicates whether the client can handle persistent HTTP connections. In HTTP 1.0, a value of Keep-Alive means that persistent connections should be used.