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

Servlet Programming

A servlet is a Java class that extends capabilities of servers hosting web applications accessed through request-response programs. A servlet can be hosted on a web server or application server. An application server contains both a web container for supporting servlets/JSP and an enterprise container for supporting EJBs. The web container handles requests and responses during execution, while the EJB container implements EJB specifications and provides an environment for enterprise components. Basic servlets programming involves extending javax.servlet and javax.servlet.http interfaces and classes to handle HTTP requests and responses.

Uploaded by

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

Servlet Programming

A servlet is a Java class that extends capabilities of servers hosting web applications accessed through request-response programs. A servlet can be hosted on a web server or application server. An application server contains both a web container for supporting servlets/JSP and an enterprise container for supporting EJBs. The web container handles requests and responses during execution, while the EJB container implements EJB specifications and provides an environment for enterprise components. Basic servlets programming involves extending javax.servlet and javax.servlet.http interfaces and classes to handle HTTP requests and responses.

Uploaded by

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

Servlets Programming (For Beginners) A servlet is a Java programming language class used to extend the capabilities of servers that

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

DATABASE HTML ENTERPRISE 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

You might also like