Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » JSP » Create a JSP page to demonstrate jsp forward action tag
Suryateja Pericherla Categories: JSP. No Comments on Create a JSP page to demonstrate jsp forward action tag
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Create a JSP page to demonstrate jsp:forward action tag.

 

Theory

JSP

JSP is one of the most used server-based technologies, it separates business logic from the presentation layer of any project. JSP (Jakarta Server Pages, formerly called Java Server Pages) is a technology that is used to develop different interactive Web pages, web applications, and applications which are normally HTML pages that are embedded with Java code. JSP is an updated version of Java Servlets and was developed by Sun Microsystems.

 

Following are the differences between Servlets and JSP:

Servlets JSP
In Servlets we write the HTML code using the Java code In JSP we write the Java code using the HTML code
Servlets execute faster as they are executed directly by the server JSP runs slower as it needs to be converted into Servlet first and then it is run by the server
In the MVC (Model View Controller) pattern, Servlet will be used as a controller In the MVC pattern, JSP will be use generally as a view
Servlets accepts other protocols including HTTP/S requests JSP accepts only HTTP/S protocol requests
Session management is by default disabled Session management is by default enabled
Servlets are difficult to code when compared to JSP JSP is easier to code when compared to Servlets
Making any change in a Servlet requires recomp-ilation and restart of the server Making changes doesn’t require any recompilation or restart of the server

 

JSP Action Tags

There are many JSP action tags or elements. Each JSP action tag is used to perform some specific task. The action tags are used to control the flow between pages, to use a Java Bean, etc. The JSP action tags are given below:

JSP Action Tag Description
jsp:forward Forwards the request and response to another page
jsp:include Includes another resource in the current page
jsp:useBean Creates or locates a bean object
jsp:setProperty Sets the value of property in bean object
jsp:getProperty Gets the value of property in bean object
jsp:plugin Embeds another component such as applet
jsp:param Sets the parameter value. It is generally used along with forward and include tags.
jsp:fallback Can be used to print a message if a plugin is not working

 

jsp:forward Action Tag

The jsp:forward action tag is used to forward the request to another resource it may be jsp, html or any other resource.

 

Syntax of jsp:forward action tag without parameter is:

<jsp:forward page="relativeURL | <%= expression %>" />

 

Syntax of jsp:forward action tag with parameter is:

<jsp:forward page="relativeURL | <%= expression %>">  
	<jsp:param name="parametername" value="parametervalue | <%=expression%>" />  
</jsp:forward>

 

Program

login.html

<html>
	<head>
		<title>Login Page</title>
	</head>
	<body>
		<form action="Validate" method="post">
			<label>Username: </label>
			<input type="text" name="txtUsername" /><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Submit" />
			<input type="reset" value="Clear" />
		</form>
	</body>
</html>

 

Validate.java

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Validate extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String uname = request.getParameter("txtUsername");
		String pass = request.getParameter("txtPassword");
		if(uname.equals("admin") && pass.equals("123456"))
		{
response.getWriter().print("<html><head><title>Login Success</title></head>");
			response.getWriter().print("<body>");
			response.getWriter().print("<p>Login successful</p>");
			response.getWriter().print("<p><a href=\"uhome.jsp\">User Home</a></p>");
			response.getWriter().print("</body></html>");
		}
		else
			response.getWriter().print("Incorrect username or password. Try again!");
	}
}

 

uhome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
	<title>User Home</title>
</head>
<body>
	<h1>User Home Page</h1>
	<jsp:forward page="gallery.jsp"></jsp:forward>
</body>
</html>

 

gallery.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
</head>
<body>
	<h1>User's Gallery</h1>
</body>
</html>

 

Input and Output

login.jsp

JSP forward action tag login

 

Invalid Details

JSP forward action tag invalid details

 

Valid Details

JSP forward action tag login success

 

gallery.jsp

JSP forward action tag gallery

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory