Introduction
This article explains Jsp:forward Action Tags in Java. The NetBeans IDE is used for sample examples.
Action Tags
It generates code in the "_jspService" method to perform a specific task.
Syntax
<jsp:Action-Tag page="URL"/>
There are many JSP Action tags; the following are some of them:
- jsp:include
- jsp:forward
- jsp:useBean
- jsp:plugin
- jsp:param
- jsp:setProperty
- jsp:getProperty
- jsp:fallback
The "jsp:useBean", "jsp:setProperty" and "jsp:getProperty" tags are used in development of beans. I have already explained those tags in the SetProperty and GetProperty articles.
jsp:forward action tag
The "jsp:forward" action tag is used to forward request to another resource; it may be JSP, HTML or another resource.
Syntax
<jsp:forward page="relativeURL | <%=expression %>" />
<jsp:param name="parametername" value="parametervalue | <%=expression%>" />
</jsp:forward>
1. Example
In this example, first we create a "index.jsp" page; This page forwards a request to another JSP page ("ShowDate.jsp"). Now the second JSP page displays the current date and time in the browser as shown below.
Now we need to use the following procedure to create this application.
Step 1
Open the NetBeans IDE.
Step 2
Now choose "Java web" -> "Web application" as in the following.
Step 3
Type your project named as "JSPActionDemo" as in the following.
Step 4
Choose your Java version and server wizard as in the following.
Step 5
Now change the code of the default "index.jsp" file and write the following code there.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:forward page="ShowDate.jsp" />
</body>
</html>
Step 6
Now create another JSP file named "ShowDate" and write the following code for it.
ShowDate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>This is index page</h1>
<% out.print("Today is: " + new java.util.Date()); %>
</body>
</html>
Step 7
Now your project is ready to run.
Right-click on the project menu, then choose "Run". The following output will be shown in the browser.
2. Example
Now in this example we pass some parameters with forward action tags and then print the parameter value with date and time.
Step 8
Now change the code of both the files with the following code.
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:forward page="ShowDate.jsp">
<jsp:param name="vname" value="c-sharpcorner.com" />
</jsp:forward>
</body>
</html>
ShowDate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>This is index page</h1>
<% out.print("Today is: " + new java.util.Date()); %><br>
<b><%= request.getParameter("vname") %></b>
</body>
</html>
Step 9
Now run again your project and the following output will be shown this time.