Java Servlet Forward HTTP Request Example

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

Java Servlet Forward Http Request Example

Maven Dependency

Servlet Forward HTTP Request
The forward method of the dispatcher sets the request type to DispatcherType.FORWARD.

Servlet Forwarded HTTP Request

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
Java Servlet Forward Http Request Example

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!

Leave a Comment

*

Please share it if you found this useful
Hide Buttons