[go: up one dir, main page]

0% found this document useful (0 votes)
57 views8 pages

JSP Actions for Developers

Enterprise Programming for java web development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views8 pages

JSP Actions for Developers

Enterprise Programming for java web development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JavaServer Pages (JSP) actions

JavaServer Pages (JSP) actions are XML tags that perform specific tasks in a JSP page. They are used to
control the behavior of the servlet engine and to interact with JavaBeans, forward requests, include
other resources, and more.

Here are the key JSP actions and their use case examples:

1. `<jsp:useBean>`

Description: Creates or reuses a JavaBean instance.

Syntax

<jsp:useBean id="beanId" class="beanClass" scope="scope" />

Example
You have a `User` bean with `name` and `email` properties.

User.java:

package com.example;

public class User {

private String name;

private String email;

// Getters and Setters

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; }

index.jsp:

<jsp:useBean id="user" class="com.example.User" scope="session" />

<jsp:setProperty name="user" property="name" value="John Doe" />

<jsp:setProperty name="user" property="email" value="john.doe@example.com" />

<h1>User Info</h1>

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

<p>Email: <jsp:getProperty name="user" property="email" /></p>

2. `<jsp:setProperty>`

Description: Sets a property value in a JavaBean.

Syntax

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

Example
Sets the properties of a `User` bean.
<jsp:useBean id="user" class="com.example.User" scope="session" />

<jsp:setProperty name="user" property="name" value="John Doe" />

<jsp:setProperty name="user" property="email" value="john.doe@example.com" />

3. `<jsp:getProperty>`

Description: Retrieves a property value from a JavaBean.

Syntax

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

Example
Retrieves the `name` and `email` properties of a `User` bean.

<jsp:useBean id="user" class="com.example.User" scope="session" />

<h1>User Info</h1>

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

<p>Email: <jsp:getProperty name="user" property="email" /></p>

4. `<jsp:include>`

Description: Includes a static or dynamic resource in the current JSP page.


Syntax

<jsp:include page="relativeURL" flush="true" />

Example
Includes a header and footer in a JSP page.

header.jsp:

<h1>Header Content</h1>

footer.jsp:

<p>Footer Content</p>

index.jsp:

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

<p>Main Content</p>

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

5. `<jsp:forward>`

Description: Forwards the request to another resource.


Syntax

<jsp:forward page="relativeURL" />

Example
Forwards the request to `welcome.jsp` if a condition is met.

index.jsp:

<%

boolean condition = true; // Example condition

if (condition) {

%>

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

<%

%>

6. `<jsp:param>`

Description: Adds a parameter to a `<jsp:include>` or `<jsp:forward>` action.

Syntax
<jsp:param name="paramName" value="paramValue" />
Example
Passes parameters to an included JSP page.

included.jsp:

<p>Param1: ${param.param1}</p>

<p>Param2: ${param.param2}</p>

index.jsp:

<jsp:include page="included.jsp">

<jsp:param name="param1" value="value1" />

<jsp:param name="param2" value="value2" />

</jsp:include>

7. `<jsp:plugin>`

Description: Embeds a Java applet or a JavaBean in a JSP page.

Syntax

<jsp:plugin type="applet" code="appletClass" width="width" height="height">

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

</jsp:plugin>
Example
Embeds a Java applet.

<jsp:plugin type="applet" code="com.example.MyApplet" width="300" height="200">

<jsp:param name="param1" value="value1" />

</jsp:plugin>

8. `<jsp:element>` and `<jsp:attribute>`

Description: Defines an XML element and its attributes dynamically.

Syntax

<jsp:element name="elementName">

<jsp:attribute name="attributeName">attributeValue</jsp:attribute>

</jsp:element>

Example
Creates a dynamic XML element.

<jsp:element name="customElement">

<jsp:attribute name="attr1">value1</jsp:attribute>

<jsp:attribute name="attr2">value2</jsp:attribute>

</jsp:element>

You might also like