[Read more…] about Difference Between Rd.Forward () Res. sendRedirect ()
How to use sendRedirect method
This method sends a temporary redirect response to the client using the mentioned redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading ‘/’ the container interprets it as relative to the current request URL. If the location is relative with a leading ‘/’ the container interprets it as relative to the servlet container root. [Read more…] about How to use sendRedirect method
How to use RequestDispatcher Forward method
This method forwards a request from a servlet to another resource (servlet, JSP file or HTML file) on the server. It enables one servlet to do prelude processing of a request and another resource to create the response. [Read more…] about How to use RequestDispatcher Forward method
How to use RequestDispatcher include method
This method of RequestDispatcher interface includes the content of web resource (servlets, JSP and HTML file) in the response. In other words, this method allows server-side to include the response of destination program to source program. Here ServletResponse object are passed as the argument of include () method. [Read more…] about How to use RequestDispatcher include method
Servlet Chaining Between Two Servlet Programs
In the diagram Below, Srvl program forwards the request to Srv2 only when the generated square value is less than 100,otherwise, the Srvl directly sends response to browser window displaying that square value.
Deploy both these web applications in web logic server (Adv.java Batch Domain) copy WeqAppl, WebApp2 web applications to <oracleweblogic_home>\user-if possible \domains \Adv.javaBatchDomain \autodeploy folder.
In the Srvl servlet program of the above WebAppl web application, we must create RequestDispatcher object based on ServletContext object.
Source Code (WebApp1)
input.html url patttren of Srvl servlet program
<form action=”slurl” method=”get”>
A value : <input type = “text” name= “t1”><br>
<input type= “submit” value= “getResult”>
</form>
Srv1.java
import javax.servlet.*;
import javax.http.*;
import javax.io.*;
public class Srv1 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
response.setContentType(“text/html”);
int no=Integer.parselnt(req.getParameter(“t1”));
int res1=no*no;
if (res>=100)
{
pw.println(“Srv1 : Square val is :”+res);
}
else
{
ServletContext sc1 = getServletContext();
ServletContext sc2 = sc1.getContext(“WebApp2”);
RequestDispatcherrd = sc2.getRequestDispatcher(“/s2url”);
rd.forward(req,res);
}
}
}
web.xml
Configure Srvl program with /srvlurl as url pattern’
Source Code(WebApp2)
Srv2. java
import javax.servlet.*;
import javax.http.*;
import javax.io.*;
public class Srvl extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
response.setContentType(“text/html”);
int no=Integer.parselnt(req.getParameter(“t1”));
int res2=no*no*no;
pw.println{“Srv2: cube val is :”+res2) ;
}
}
Difference between the REQUESTDISPATCHER Object and SERVLETCONTEXT Object
The request object based RequestDispatcher object expects that the source servlet Program, destination web resource program and the destination web resource program be in the same web application.
The ServletContext object based RequestDispatcher object allows to keep the source servlet program and destination web resource program either in the same web application or in two different web application of the same server, but they cannot be two different web applications of two different servers.
Here, we can use request object or ServletContext object based.
Here Srv1 should use ServletContext object based RequestDispatcher object.
This kind of ServletChaining is not possible with RequestDispatcher object use send redirection concept. Servlet chaining is all about performing servlet-servlet communication.
Difference between GETREQUESTDISPATCHER () AND GETNAMEDDISPATCHER ()
GETREQUESTDISPATCHER ()
lnvokable on both request, servletContext object. Expects URL pattern of destination JSP or HTML programs as argument value. Generated RequestDispatcher object can point the destination servlet JSP program and HTML program.
GETNAMEDDISPATCHER ()
lnvokable only on ServletContext object. Expects logical name of the destination servlet/JSP program as argument value. Generated RequestDispatcher object can point only to the destination servlet, JSP programs.
RequestDispatcher in Servlet
While building a complex web application there might be a need to distribute the request to multiple servlets. This is where request dispatching comes into use. Due to this requirement Servlet container supports request dispatching within the same context.
The javax.servlet.RequestDispatcher is an interface that enables the Servlet Container to dispatch the request from a web application to another within the same context.
In order to dispatch the request we need to perform these tasks:
• Get a RequestDispatcher object
• Use the forward() method or include method of RequestDispatcher
There are three approaches to create the RequestDispatcher object:
i. By using ServletRequest object
ii. By using getRequestDispatcher() method of ServletContext
iii. By using getNamedDispatcher() method of ServletContext
Approach 1: (by using reqobj)
Example 1
In srv1 source code,
RequestDispatcherrd=req.getRequestDispatcher(“s2url“);
rd.forward(req,res);
(or)
Rd.include(req,res);
Example 2
In source srv2 program
RequestDispatcherrd=req.getRequestDispatcher(“/abc.html“);
(or)
RequestDispatcherrd=req.getRequestDispatcher(“/abc.jsp“);
rd.forward(req,res);
(or)
rd.include(req,res);
Approach 2: (by using ServletContextobj)
Example 1
In srv1 source code,
ServletContext sc=getServletcontext();
RequestDispatcherrd=req.getRequestDispatcher(“s2url“);
rd.forward(req,res);
(or)
Rd.include(req,res);
Example 2
In source srv1 program
ServletContext sc=getServletcontext();
RequestDispatcherrd=req.getRequestDispatcher(“/abc.html“);
(or)
RequestDispatcherrd=req.getRequestDispatcher(“/abc.jsp“);
rd.forward(req,res);
(or)
rd.include(req,res);
Approach 3: (by using ServletContextobj)
Example 1
In srvl source code,
ServletContext sc=getServletcontext();
RequestDispatcherrd=sc.getNameDispatcher(“s2”);
rd.forward(req,res);
(or)
rd.include(req,res);
Example 2
In srv1 source code,
ServletContext sc=getServletcontext();
RequestDispatcherrd=sc.getNameDispatcher(“j2”);
rd.forward(req,res);
(or)
rd.include(req,res);
• If the source servlet program calls rd.forward(req,res) method, then it performs forwarding request mode of servlet chaining with destination web resource program.
• Servlet configuration in web.xml is mandatory so it contains logical name and URL pattern.
• The JSP program configuration in web.xml file is optional so it mayor may not contain logical name and URL pattern. We cannot configure HTML in web.xml.
Type of Servlet Chaining
• In any mode of servlet chaining, all servlet programs/web resource programs use the same request and response objects. If srvI, srv2, srv3 and srv4 servlet programs are in forwarding request mode of servlet chaining, the html output of srv1, srv and srv3 is discarded and only the output of srv4 servlet program goes to the browser window.
• If srv1, srv2, srv3 and srv4 servlet programs are in including response mode of servlet chaining, the HTML output of all servlet programs together goes to the browser window.
• The source servlet program uses RequestDispatcher object to perform servlet chaining with destination web resource program.
What is Servlet Chaining?
Taking a request from a browser window and processing it by using multiple servlets as a chain is called Servlet Chaining. In servlet chaining, communication occurs between servlet chains and servlet programs to process the request given by a client.
This is a process to make available the user’s request to multiple servlets. A user provides request to the first servlet present in the chain. The first servlet uses the form fields present in the request. It can transfer the request to another servlet with the execution control. The second servlet can use the request, as it is available to it. This process can be repeated for any number of servlets. The last servlet present in the chain provides response to the user. All servlets present before the last servlet remain invisible to the user.
A question may come to your mind, as to why one would want to use a servlet chain when one could instead write a script that edits the files in place, especially when there is an additional amount of overhead for each servlet involved in handling a request? The answer is that servlet chains have a threefold advantage:
• They can be easily undone.
• They handle dynamically created content, so you can trust that your restrictions are maintained, your special tags are replaced, and your dynamically converted PostScript images are properly displayed, even in the output of a servlet (or a CGI script).
• They handle the content of the future, so you do not have to run your script every time new content is added.
• All servlet programs that participate in the servlet chaining will use some request and response objects because they process the same request that is given by the client.
• To perform servlet chaining we need RequestDispatcher object. RequestDispatcher object means it is the object of a container supplied Java class implementing javax. servlet.RequestDispatcher interface.