JSP Actions – JSP Action Tags, Syntax and Examples
Placement-ready Online Courses: Your Passport to Excellence - Start Now
JSP Actions are one of the three important elements of a JSP page. They perform some specific tasks and are predefined. They provide functionalities like-
- Dynamic insertion of a file
- Controlling behavior of the servlet engine
- Forwarding a user to another page
- Controlling flow between pages
JSP Actions
XML format is used to define JSP Actions. What makes them different from directives is that each time a page is visited, if it contains actions then those actions get re-evaluated.
Common Syntax:
<jsp:action_name attribute=”value”/>
There are in all eleven JSP action tags. They are:
- jsp:include
- jsp:useBean
- jsp:setProperty
- jsp:getProperty
- jsp:forward
- jsp:plugin
- jsp:body
- jsp:text
- jsp:element
- jsp:param
- jsp:attribute
- jsp:output
Before discussing these JSP actions, there are two important attributes that are common to all actions. They are id and scope. id refers the object uniquely created by actions, scope tells about the life of that object. The scope attribute has four values. They are page, application, session, and request.
1. <java:include>
This action will include the required resources like html, servlets and JSP.
This jsp:include action is different from jsp directive. Include directive includes resources at the time of translation, whereas include action includes resources dynamically at request time. Action directives work well for static pages, whereas later works better for dynamic pages.
There are two attributes under include:
- Page: its value is the url of the required resource.
- Flush: it checks that the buffer of the resource is flushed before it is included.
Syntax:
<jsp:include page="page URL" flush="true/false">
Advantages of include actions are:
- It is best for dynamic pages.
- It promotes code reusability as we include pages
- This saves time as we can use one page again and again.
- It can be created with or without parameters.
Example:
Main.jsp <html> <head> <title>JSP Actions</title> </head> <body> <h3>--DataFlair--</h3> <jsp:include page="date.jsp" flush="true" /> </body> </html> date.jsp <html> <head> <title>JSP Actions</title> </head> <body> <p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
Explanation:
In this example we can see that main.jsp includes date.jsp that gives date and time, whereas date.jsp contains the code to retrieve the date.
Output:
2. <jsp:usebean>
If we want to instantiate bean (many objects encapsulated in one object) class in our JSP page, then this action comes into play. It has following attributes:
- id: id uniquely identifies bean in a specified scope.
- Scope
- Page: It is the default scope that states that we can use bean within this JSP page.
- Request: It is broader than a page as bean can be used from any page that processes similar requests.
- Session: It has a wider range than above as bean can be used from any page in the same session.
- Application: It has the maximum range as bean can be used from any page present in the same application.
- Class: Creates object of bean class.
- Type: It gives the data type to the bean existing in the specified scope. It is used with class and beanName.
- beanName: Uses the java.beans.Beans.instantiate() method and instantiates bean.
Syntax:
<jsp:useBean id= "bean" scope= "page/(session/request/application)" class= "package.class" type= "package.class" beanName="package.class > </jsp:useBean>
Example:
square.java Package demotest; public class square { public int square(int n) {return n*n;} } example.jsp <html> <title>usebean</title> <body> <jsp:useBean id="obj" class="demotest.square"/> <% int m=obj.square(3); out.print("square of 3 is "+m); %> </body></html>
Explanation:
In the java file square.java we prepare a basic class and in jsp file we invoke useBean. The object is thus instantiated, referenced through id. We generate the square of the given number.
Output:
3. <jsp:setProperty>
jsp:setProperty and getProperty are used with usebeans. It is used to modify the properties of beans. It gets executed when new objects are created.
This has four attributes:
- name: It is literally the name of the bean whose property we want to set. It has the same name as the object we instantiated using usebean.
- property: sets the property of the bean “*” means all the requests that match bean properties are included.
- value: gives specific value to the bean.
- Param: a name that will fetch the value .
Syntax:
<jsp:setProperty name=“bean” property= “*”| property=“propertyName” value= “<%=expression>”| property=“propertyName” param= “Parametername”
**Example with get property
4. <java:getProperty>
This action gets the value of the property and gives it out on the output as a string.
To get the property of a bean, bean and its name must be defined. Our main purpose is to get the value of the property where the property is the name of the bean property that was set by setProperty as well. The attributes used by getProperty are
- name: It gets the name of the bean whose property we need to get as its value.
- property: It defines the name of the property required for the bean.
Syntax:
<jsp:setProperty name="bean" property="message" value="Myexample" /> <jsp:getProperty name="MyPage" property="message" />
Example:
TestBean.java package demotest; import java.io.Serializable; public class TestBean implements Serializable { private String message = "null"; public String getMessage() { return msg; } public void setMessage(String message) { this.message = message; } } GetSet.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Get Set Property</title> </head> <body> <jsp:useBean id="myBean" class="demotest.TestBean" /> <jsp:setProperty name="myBean" property="message" value="GetSetActions" /> <jsp:getProperty name="myBean" property="message" /> </body> </html>
Explanation:
Firstly, a TestBean class of package demotest that implements serializable class has been defined that gets and sets the message using getters and setters.
In jsp code, we used useBean action that will instantiate the object myBean. Then we set the property of the object and give it a value “GetSetAction”. Thereafter, using getProperty we fetch the value for myBean that was set as GetSetAction as our output.
Output:
5. <jsp:forward>
This action as the name suggests forwards the request to another page. This other page may be static as well as a JSP page.
Syntax:
main.jsp <jsp:forward =“otherpage.jsp”>
Example:
Forward.jsp <html> <head> <title>JSP Forward</title> </head> <body> <a>I was the requested page but forwarded the request to other</a> <jsp:forward page="other.jsp" /> </body> </html> Other.jsp <html> <title>JSP Forward 2</title> <h3>DataFlair</h3> </head> <body> <a>I am the other page where request is forwarded.</a> </body> </html>
Explanation: User calls main.jsp but gets forwarded to other.jsp
Output:
6. <jsp:plugin>
Plugin inserts java objects or embed tags into a JSP page. It has three attributes:
- type: applet/bean
- code: It is the name of the class file.
- codebase: It is the directory of the name of the class file.
Syntax:
<jsp:plugin type="bean/applet" code="className" codebase="package.class">
**Example of plugin with param
7. <jsp:param>
It sends parameters to a bean or applet. It can also be said as the child object of a plugin.
Syntax:
<jsp:params> <jsp:param name=“__” value="__"/ > </jsp:params>
Example:
First.jsp <html> <head> <title>Plugin Param </title> </head> <body> <jsp:plugin type="bean" code="colour.class" codebase="demotest.colour"> <jsp:params> <jsp:param name="id" value="5" /> <jsp:param name="nameofcolour" value="red" /> </jsp:params> </jsp:plugin> </body> </html> Colour.java package demotest; import java.io.Serializable; public class Colour implements Serializable { public String getName () { return name; } public void setName (String name) { this.name = name; } public int getId() { return id; } public void setId (int id) { this.id = id; } //getters and setters private String name = "null"; private int id = 0; }
Explanation: In the First.jsp we are using plugin attributes type, code and codebase. In that we are having param child objects that get the name and value for the plugin. Whereas in Colour.java, we have used getters and setters to get and set the values of name and id.
Output:
8. <jsp:body>and <jsp:element>
In the body part of this action, we can define XML. Elements will be generated at runtime i.e. dynamically.
Syntax:
<jsp:body>..... </jsp:body>
**Example of element, attribute, body together below.
9. <jsp:attribute></jsp:attribute>
Once XML is defined, we can also define its attribute dynamically.
Syntax:
<jsp:attribute name="Put your XMLattribute here.."> </jsp:attribute>
Example:
<html> <head><title>ele attr body</title> </head> <body> <jsp:element name="XMLelement"> <jsp:attribute name="XMLattribute"> </jsp:attribute> <jsp:body> My XML</jsp:body> </jsp:element> </body> </html>
Explanation:
This code contains the example of element, attribute, body of a dynamically generated HTML.
Output:
10. <jsp:text>
It will contain the template data. It can have either text or EL expressions.
Syntax:
<jsp:text> Enter your text here…</jsp:text>
Example:
<html> <head> <title>Template</title> </head> <body> <jsp:text>Here goes the template text.</jsp:text> </body> </html>
Explanation: Under jsp:text action we can output the template text.
Output:
11. <jsp:output>
JSP output contains two attributes i.e doctype-root-element that will possess the root element of the generated XML and doctype-system that shows doctype that will be generated in output.
Syntax:
<jsp:output doctype-root-element="__" doctype-system="__">
Example:
<html> <head> <title>Output Actions</title> </head> <body> <jsp:output doctype-root-element="XML" doctype-system="http://www.myxml.com/loose.dtd"/> </body> </html>
Explanation: This output is generated internally thus we can’t infer output
Conclusion
In reference to this article, we came to know about different action tags, their attributes and values. They are written in XML format. They stimulate the behaviour of the servlet engine and perform specific tasks.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google