sendRedirect

  • sendRedirect() method sends the request with the new URL, which connects to this URL.

  • Every time it creates a new request or response object.

  • response.sendRedirect(www.jspts.com);
    

    Inter-Servlet Communication

    • Servlets that are present in the same web-server can communicate with each other and can also share resources, such as variables amongst each other.
    • Communication between servlets can be implemented by using the RequestDisptacher interface.

    RequestDispatcher

    RequestDispatcher object can forward a client's request to a resource or include the resource itself in the response back to the client. A resource can be another servlet, or an HTML file, or a JSP file, etc.
    There are two ways to delegate a request:-
    1. Forward
    2. Include
    Forward Include
    Used to forward a request to one Servlet to another Servlet.This method must be used when output is generated by second Servlet If PrintWriter object is accessed by the first servlet already, then exception is thrown by this method This method is used to invoke one servlet from the another servlet like the forward method.
    However,you can also include the output of first servlet with current servlet.
    The first servlet can make use of the PrintWriter Object even after calling the include method.
    ServletContext sc= this.getServletConfig.getServletContext();
    RequestDispatcher rd = sc.getRequestDispatcher ("/SecondServlet");
    rd.forward(response,request);
    ServletContext sc= getServletContext();
    RequestDispatcher rd = sc.getRequestDispatcher ("/SecondServlet");
    rd.include(response,request);

    ServletConfig vs ServletContext

    ServletConfig ServletContext
    One Servlet Config Object per Servlet One Servlet Context per Web-Application.
    Use it to pass deploy the Information to Servlet e.g-( A DataBase name or EJB look up name). Use it to access Web-application parameter
    (it has setAttribute/getAttribute method).ServletContext also configure in Deployment Descriptor.
    Use it access the servletContext Use it as a kind of application bulletin-board,where you can put up messages or use it to get the server information including the name version of container
    ServletConfig sc=this.getServletConfig(); ServletContext sc=this.getServletConfig.
    getServletContext()
    ;
    String a=sc.getInitParameter ("email"); String a=sc.getInitParameter ("Companyemail");
    Entry in web.xml:-
    <servlet>

    <servlet-name>LoginServlet</servlet-name>

    <sevlet-class>LoginServlet</sevlet-class>

    <init-param>

    <param-name>email</param-name>

    <param-value>abc@yahoo.com</param-value>

    </init-param>
    </servlet>
    Entry in web.xml:-
    <web-apps>
    <context-param>

    <param-name>companyemail</param-name>

    <param-value>efg@rkgit.com</param-value>

    </context-param>
    </web-apps>
    No setAttribute and getAttribute in ServletConfig. ServletContext has setAttribute(String name,Object o)to set value during coding
    and getAttribute to reterive the set value.