C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Difference between FORWARD and INCLUDE request in JSP

By: Vanka Manikanth, on 27 FEB 2017

Learn: Difference between FORWARD and INCLUDE Requests in JSP.

Still, some may tell that both requests are nearly same. But off course there is a difference here the below code illustrates both the difference clearly.

index.jsp

Here, we are taking the inputs from the user and action is set to output.jsp with a method of POST, the use of POST and GET will be covered in our next tutorial.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Diff b/w INCLUDE & FORWARD</title>
		<style type="text/css">
			body{
			text-align:center;
			}
			p{
			font-size:16pt;
			}
			h1{
			background-color:lightblue;
			}
		</style>
	</head>
	<body>
		<!-- taking inputs from the user & action triggering to output.jsp  -->
		<h1>DIFFERENCE BETWEEN INCLUDE AND FORWARD REQUEST IN JSP</h1>
		<form action="output.jsp" method="post">
		<p><label>USER NAME :</label>
		<input type="text" name="username"></p>
		<p><label>PASSWORD :</label>
		<input type="password" name="password"></p>
		<p><label>GENDER : </label>
		<input type="radio" name="gender" value="Male">MALE
		<input type="radio" name="gender" value="Female">FEMALE</p>
		<p>
		<label>DEPARTMENT :</label>
		<select name="department" required>
		<option value="">Select</option>
		<option value="Training">Training</option>
		<option value="Marketing">Marketing</option>
		<option value="Testing">Testing</option>

		</select>
		</p>
		<p><label>LANGUAGES KNOWN :</label><input type="checkbox" name="lgknown" value="Telugu">TELUGU
		<input type="checkbox" name="lgknown" value="Hindi">HINDI
		<input type="checkbox" name="lgknown"value="English" >ENGLISH
		</p>
		<button>SAVE</button>
		</form>
	</body>
</html>





output.jsp

Here, we are getting the values through request.getParameter method and displaying those using SCRIPTLETS, in development most of the coders use SCRIPTLETS, to make their code short and understandable, so try to avoid using out.print statement.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>output</title>	
		<style type="text/css">
			p{
			text-align:center;
			font-size:16pt;
			}
		</style>
	</head>
	 <jsp:include page="index.jsp"></jsp:include> <!-- this is jsp include tag, 
												  here the request of index page is included to it
												 and displays the values in the same page -->
												 
												 
	<%--  <jsp:forward page="index.jsp"></jsp:forward> --%>
	<%--HERE FORWARD REQUEST IS COMMENTED, just UNCOMMENT AND MAKE INCLUDE TAG COMMENT
	AND SEE THE OUTPUT, YOUR INDEX PAGE REQUEST WILL BE FORWARDED,
	BUT YOU CANNOT SEE THE OUTPUT PAGE RESPONSE
	 --%>
	<body>
		<%String username = request.getParameter("username");
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String department = request.getParameter("department");


		%>
		<p>ENTERED DETAILS ARE :</p>
		<p>USER NAME IS : <%=username %></p><!-- here we used scriptlets to output values -->
		<p>PASSWORD IS : <%=password %></p>
		<p>GENDER  IS : <%=gender %></p>
		<p>DEPARTMENT  IS : <%=department %></p>

		<p>LANGUGAGES KNOWN ARE:
		<%
		String lgknown[] = request.getParameterValues("lgknown");
		String cValue="";
		for(int i=0;i<lgknown.length;i++){
			cValue = lgknown[i];%>
			<%=cValue %>
			
		<% }%>
		</p>
	</body>
</html>

Output:

JSP Forward and Include Request differences

JSP Forward and Include Request differences

COMMENTS