0% found this document useful (0 votes)
5 views5 pages

Servlet To Servlet

Servlets are Java programs that run on the server-side to generate dynamic responses to client requests. They can call one another using RequestDispatcher or sendRedirect methods, with RequestDispatcher forwarding requests internally without changing the URL, while sendRedirect provides a temporary redirect to a different URL. The RequestDispatcher interface allows for dispatching requests to other resources like servlets or JSPs on the same server.

Uploaded by

22btcse067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Servlet To Servlet

Servlets are Java programs that run on the server-side to generate dynamic responses to client requests. They can call one another using RequestDispatcher or sendRedirect methods, with RequestDispatcher forwarding requests internally without changing the URL, while sendRedirect provides a temporary redirect to a different URL. The RequestDispatcher interface allows for dispatching requests to other resources like servlets or JSPs on the same server.

Uploaded by

22btcse067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Servlet

(Servlet to Servlet)
Servlet overview
• Servlets are the Java programs that run on the server-side and generate dynamic
responses to the client request.
• The execution of the Servlet involves the following steps:
• The client sends the request through the browser to the Web server/Application
Server.
• The server passes the client request to the respective servlet.
• Servlet processes the request generates the response and sends the response back
to the server.
• The server passes the generated response to the browser/client
Call a servlet from another servlet
• Now, let’s consider we have a requirement to call a servlet from another servlet
bypassing the information using request and response objects. We can achieve
this in 2 ways, servlet provides
• RequestDispatcher()

• sendRedirect()
RequestDispatcher Interface
• Servlets provide RequestDispatcher in javax.servlet package. It defines an object to
dispatch the client request from one servlet to another servlet on the same server.
• Basically, this can be used to receive the client request object and send that object to any
other resource such as Servlet, JSP, HTML file, etc. We can get the RequestDispatcher
object using the below syntax:
RequestDispatcher r = req.getRequestDispatcher(String arg);
forward() method
• This method forwards a request from a servlet to another servlet on the same server. It
allows one servlet to do the initial processing of a request, obtains the RequestDispatcher
object, and forwards the request to another servlet to generate the response.
r.forward(req, resp);

Note : When we are forwarding the client request using the forward() method, the client/browser doesn’t
even know that the request is forwarding to another servlet file or JSP file or from which file response will be
generating. There will be no change in the URL in the browser.
sendRedirect() method
• It sends a temporary redirect response to the client using the specified redirect
location URL given in the method and clears the buffer.
• Let’s consider a scenario, where we need to redirect the request from a servlet to
other servlets which is in another server, in this case, we can use the
sendRedirect() method so that the client/browser knows where the request is
going and getting processed.
resp.sendRedirect(java.lang.String location)

You might also like