Follow us on Facebook

Header Ads

Welcome to JAVA POint

SERVLET IN JAVA

                                  *********SERVLET IN JAVA*************

-          Servlet technology is used to create a web application (resides at server side and                         generates a dynamic web page).

-          What is a Servlet?

-          Servlet is a technology which is used to create a web application.

-          Servlet is an API that provides many interfaces and classes including                                 documentation.

-          Servlet is an interface that must be implemented for creating any Servlet.

-          Servlet is a class that extends the capabilities of the servers and responds to the               incoming requests. It can respond to any requests.

        Servlet is a web component that is deployed on the server to create a dynamic web page.

Methods of GenericServlet Class:                                                          

  • public void init(ServletConfig)
  • public abstract void service(ServletRequest request,ServletResponse response)
  • public void destroy()
  • public ServletConfig getServletConfig()
  • public String getServletInfo()
  • Public String getInitParameterNames()

HTTPServlet  Class:

-          It is also an abstract class.

-          This class gives implementation of various service() methods of Servlet interface.

-          To create a servlet,we should create a class that extends HTTPServlet class.

-          The servlet class must not override service().

-          It will override only doGet() and doPost().

The service() method listens to Http methods (GET,POST) from request stream and invokes doGet() and doPost() methods based on Http method type




It extend 

2.Handling HTTPResponse:

-          Servlet API provides two important interfaces ServletResponse and HttpServletREsponse to assist in sending response to client.

-          The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client.javax.servlet.HttpServlet

-          Have in-built HTTP protocol support and are

more  useful in a Sun Java System web server.

-          For both servlet types,you implement the

constructor init() and the destructor destroy() to initialize or deallocate resources.

-          All  servlet must implement a service() method,which is responsible for handling servlet requests.

-          HTTP servlet provide a service method method  that automatically routes the request to another method .

-          Http  servlet override doGet() to process GET requests and doPost() to process POST request

-          HTTP servlet is HTTP protocol specific .


Life Cycle of a Servlet (Servlet Life Cycle):

       Servlet class is loaded

       Servlet instance is created

       init() method is invoked

       service() method is invoked

       destroy() method is invoked

       The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet:



Methods of servlets

 

1)      init() Method

public  void init(ServletConfig config) throws ServletException

-called only once.

-          It is called only when the servlet is created.

-          it is used for one-time initializations.

2) service() Method

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { }

-is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

The doGet() and doPost() are most frequently used methods with in each service request.


a)doGet() Method

       A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code }

b)doPost() Method

       A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code}

3) destroy() Method

-          The destroy() method is called only once at the end of the life cycle of a servlet.

-          This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

       After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this −

public void destroy() { // Finalization code... 


Simple servlet program:

HelloServlet.java

import java.io.*;

import  javax.servlet.*;

public class HelloServlet extends GenericServlet

{

                public void service(ServletRequest request,ServletErsponse response) throws ServletException,IOException

      {

                                response.setContentType(“text/html”);

                                PrintWriter pw= response.getWriter();

                                pw.println(“<B> Hello!”);

                                pw.close();

                }

}


Handling HTTPRequest and HTTPResponse

  1. Handling HTTPRequest:

-          When a browser requests for a web page, it sends lot of information  to the web server which cannot be read directly because this information travel as a part of header of HTTP request.

Methods

Descritption

String  getContextPath()

This  method returns the portion of the request URL that indicates the context of the request.

Cookies getCookies()

This method return an array containing all of the Cookie objects the client sent with this request.

String getQueryString()

Returns the query string that is contained in the request URL after the path.

HttpSession getSession()

Returns the current HttpSession associated with this request

String getMethod()

Returns the name of the HTTP method with which this request was made ,e.g. GET,POST,

Part getPart(String name)

Gets the Part with the given name.

String getPathInfo()

Returns any extra path information associated with URL the client sent when it made this request.

String getServletPath()

Returns the part of this requests URL that calls the servlet.

 2.Handling HTTPResponse:

-          Servlet API provides two important interfaces ServletResponse and HttpServletREsponse to assist in sending response to client.

-          The HttpServletResponse interface enables a servlet to formulate an HTTP response to a client.


Methods

Description

void addCookie(Cookie cookie)

Method adds the specified cookie to the response

void sendRedirect(String location)

Sends a temporary redirest response to the client using the specified redirect location URL and clears the buffer

int getStatus()

Gets the current status code of this response.

String getHeader(String name)

Gets the value of the response header with the given name.

void setHeader(String name,String value)

sets a response header with the given name and value.

void setStatus(int sc)

Sets the status code for this response.

void sendError(int sc,String msg)

Sends an error response to the client using the specified status and clears the buffer.

 



0 Comments:

Post a Comment

C language and C++ progaming