Two Ways to Chain Servlets

In Servlets/JSPs, there are two ways to achieve servlet chaining using javax.servlet.RequestDispatcher:

  1. Include:
    RequestDispatcher rd = req.getRequestDispatcher("Servlet2");rd.include(req, resp);
  2. Forward, where req is HttpServletRequest and resp is HttpServletResponse:
    	RequestDispatcher rd = req.getRequestDispatcher("Servlet3");	rd.forward(req, resp);

In JSPs, the above code would look like this:

 	RequestDispatcher rd = request.getRequestDispatcher("Servlet4");	rd.forward(request, response);

When you use servlet chaining, you delegate the responsibility of generating the response to the next servlet in the chain.

But, suppose you have some data generated by current servlet that will be required by next servlet. How would you pass it?

You could keep it in the session, but you would need to remove it from that session after you fetch it in next servlet?otherwise your session will have useless data once your request is complete. Not only does this add overhead, but it also makes your next servlet dependent on session tracking.

You can attach your additional data with the request that you are passing to next servlet:

	RequestDispatcher rd = req.getRequestDispatcher("Servlet3");	req.setAttribute("key1", value1);	req.setAttribute("key2", value2);	rd.forward(req, resp);

In the above code, req is HttpServletRequest and resp is HttpServletResponse. The code attaches two data items, value1 and value2, with the req object. Use the following code to access this value in the next servlet (Servlet3 in the example):

	Object val1 = req.getAttribute("key1");	Object val2 = req.getAttribute("key2");

This attaches two data items (value1 and value2) with the HttpServletRequest object and forwards them to Servlet3. Servlet3 reads them from the HttpServletRequest and uses them.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may