JSP – Action Tags
Last Updated :
08 Apr, 2024
In Java, JavaServer Pages can provide action tags that can enable the developers to perform specific tasks including the other files, forwarding the requests, and manipulating the session data within the JSP documents. Action tags are denoted by the <jsp: ..> syntax and it is used to perform the dynamic actions on the server side of the JSP applications.
Understanding of the JSP Action Tags
JSP action tags are the special tags that can be used to provide instructions to the JSP container on how to manage the server-side actions. It can be enclosed with the <jsp: .. > tags and it can allow the developers to perform various tasks such as including other files, forwarding the requests, and manipulating the session attributes.
List of the Commonly used JSP Action Tags in JSP
Tag Name
| Syntax
| Description
| Example
|
---|
Include Tag
| <jsp:include page=”filename.jsp” />
| It can be used to include the content of the other resources like the JSP, HTML, or servlet in the current JSP page.
| <jsp:include page=”header.jsp” />
|
---|
Forward Tag
| <jsp:forward page=”destination.jsp” />
| This tag can be used to forward the current request to another resource like the JSP, HTML, or servlet without the client’s knowledge.
| <jsp:forward page=”success.jsp” />
|
---|
setProperty Tag
| <jsp:setProperty name=”beanName” property=”propertyName” value=”propertyValue” />
| This tag can be used to set the properties of the JavaBean component of the JSP pages.
| <jsp:setProperty name=”user” property=”username” value=”John” />
|
---|
getProperty
| <jsp:getProperty name=”beanName” property=”propertyName” />
| This tag can be used to retrieve the properties of the JavaBean component of the JSP pages.
| <jsp:getProperty name=”user” property=”username” />
|
---|
useBean Tag
| <jsp:useBean id=”beanId” class=”packageName.className” />
| This tag can be used to instantiate the JavaBean component or retrieve the existing instance of the JSP pages.
| <jsp:useBean id=”user” class=”com.example.User” />
|
---|
plugin Tag
| <jsp:plugin type=”pluginType” code=”pluginCode” />
| It can be used to generates the HTML code for the browser specific plugin.
| <jsp:plugin type=”applet” code=”MyApplet.class” />
|
---|
attribute tag
| <jsp:attribute name=”attributeName” value=”attributeValue” />
| This tag can be used to defines the attributes values for the custom actions of the JSP pages.
| <jsp:attribute name=”color” value=”blue” />
|
---|
Body Tag
| <jsp:body> <!– Body Content –> </jsp:body>
| This tag can be used to defines the body content for the custom actions of the JSP pages.
| <jsp:body> <p>This is the body content.</p> </jsp:body>
|
---|
Example Project of The Action Tags
We can develop the simple JSP that can demonstrate all actions tags of the JSP project.
index.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index Page</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Welcome to JSP Action Tags Example</h1>
<ul>
<li><a href="include.jsp">Include Action</a></li>
<li><a href="forward.jsp">Forward Action</a></li>
<li><a href="setProperty.jsp">setProperty Action</a></li>
<li><a href="getProperty.jsp">getProperty Action</a></li>
<li><a href="useBean.jsp">useBean Action</a></li>
<li><a href="plugin.jsp">plugin Action</a></li>
<li><a href="attribute.jsp">attribute Action</a></li>
<li><a href="body.jsp">body Action</a></li>
</ul>
</body>
</html>
include.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Include Action</title>
</head>
<body>
<jsp:include page="header.jsp" />
<p>This is the content of the include.jsp page.</p>
</body>
</html>
attribute.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Define custom tag with attributes --%>
<customTag:tagName>
<jsp:attribute name="attributeName">attributeValue</jsp:attribute>
</customTag:tagName>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Attribute</title>
</head>
<body>
<h1>Display Attribute</h1>
<%-- Display attribute value --%>
<p>Attribute Value: <c:out value="${attributeName}" /></p>
</body>
</html>
body.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:body>
<p>This is the body content.</p>
</jsp:body>
destination.jsp
HTML
<%@ 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>
<p>This is the forwarded page content.</p>
</body>
</html>
forward.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:forward page="destination.jsp" />
getProperty.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:forward page="destination.jsp" />
plugin.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:plugin type="applet" code="MyApplet.class" />
setProperty.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="example.User" />
<jsp:setProperty name="user" property="username" value="John" />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Set and Display Property</title>
</head>
<body>
<h1>Set and Display Property</h1>
<%
// Get the value of the username property and print it
String username = user.getUsername();
%>
<p>Username: <%= username %></p>
</body>
</html>
useBean.jsp
HTML
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="example.User" />
Output Screens:
Index.jsp
Include Action:
Forward Action:
getProperty Action:
setProperty Action:
Attribute Action:
body Action:
Conclusion
JSP action tags are the powerful tools for the dynamic server side content generation. It can allows the developers to the includes the files, forward the requests and manipulate the session attributes easily within the JSP documents. Understanding and utilizing these tags are effectively can greatly enhance the functionality and interactivity of the JSP based web applications.
Share your thoughts in the comments
Please Login to comment...