Servlet Programming
Servlet Programming
host applications accessed via a request-response programming model. A servlet can be hosted using a Webserver and also via Application Server Whats a Application Server Application Server to be straight is a Server with two containers 1. Web Container [To Support Servlets, JSP, Struts etc] 2. Enterprise Container [To Support EJBs]
WEB CONTAINER
Application Server Diagram A webserver for e.g. Apache Tomcat only contains a Web container by default. A Web container is a runtime environment or framework which during execution time creates an object which handles the requests as well as delivers the response. EJB container is nothing but the program that runs on the server and implements the EJB specifications. EJB container provides special type of the environment suitable for running the enterprise components.
SERVLET javax.servlet.http HttpServlet *Abstract Servlets ----Implemented By- Generic Servlets-----Extended By--HttpServlet ** Generic Servlets is protocol independent
Basic Servlets API Various classes and interfaces required to execute a servlet program are found by extending the following javax.servlet.*; javax.servlet.http.*; Various HTTP Servlets Specific Interfaces 1. javax .servlet.http.HttpServletResponse This provides an object representation of the servers response to a clients request. Various methods under this interface (a) Add Cookie Syntax: public abstract void addCookie(Cookie cookie) This method adds the specific cookies to the response. Multiple calls can be made to addCookie() to add additional cookies. Note: There is a browser limitation i.e. 20 cookies per site and 300 per user. (b) ContainsHearder Syntax: public abstract boolean containsHeader(String name) This method determines whether a specified header is contained in the response. If exists returns true or else false. (c) SendError Syntax: public abstract void SendError(int sc) throws IOException This method is similar to setStatus() method but is used when status indicates an error during handling the request. This method should be called before sending any content. (d) GetOutputStream Syntax: public abstract ServletOutputStream getOutputStream () throws IOException Returns an output stream over which a binary data can be transmitted back to client. (e) GetWriter Syntax: public abstract PrintWriter GetWriter() throws IOException This method returns a PrintWriter for writing character based response back to client.
(f) SetContentLength Syntax: public abstract void SetContentLength(int len) This method sets the value of the content being returned by the server. [Optional] (g) SetContentType Syntax: public abstract void SetContentType(String type) This method sets the Content-Type HTTP headers in the response. ** Content Type once set can not be changed. If ContentType() is called second time with a different MIME type then we will get error like java.lang.IllegalStateException
2. javax.servlet.http.HttpServletRequest The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).
Example