Action Tags in JSP

In the previous article, we have seen JSP Directives such as page Directive, include directive and taglib directive in detail. In this article, we will cover all the JSP Action Tags available on the JSP page.

This Action Tags simply means what action to be done while the page is being accessed and to control the behavior of the Servlet engine. Let’s see first what is Action tags in detail.

What is JSP Action?

The JSP Action Tags are used to control the flow between pages and JAVA bean.

  • JSP actions use the constructs in XML to control the behavior of the servlet engine.
  • It helps us to insert a file dynamically, using Java beans.
  • It helps us to forward users to the next page.
  • There are different Actions Tags available and each is used to perform different tasks.

Syntax for JSP Action Tag: It follows the XML standard.

<jsp:action_name attribute="value" />

There are different types of JSP Tags available Let’s see each in detail.

JSP:useBean

This action tag creates or locates the Java Bean.

  • This action name is used when we want to include Java bean in JSP pages.
  • It is used for locating Java Bean.

Syntax

<jsp:useBean id="" class="" />

Example

In this example, we will use bean id with the classpath of Bean.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Action Tags:-> jsp:useBean</title>
</head>
<body>
<jsp:useBean id="id1" class="com.demoaction"></jsp:useBean>
</body>
</html>

JSP:include

This Action Tag is used to include the page when requested.

  • It is used to include one file into another as we saw earlier include directive.
  • It added at the time of request processing.

Syntax

<jsp:include page="page URL" flush="true/false">

Example

In this example, we will try to include one file into another.

datefile.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Date</title>
</head>
<body>
Date: <%
Date date=new Date();
date.toString();
out.println(date.toString());
%>
</body>
</html>

useofinclude.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="datefile.jsp"></jsp:include>
</body>
</html>

Output: When you will run include.jsp file it will include the file datafile.jsp

JSP:setProperty

This action tag is used to set the property of the Java Bean class.

  • It sets the value of the property of the bean.
  • Before setting the property we should have a bean class.

Syntax

<jsp:setproperty name="" property="" >

JSP:getProperty

This action tag is used to print the property of the bean.

  • It is used to get the property of Java Bean.
  • It retrieves the values of the property and convert it into a string and then insert it into output.

Syntax

<jsp:getAttribute name="" property="" >

Example for setProperty and getProperty

In this example, I am trying to set the property using setProperty and get the value of the property using getProperty.

Beanclass.java

package com;

public class Beanclass {
  
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
}

setandgetproperty.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<jsp:useBean id="name1" class="com.Beanclass"></jsp:useBean>
<jsp:setProperty property="name" name="name1" value="My name is Chandler"/>
<jsp:getProperty property="name" name="name1"/>
</body>
</html>

Output

JSP:forward

This action tag is used to forward the request and response to another page or resource.

  • It forwards the request to another page.
  • The page where the action is forward is appended in the forward tag.

Syntax

<jsp:forward page="value">

Example

In this example, when we call the forward.jsp the request gets forwarded to page we specify.

forward.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="forwarded.jsp"></jsp:forward>
</body>
</html>

forwarded.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<strong>Forwarded Page</strong>
<p>Using forward Action Tag</p>
</body>
</html>

Output

 

JSP: plugin

This action tag is used to include the java components in the JSP page. It is used when there is a need for a plugin to run a Bean class.

Syntax

<jsp:plugin type="applet/bean" code="classname" codebase="objectclassname">
  • The type here indicates the applet or bean used in the page.
  • code specifies the class name.
  • The codebase has the URL that contains all the files.

JSP param

This action tag is used to set the parameter value.

Syntax

<jsp:params>
<jsp:param name="value" value="value"/ >
</jsp:params>

Example

In this example, we will be using JSP param tags to set the value and in the second.jsp page, we will retrieve it using reqeust.getParamrter(“name”).

param.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>use of Param tags</title>
</head>
<body>
<jsp:forward page="second.jsp">
<jsp:param value="Nicolas" name="name"/>
</jsp:forward>
</body>
</html>

second.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="#A5D8F3">
<h1>Use of Param Tags</h1>
<%=request.getParameter("name")%>
</body>
</html>

Output

JSP:body

This action tag is used to define an XML element dynamically.

Syntax

<jsp:body></jsp:body>

JSP-attribute

This attribute is used to define XML elements attributes dynamically.

  • The JSP attribute element accepts the name and trims attribute.
  • The name attribute identifies which tag attribute is being specified.
  • The optional trim attribute determines whether or not white space appearing at the beginning and end of the element body should be discarded.

Syntax

<jsp:attribute></jsp:attribute>

JSP:text

This action Tag is used to write template text in JSP pages.

Syntax

<jsp:text>template text</jsp:text>

JSP:output

This action tag is used to specify the XML declaration just like the doctype declaration of JSP.

  • doctype-root-element indicates the root element of XML.
  • Doctype-system tells us doctype which is generated in output.

Syntax

<jsp:output doctype-root-element="" doctype-system="">

Thus these are the Action Tags we use in our JSP page to make things simpler and easy.

In the next article of this tutorial, we will cover the Expression languages on the JSP page with a simple example.