Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

JSP Actions

What are JSP Actions?

JSP actions are XML-based tags that control the behavior of the Servlet engine. Unlike directives (which affect translation), actions affect the runtime behavior of the JSP. They use the jsp: prefix and follow XML syntax.

Action TagDescription
<jsp:include>Dynamically includes another resource at request time
<jsp:forward>Forwards the request to another resource
<jsp:useBean>Instantiates or locates a JavaBean
<jsp:setProperty>Sets a property on a JavaBean
<jsp:getProperty>Gets a property from a JavaBean
<jsp:param>Passes parameters to included/forwarded pages
jsp:include and jsp:forward
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>

<h2>jsp:include - Dynamic Include</h2>
<%-- Includes header.jsp at request time (dynamic) --%>
<jsp:include page="header.jsp">
    <jsp:param name="title" value="My Page Title"/>
    <jsp:param name="color" value="blue"/>
</jsp:include>

<p>Main page content here.</p>

<%-- Include a dynamic page --%>
<jsp:include page="sidebar.jsp" flush="true"/>

<%-- jsp:forward - Forwards request to another page --%>
<%
    String role = (String) session.getAttribute("role");
    if (role == null) {
%>
    <jsp:forward page="login.jsp">
        <jsp:param name="message" value="Please login first"/>
    </jsp:forward>
<%
    }
%>

</body></html>

jsp:useBean, setProperty, getProperty

These three actions work together to integrate JavaBeans into JSP pages. A JavaBean is a reusable Java class with private fields and public getter/setter methods.

JavaBean and JSP Actions
package com.example.beans;

// JavaBean: must have no-arg constructor and getter/setters
public class UserBean {
    private String name;
    private String email;
    private int age;

    // No-argument constructor (required for JavaBean)
    public UserBean() {}

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

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public String getInfo() {
        return name + " (" + age + ") - " + email;
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>

<%-- Create/locate a UserBean in session scope --%>
<jsp:useBean id="user" class="com.example.beans.UserBean" scope="session"/>

<%-- Set properties from request parameters automatically --%>
<jsp:setProperty name="user" property="*"/>

<%-- Or set individual properties --%>
<jsp:setProperty name="user" property="name" value="Alice"/>
<jsp:setProperty name="user" property="email" param="userEmail"/>
<jsp:setProperty name="user" property="age" value="25"/>

<h2>User Information</h2>
<p>Name: <jsp:getProperty name="user" property="name"/></p>
<p>Email: <jsp:getProperty name="user" property="email"/></p>
<p>Age: <jsp:getProperty name="user" property="age"/></p>
<p>Info: <%= user.getInfo() %></p>

</body></html>

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.