Home » Java programming language

Difference between FORWARD and INCLUDE request in JSP

Learn: Difference between FORWARD and INCLUDE Requests in JSP.
By: Vanka Manikanth, on 27 FEB 2017

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 and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.