Updated April 6, 2023
Introduction to JSP Forward
The JSP has a set of different features for creating the web pages in the application will be more sophisticated. Among this jsp have set of predefined tags for working the functions of the web applications we have used mostly navigate the web pages using include and forward tags the tag which is used for forwarding the request from one page into another page it may be any resources like jsp, servlet or html the given request be either parametrized or non-parameterized based on the user requirement but the main focus is to terminate the current web page and also binded with the forward page.
Syntax:
The basic syntax of the forward tag in the web applications as below.
<html>
<body>
<jsp:forward page="">
----some logic codes----
</body>
</html>
In the above codes, the forward tag is used for navigating the web pages and forward the request from one page into another page resources it can be either jsp or static web pages like html or java servlets the page requests are forwarded using with or without parameters.
How Does JSP Forward?
The jsp forward tag will use mainly in the action event in the web page that is used for forwarding the request from one-page resources it will be either the html, jsp or any java servlet files it has to be loaded after the user clicks the events or functions of the web page. The jsp action tags allow the perform actions on the jsp pages and it forwards the http requests into the jsp or servlet page. It also includes the data contents of another jsp, html web pages within the jsp web page. We can create the java bean used to create the instance of the html ui tag elements like getter and setter methods we can set the values of its instance variables.
The forward also one of the action tags like added the different set of extension files into the current jsp file or page. The included file resources also can be any static page for jsp,html or java servlet. We can also include the arguments in the particular values for the specific resources file its also be included in the forward tag. Using <jsp:param…./> tag we can add the parameters or arguments in the file. If we use the include in the <%@forward file=””%> header page whatever the changes we make to include in the files it’s not be reflected in the application but if we use the include in the inside of the web page <jsp:forward page=””/> that is the action time its will reflected the changes in the web applications.
While Accessing the jsp directives in the more number of times somewhere is not update the time when we use the tomcat application server it reflects the changes but the container won’t check the included files timestamp also not updated but include directive can include only static page contents, values are assigned in the property file it must be path and file names files exist otherwise file not found exception occurred at run time the files extension may be any types like .html,.jsp,.js, etc. If suppose, we have a servlet back end code in our application it may not be included in the include header tag when we include the same servlet in the tag we get the exception like file not found error at the run time. But when we include the servlet in the action tag format <jsp:forward=”/servletname”/> the include action tag accept not only the static page contents it also supports dynamic web page contents. The forward in action tag format it supports the requested time interval if it exceeds the server timeout error will be shown and also page contents do not affect the main page of the web applications it includes and shown the output of the included jsp pages.
When compared to the forward tag as the directive as well as action scenarios in directive files forward it include static format only and also files are changed randomly if we use common codes in the snippet that we can reuse the multiple pages. The forward directive code is the reusability page also translates the source codes at only once so it will be better to include the file resources as static.
Examples of JSP Forward
Here are the following examples mention below
Example #1
Code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample</title>
</head>
<body>
<c:iftest="${param.B == 0}">
<jsp:forward page="date.jsp"/>
</c:if>
<h2 align="center">
Result is ${param.A} / ${param.B} is ${param.A / param.B}
</h2>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Second</title>
</head>
<body>
<h2 align="center">Number is can't divide by zero</h2>
</body>
</html>
Sample Output:
Example #2
Code:
<html>
<head>
<title>Sample</title>
</head>
<body>
<p align="center">Welcome To My Domain</p>
<jsp:forward page="date.jsp"/>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Second</title>
</head>
<body>
<h2 align="center">Welcome User</h2>
</body>
</html>
Sample Output:
Example #3
Code:
<html>
<head>
<title>Home Page</title>
</head>
<body>
<jsp:forward page="date.jsp">
<jsp:param name="name" value="Siva"/>
<jsp:param name="site" value="Metasploit.com"/>
<jsp:param name="date" value="02/05/2020"/>
<jsp:param name="user" value="siva"/>
</jsp:forward>
</body>
</html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<h2>Welcome To My Domain</h2>
I am: <%=request.getParameter("name") %><br>
My Website: <%=request.getParameter("site") %><br>
Date: <%=request.getParameter("date") %><br>
Current User: <%=request.getParameter("user") %>
</body>
</html>
Sample Output:
The above three examples we use three different scenarios in forward tags the first example we use the concepts like jstl javascript tag library we pass the values as param type the second example is basic forward action tag and the final example we can use the normal request.getParameter type as the arguments in the param type and forward or navigate the pages from one page into another page.
Conclusion
The jsp forward directive and action tags are two different types for translate the jsp codes into the jsp containers we can add all other extension files in the single web page for work in the navigation for web applications. We can forward the web pages but in url, the menu does not change the page.
Recommended Articles
This is a guide to JSP Forward. Here we discuss an introduction to jsp forward along with working and respective examples. You may also look at the following articles to learn more –