SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Servlet API and Life Cycle Events

 Introduction

Servlets are server side components.These components can be run on any platform or any server due to the core java technology which is used to implement them. Servlets augment the functionality of a web application.They are dynamically loaded by the server's Java runtime environment when needed. On receiving an incoming request from the client, the web server/container initiates the required servlet. The servlet processes the client request and sends the response back to the server/container, which is routed to the client


 Servlet API and Life Cycle Events

The following figure shows the high-level object model for representing the class hierarchy of javax.servlet package:



The following figure shows the high-level object model representing the class hierarchy of javax.servlet.http package:


 The HttpServletRequest Interface
Setting Request Headers
HTTP requests have number of associated headers. These headers provide extra information about the client, such as the name and the version of browser sending the request. Some of the important HTTP request headers are:
  • Accept: Specifies the MIME type that the client prefers to accept.
  • Accept-Language: Specifies the language in which the client prefers to receive the request.
  • User-Agent: Specifies the name and version of the browser sending the request.
 The HttpServletResponse Interface
Setting Response Headers
You can provide additional information about the response that is sent to the client by the application server by adding new HTTP headers. Some additional HTTP response headers that a servlet can set are:
  • Content-Type: Specifies the MIME type of the data sent by the servlet, such as text/HTML and text/plain.
  • Cache-control: Specifies information to cache a servlet. If the value of Cachecontrol is set to no-store then the servlet is not cached by a browser or a proxy server. The max-age value specifies the time in seconds for which a servlet needs to be in cache.
  • Expires: Specifies the time when the content of the servlet may change or when its information will become invalid and needs to be reloaded by the browser to show the latest data.
Servlets make use of various methods, such as setHeader() and setDateHeader() to set the value of HTTP headers in the response object. You can use the following code snippet to set the HTTP header information:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter();
long mseconds;
java.util.Date date = new java.util.Date();
mseconds = date.getTime() + 10000;
res.setHeader("Content-Type", "text/html");
res.setDateHeader("Expires", mseconds);
out.println("<HTML><BODY>");
out.println(new java.util.Date());
out.println("</BODY></HTML>");
}

 Click for Next Topic
<- PREVIOUSNEXT ->