Today, we show you Java Servlet Forward HTTP Request Example that demo how to forward an HTTP Request from a servlet to another servlet. In order to do a forward action you should use the forward method of the RequestDispatcher. This method will forward a request from a servlet to another resrouce. The resource can be either another servlet, JSP file or HTML file. This is eighth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.
Table of contents
1. Project structure
2. Maven Dependency
3. Implement Servlet Forward HTTP Request
4. Servlet Forwarded HTTP Request
5. Deploy Java Servlet Forward HTTP Request example
Project structure
Maven Dependency
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> |
Servlet Forward HTTP Request
The forward method of the dispatcher sets the request type to DispatcherType.FORWARD.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javabycode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/forward") public class ForwardServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String message = "**********The message from ForwardServlet**********"; req.setAttribute("message", message); req.getRequestDispatcher("/display").forward(req, resp); } } |
Servlet Forwarded HTTP Request
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.javabycode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/display") public class DisplayServlet extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String message = (String) req.getAttribute("message"); PrintWriter out = resp.getWriter(); out.write("---------- Servlet forward HTTP Request Example ----------"); out.write("\n"); out.write(message); } } |
Deploy Java Servlet Forward HTTP Request example
Deploy file war on a servlet container( Tomcat 8 for example). Then we access the address such as
URL: http://localhost:8080/servlet-forward/forward
You should know that the forward method of the request dispatcher the URL is the same as the requested URL. You can see the screen shot for detail.
Here is the output result
That’s all. You can also find other servlet example in the series of Java Servlet Example.
We have an other example that uses Servlet Forward HTTP Request. Here is that post link Servlet Request Session Application Scope Attributes
Happy learing!