JSP Actions

JSP actions are special XML tags that perform specific tasks, such as creating or accessing Java objects, including other resources, or forwarding the request to another resource. They provide a way to dynamically insert content into a JSP page and interact with JavaBeans components. In this blog post, we will explore the various JSP actions, their syntax, and their usage with examples.

Types of JSP Actions

  1. <jsp:useBean>
  2. <jsp:setProperty>
  3. <jsp:getProperty>
  4. <jsp:include>
  5. <jsp:forward>
  6. <jsp:param>
  7. <jsp:plugin>

1. <jsp:useBean>

The <jsp:useBean> action is used to locate or instantiate a JavaBean. It creates an instance of the bean or retrieves an existing one if it already exists in the specified scope.

Syntax

<jsp:useBean id="beanInstanceName" class="package.ClassName" scope="page|request|session|application" />

Example

<jsp:useBean id="user" class="net.javaguides.User" scope="session" />
<jsp:setProperty name="user" property="name" value="John Doe" />

2. <jsp:setProperty>

The <jsp:setProperty> action is used to set the property of a JavaBean.

Syntax

<jsp:setProperty name="beanInstanceName" property="propertyName" value="value" />

Example

<jsp:setProperty name="user" property="age" value="30" />

3. <jsp:getProperty>

The <jsp:getProperty> action is used to retrieve the property of a JavaBean and convert it to a string representation that can be displayed in the JSP page.

Syntax

<jsp:getProperty name="beanInstanceName" property="propertyName" />

Example

<p>Name: <jsp:getProperty name="user" property="name" /></p>
<p>Age: <jsp:getProperty name="user" property="age" /></p>

4. <jsp:include>

The <jsp:include> action is used to include the content of another resource (such as a JSP, HTML, or servlet) in the current JSP page. This inclusion happens at request time.

Syntax

<jsp:include page="relativeURL" />

Example

<jsp:include page="header.jsp" />

5. <jsp:forward>

The <jsp:forward> action is used to forward the request to another resource (such as a JSP, HTML, or servlet). The control is transferred to the forwarded resource.

Syntax

<jsp:forward page="relativeURL" />

Example

<jsp:forward page="login.jsp" />

6. <jsp:param>

The <jsp:param> action is used to add parameters to the request. It is typically used within the <jsp:include> and <jsp:forward> actions to pass parameters to the included or forwarded resource.

Syntax

<jsp:param name="paramName" value="paramValue" />

Example

<jsp:forward page="welcome.jsp">
    <jsp:param name="username" value="John" />
</jsp:forward>

7. <jsp:plugin>

The <jsp:plugin> action is used to embed a Java applet or a JavaBean in the JSP page. It generates the appropriate HTML tags to include the plugin.

Syntax

<jsp:plugin type="bean|applet" code="className" codebase="url" />

Example

<jsp:plugin type="applet" code="com.example.AppletClass" width="300" height="300">
    <jsp:param name="param1" value="value1" />
    <jsp:param name="param2" value="value2" />
</jsp:plugin>

Complete Example of <jsp:useBean>, <jsp:setProperty>, and <jsp:getProperty>

Let's create a bean class Student with four variables: firstName, lastName, emailId, and password. We will use these variables in a JSP page.

Student.java

package net.javaguides.jsp.tutorial;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    private String firstName;
    private String lastName;
    private String emailId;
    private String password;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

student.jsp

Create a student registration form:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>JSP Actions Example</title>
</head>
<body>

    <h1> Student Registration Page</h1>
    <form action="studentdetails.jsp" method="post">
        First Name: <input type="text" name="firstName">
        <br><br> 
        Last Name: <input type="text" name="lastName">
        <br><br> 
        Email ID: <input type="email" name="emailId">
        <br><br> 
        Password: <input type="password" name="password">
        <br><br> 
        <input type="submit" value="register">
    </form>

</body>
</html>

studentdetails.jsp

Create studentdetails.jsp to show the student registration details:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Student Details</title>
</head>
<body>

    <jsp:useBean id="student" class="net.javaguides.jsp.tutorial.Student"></jsp:useBean>
    <jsp:setProperty property="*" name="student" />
    <h1>Student Registered with Following Details</h1>
    <b>First Name:</b> <jsp:getProperty property="firstName" name="student" /><br><br>
    <b>Last Name:</b> <jsp:getProperty property="lastName" name="student" /><br><br>
    <b>Email ID:</b> <jsp:getProperty property="emailId" name="student" /><br><br>
    <b>Password:</b> <jsp:getProperty property="password" name="student" />

</body>
</html>

Output

Student Registration Page
Fill details and submit a student registration form:

Conclusion

JSP actions provide a powerful way to interact with JavaBeans, include other resources, and forward requests within JSP pages. By understanding and utilizing these actions effectively, developers can create dynamic, modular, and maintainable web applications.

In the next article, we will learn how to process HTML forms using JSP.

Comments