JSP Actions: Tags, Syntax, and Examples

JSP Actions: Tags, Syntax, and Examples

In this article, I am going to discuss JSP Actions: Tags, Syntax, and Examples. Please read our previous article where discussed JSP Implicit Objects. At the end of this article, you will understand the following pointers in detail.

  1. What are JSP actions?
  2. Why do we need JSP Actions?
  3. Types of JSP Actions
  4. Standard Actions
  5. The <jsp:include> Action
  6. The <jsp:param> Action
  7. The <jsp:forward> Action
  8. The <jsp:plugin> Action
  9. The <jsp:fallback> Action
  10. The <jsp:getProperty> Action
  11. The <jsp:setProperty> Action
  12. The <jsp:useBean> Action
  13. The <jsp:attribute> Action
  14. The <jsp:body> Action
  15. The <jsp:text> Action
  16. The <jsp:output> Action
  17. JSP Custom Actions
What are JSP actions?

The servlet compartment gives much implicit usefulness to facilitate the improvement of the applications. Software engineers can utilize these capacities in JSP applications. The JSP Actions labels empower the software engineer to utilize these capacities. The JSP Actions are XML labels that can be utilized on the JSP page. These labels inside utilized functionalities of the servlet-programming interface to perform tasks straightforwardly at the execution stage or solicitation handling stage.

In JSP innovation, by utilizing scripting components we can give java code inside the JSP pages, yet the fundamental of JSP innovation isn’t to permit java code inside JSP pages. To dispose of java code from JSP pages we need to wipe out scripting components, to kill scripting components from JSP pages we need to give an option, for example, JSP Actions. If there should be an occurrence of JSP Actions, we will characterize a scripting tag on the JSP page and we will give a square of java code w.r.t. scripting tag. At the point when the compartment experiences the scripting label the holder will execute individual java code, by this, an activity will be performed called JSP Action.

Types of JSP Actions

In JSP technology there are two types of actions:

  1. Standard Actions
  2. Custom Actions
Standard Actions

Standard activities are implicit labels of JSP. It very well may be characterized by the JSP innovation to play out a specific activity. JSP innovation has given all the standard activities as a bunch of predefined labels called Action Tags. These are essentially used to pass runtime data to the compartment. As a part of JSP we have the following standard actions:

The <jsp:include> Action

In JSP pages, <jsp:include> activity tag can be utilized to incorporate the substance of the objective asset into the present JSP page. <jsp:include> activity tag can be utilized to incorporate static assets where the incessant updates are accessible. It will be settled at the hour of solicitation handling. It functions as a sub-daily practice; the Java servlet briefly passes the solicitation and reaction to the predetermined JSP/Servlet. Control is then returned back to the current JSP page. This tag is utilized for preparing a customer demand by a source JSP page by including other JSP pages and static assets like HTML’s. One source JSP can incorporate the number of worker side assets lastly, we get the reaction of source JSP. This tag is given to incorporate the yield objective web asset program to yield source JSP program by rd.include(_,_) inside. It produces a separate JES class for the objective JSP page and remembers that yield for source JSP by utilizing rd.include(_,_). It performs yield incorporation.

Syntax: <jsp:include page=” ” flush=” “/>

Attributes:
  1. Page: It helps to include the relative URL of the page.
  2. Flush: It is a Boolean attribute that determines whether the included resource has its buffer flushed before it is included.
Example:

In this example, index.jsp page includes the content of include.jsp page.

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
 <h2>this is index page</h2>
 <jsp:include page="include.jsp" />
 <h2>end section of index page</h2>
</body>
</html>
include.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <%
  out.print("Today is:" + java.util.Calendar.getInstance().getTime());
 %>
</body>
</html>

Output

The <jsp:include> Action

The <jsp:param> Action

The jsp:param action is used to add the specific parameter to the current request. The jsp:param tag The jsp:param activity is utilized to add the particular boundary to the current solicitation. The jsp:param tag can be utilized inside a jsp:include, jsp:forward block. This activity tag can be utilized to give a name esteem pair to the solicitation object at the hour of by-passing solicitation object from present JSP page to target page either in an incorporate component or in forward system or in both. This label should be used as a kid tag to <jsp:include> tag and <jsp:forward> labels. This tag is given to pass information from the source page to the objective page as extra solicitation param values.

Syntax: <jsp:param name=” ” value=” “/>

Attributes:
  1. Name: It specifies the name of a parameter.
  2. Value: It specifies the value of the parameter.
Example:

In this example, we are including file.jsp to the param.jsp page. And we are passing the parameters from index to file page using <jsp:param> tag. We can extract the same using an expression tag.

param.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP include with parameters example</title>
</head>
<body>
 <jsp:include page="file.jsp">
  <jsp:param name="firstname" value="Chaitanya" />
  
  <jsp:param name="lastname" value="Singh" />
 </jsp:include>
</body>
</html>
file.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 ${param.firstname}
 <br> ${param.lastname}
</body>
</html>

Output

The <jsp:param> Action

The <jsp:forward> Action

The jsp:forward tag is utilized to hand off the solicitation and reaction to another JSP or servlet. For this situation, the solicitation stays away for the indefinite future to the calling JSP page. At the point when we need to advance a solicitation and reponse to the objective JSP page from the source JSP we should utilize <jsp:forward>. This activity is given to advance the solicitation from source JSP program to objective program. In this cycle, the yield of the source JSP program will be disposed of and just the yield of the objective program goes to the program as a reaction. It inside utilizations (_) strategy. The contrast among rd.forward(_,_) and <jsp:forward> tag in JSP writing computer programs is articulation put after rd.forward(_,_) technique in source servlet program agent however the proclamations set after <jsp:forward> tag in source JSP program doesn’t execute. <jsp:forward> tag was planned based on Forward Request Dispatching Mechanism. At the point when holder experiences <jsp:forward> label then compartment will sidestep solicitation and reaction objects to the objective asset by reviving reaction object for example by taking out past reaction accessible accordingly object, toward the finish of target asset compartment will dispatch the created dynamic reaction straightforwardly to the customer without moving back to the first asset. Accordingly, if there should be an occurrence of <jsp:forward> label customer can get just an objective asset reaction.

Syntax: <jsp:forward page=” “/>

Attributes:
  1. Page: It consists of a relative URL of another resources
Example

In this example, we are forwarding the request from index.jsp page to forward.jsp page with parameter and forward.jsp page prints the parameter values with date and time.

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
 <h2>this is index page</h2>
 <jsp:forward page="forward.jsp">
  <jsp:param name="name" value="Hello World" />
 </jsp:forward>
</body>
</html>
forward.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<body>
 <%
  out.print("Today is:" + java.util.Calendar.getInstance().getTime());
 %>
 <%=request.getParameter("name")%>
</body>
</html>

Output

blank

The <jsp:plugin> Action

In more seasoned variants of Netscape Navigator and Internet Explorer; various labels are utilized to implant applet. The jsp:plugin tag really creates the proper HTML code that implants the Applets accurately. This tag is given to show Applet/GUI java bean on the program window. In the event that the logo of the site is planned as an applet having movement uphold through strings then we need to work with <jsp:plugin> tag. This inside uses <object>/<embedded>/<applet> tag to show the applet on program. This tag can be utilized to incorporate an applet into the present JSP.

Syntax: <jsp:plugin code=”-” width=”-” height=”-” type=”-“/>

Attributes:
  1. Code: It will take the fully qualified name of the applet.
  2. Width: It is used to specify the width of the applet.
  3. Height: It is used to specify the height of the applet.
  4. Type: It can be used to specify which one we are going to include whether it applet or bean.
Example:

In this example, we are displaying Applet in JSP. We have created MouseDrag.java applet class and used <jsp:plugin> action to run the applet on a JSP page.

MouseDrag.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseDrag extends Applet implements MouseMotionListener{

public void init(){
addMouseMotionListener(this);
setBackground(Color.red);
}

public void mouseDragged(MouseEvent me){
Graphics g=getGraphics();
g.setColor(Color.white);
g.fillOval(me.getX(),me.getY(),20,20);
}
public void mouseMoved(MouseEvent me){}
}
index.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>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mouse Drag</title>
</head>
<body bgcolor="khaki">	
 <jsp:plugin align="middle" height="500" width="500" type="applet"
  code="MouseDrag.class" name="clock" codebase="." />
</body>
</html>

Output

The <jsp:plugin> Action

The <jsp:fallback> Action

The jsp:fallback tag is utilized to determine the message to appear on the program if apples aren’t upheld by the program. Jsp:fallback is the subtag of jsp:plugin label that is equipped for showing blunder message when the program doesn’t uphold applets. The principle motivation behind jsp:fallback tag is to show an elective message when the customer program isn’t supporting the <OBJECT – > tag and <EMBED – > tag.

Syntax: <jsp:fallback>- – – – – Description – – – – -</jsp:fallback>

Example:

In this example, we have created a simple Java Applet MyApplet.java class and used <jsp:plugin> action to run the applet on a JSP page index.jsp.

MyApplet.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JApplet;
import javax.swing.JLabel;

public class MyApplet extends JApplet {
 private static final long serialVersionUID = 1L;
 private JLabel label = new JLabel();

 public void init() {
  label.setHorizontalAlignment(JLabel.CENTER);
  label.setFont(new Font("Arial", Font.BOLD, 20));
  label.setForeground(Color.BLUE);
  setLayout(new BorderLayout());
  add(label, BorderLayout.CENTER);
 }

 public void start() {		
  label.setText("Hello");
 }
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Plugin action Demo</title>
</head>
<body>
 <div align="center">
  <jsp:plugin type="applet" code="MyApplet.class"
   codebase="appletCode" align="">
   <jsp:fallback>
                <p>Could not load applet!</p>
            </jsp:fallback>
  </jsp:plugin>
 </div>
</body>
</html>

Output

The <jsp:fallback> Action

The <jsp:getProperty> Action

The jsp:getProperty tag is utilized to get indicated property from the JavaBean object. The principle reason for <jsp:getProperty> tag is to execute a getter strategy to get an incentive from Bean object.

Syntax: <jsp:getProperty name=” ” property=” “/>

Attributes:
  1. Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
  2. Property: It will take a particular property to execute the respective getter method.
The <jsp:setProperty> Action

The jsp:setProperty tag is utilized to set a property in the JavaBean object. The principle motivation behind jsp:setProperty tag is to execute a specific setter technique to set an incentive to a specific Bean property.

Syntax: <jsp:setProperty name=” ” property=” ” value=” “/>

Attributes:
  1. Name: It will take a variable that is the same as the id attribute value in <jsp:useBean> tag.
  2. Property: It will take a property name in order to access the respective setter method.
  3. Value: It will take a value to pass as a parameter to the respective setter method.
Example of JSP getProperty and setProperty Actions:

In this example, we have created a Simple Bean class Test.java and index.jsp page which loads the bean and sets/gets a simple String Parameter.

Test.java
package Action;
public class Test {
 private String message = "No message specified";

 public String getMessage() {
  return (message);
 }

 public void setMessage(String message) {
  this.message = message;
 }
}
index.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>
<title>Using JavaBeans in JSP</title>
</head>
<body>
 <h2>Using JavaBeans in JSP</h2>
 <jsp:useBean id="test" class="Action.Test" />
 <jsp:setProperty name="test" property="message" value="Hello JSP..." />
 <p>Got message....</p>
 <jsp:getProperty name="test" property="message" />
</body>
</html>
</html>

Output

Example of JSP getProperty and setProperty Actions

The <jsp:useBean> Action

The jsp:useBean tag is utilized to start up an object of JavaBean or it can re-utilize the existing java bean object. The primary motivation behind jsp:useBean tag is to interface with bean objects from a specific JSP page. In this tag, it is consistently suggestible to give either application or meeting degree to the extension characteristic worth. At the point when the compartment experiences this label then the holder will get class characteristic worth for example completely qualified name of Bean class then the holder will perceive Bean .class record and perform Bean class stacking and launch. Subsequent to making the Bean object compartment will allot Bean object reference to the variable indicated as an incentive to id property. In the wake of getting Bean object reference holder will store Bean object in an extension indicated as an incentive to scope trait.

Syntax: <jsp:useBean id=” ” class=” ” type=” ” scope=” “/>

Attributes:
  1. Id: It will take a variable to manage generated Bean object reference.
  2. Class: This attribute will take the fully qualified name of the Bean class.
  3. Type: It will take the fully qualified name of the Bean class to define the type of variable in order to manage the Bean object reference.
  4. Scope: It will take either of the JSP scopes to the bean object.
Example:

In this example, we have created a simple Bean class Calculator.java and we are invoking the method of Bean class in the index.jsp.

Calculator.java
package Action;
public class Calculator {
 public int cube(int n) {
  return n * n * n;
 }
}
index.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>
<title>Using JavaBeans in JSP</title>
</head>
<body>
 <jsp:useBean id="obj" class="Action.Calculator" />
 <%
  int m = obj.cube(5);
 out.print("cube of 5 is " + m);
 %>
</body>
</html>

Output

The <jsp:useBean> Action Tag

The <jsp:attribute> Action

The jsp:attribute tag is utilized to characterize the XML content powerfully. In jsp:attribute label the components can be created during demand time than assemblage time. This tag really characterizes the trait of XML which will be produced progressively.

Syntax: <jsp:attribute>——</jsp:attribute>

The <jsp:body> Action

The jsp:body tag is utilized to characterize the XML progressively. In jsp:body label the components can produce during demand time than accumulation time. This tag really characterizes the XML, which is produced progressively component body

Syntax: <jsp:body>——–</jsp:body>

Example of JSP Body and Attribute Action Tag

In this example, we have defined an element which dynamically generated as XML, an attribute that is the XML attribute of dynamically generated XML, and body action where we are writing the XML body which will be generated in dynamically XML.

index.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>Body and Attribute Tag</title>
</head>
<body>
 <jsp:element name="XMLElement">
<jsp:attribute name="XMLattribute">
Value
</jsp:attribute>
<jsp:body>Hello World</jsp:body>
</jsp:element>
</body>
</html>

Output:

Example of JSP Body and Attribute Action Tag

The <jsp:text> Action

The <jsp:text> is utilized to layout text in JSP pages. The assemblage of jsp:text tag doesn’t contain some other components and it contains just content and EL articulations.

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

Example:

In this example, we are taking text object to print the template text.

index.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>Text Action</title>
</head>
<body>
 <jsp:text>Hello World</jsp:text>
</body>
</html>

Output

The <jsp:text> Action Tag

The <jsp:output> Action

The jsp:output determines the XML statement or the DOCTYPE revelation of JSP. This tag proclaims XML revelation and DOCTYPE.

Syntax: <jsp:output doctype-root-element=” ” doctype-system=” “>

Attributes:
  1. Doctype-root-element: It indicates the root element of XML document in DOCTYPE.
  2. Doctype-system: It indicates doctype which is generated in output and gives the system literal.
Example:

In this example, we are using the output object to generate a DOCTYPE, and internally it will be generated.

index.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>Action Guru JSP8</title>
</head>
<body>
 <jsp:output doctype-root-element="html PUBLIC"
  doctype-system="http://www.w3.org/TR/html4/loose.dtd" />
</body>
</html>

Output: There won’t be any output for this as this will be generated internally.

JSP Custom Actions

In JSP innovation, by utilizing JSP mandates we can characterize present JSP page qualities, we can’t utilize JSP orders to perform activities in the JSP pages. To perform activities in the event that we use scripting components, at that point we need to give Java code inside the JSP pages. The primary subject of JSP innovation isn’t to permit Java code inside the JSP pages, to dispense with Java code from JSP pages we need to wipe out scripting components, we need to utilize custom activities. On the off chance that we need to take out scripting components from JSP pages, we need to utilize activities. Custom activities are JSP activities that could be set up by the designers according to their application prerequisites. In JSP innovation, standard activities will be spoken to as a bunch of predefined labels are called Action Tags. In JSP Technology, standard activities will be spoken to as a bunch of predefined labels are called Action labels. Additionally, all the custom activities will be spoken to as a bunch of clients characterized labels are called Custom Tags.

Syntax: <prefix_Name : tag_Name>—–Body—–</prefix_Name>

If we want to design custom tags in our JSP applications then we have to use the following 3 elements:

  1. JSP page with Taglib directives: It is used to make available the TLD files into the present JSP page on the basis of custom tags prefix names.
  2. TLD (Tag Library Descriptor) file: This tag provides the mapping between custom tag names and respective TagHandler classes.
  3. TagHandler class: It is a normal Java class that is able to provide the basic functionality for the custom tags.

In the next article, I am going to discuss JSP Custom Tags with Examples. Here, in this article, I try to explain JSP Actions: Tags, Syntax, and Examples. I hope you enjoy this JSP Actions article.

Leave a Reply

Your email address will not be published.