The sendRedirect is a method defined in the
HttpServletResponse
interface. The signature of the method is like
void sendRedirect(java.lang.String location)
.
With this method, a servlet that implements
HttpServletResponse
, can send a redirect response back to the client so that the
client can make a fresh request to the location URL sent as
a parameter.
In this current tutorial, we will assist you in understanding when and how it could be applied, moreover, we will also point out the difference between servlet interactions through
RequestDispatcher
and
sendRedirect
How and when sendRedirect() is called
The below code snippet explains how you can call a sendRedirect method
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int firstNo = Integer.parseInt(request.getParameter("first"));
int secNo = Integer.parseInt(request.getParameter("second"));
String msg = "First Number is Greater";
if(firstNo < secNo)
msg = "Second Number is Greater";
response.sendRedirect("ResultServ?msg=" + msg);
}
The above code shows the
doGet
method of a sample Servlet called DemoServ. It
takes up two numbers from an HTML form having names first & second
and stores it in two variables
firstNo
&
secNo
.
Depending on the condition whether
firstNo
or
secNo
is greater, an appropriate String object
msg
is computed.
Finally, the
sendRedirect
method of the response object is called with a specific URL call to
another servlet ResultServ which is within the
same package as that of DemoServ, along with the
precomputed
msg
as the value of a parameter msg
Below is the code snippet of
doGet()
method of ResultServ
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String resultMsg = request.getParameter("msg") ;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("" + resultMsg + "");
}
When requested,
doGet()
method gets the value of the msg parameter and ultimately
shows that in html
So this was how you can apply
sendRedirect()
method, but more importantly, you need to understand the course of
actions that take place within this.
When
sendRedirect()
is called with a URL of ResultServ from DemoServ,
the response gets back to the client and a fresh request is
generated from that client to the ResultServ as
per the URL and consequently it is served by the ResultServ.
This means with every
sendRedirect()
call, the existing server cycle gets completed, and another new one
is created.
It is evident that whenever the ResultServ is called through the
sendRedirect()
call, the parameter values are always visible in the browser.
With all the previous discussions in mind, we can conclude that we can use
sendRedirect()
if server cycles are not that costly, and the application
information are trivial enough to be exposed in the browser.
The result of applying
sendRedirect()
method looks pretty much similar to the application of
RequestDispatcher
as with both of them, inter resource communications take place.
But a closer look to both of them will reveal the differences
between these two.
The difference between using RequestDispatcher object and sendRedirect method
The prime difference between dispatching a request throughRequestDispatcher
and redirecting an URL through
sendRedirect()
is like following
- Dispatching requests is request object-centric whereas redirecting an URL is response object-centric
- When a request is dispatched, it can only communicate another resource present in the same web container, but with URL redirecting, any resource even outside the web container can also be communicated.
- Every new call to
sendRedirect()
creates a new server cycle from the client, whereas usinginclude()
orforward()
ensures that the same server cycle is being continued.
In case, you want a clear concept of how RequestDispatcher works, you can follow this tutorial
Hope you have enjoyed this tutorial.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.