Java.Servlet.What do you know about JSP actions (Action tag and JSP Action Elements).JSP – servlet – JSP interaction.

JSP Actions (also called JSP Action Tags or JSP Action Elements) are special XML-like tags that perform predefined tasks when a JSP page is processed.

They are part of the JSP specification itself (not user-defined or third-party) and are executed on the server-side during page request handling.

🎯 Purpose of JSP Actions:

  • Encapsulate functionality (like including other resources, forwarding requests, working with JavaBeans) without writing Java code manually.
  • Simplify interaction with backend logic.
  • Promote reuse and cleaner JSP code.
  • Help JSP remain more declarative (tag-based) rather than imperative (Java code blocks).

🔥 Syntax of Action Tags

<jsp:actionName attribute="value" />

They look like XML elements: start with <jsp:, followed by the action name.

They must be properly closed (either self-closing or with explicit end tag).

🔵 Common JSP Action Elements

Action TagPurpose
<jsp:useBean>Locate or instantiate a JavaBean
<jsp:setProperty>Set a property value on a bean
<jsp:getProperty>Get a property value from a bean
<jsp:include>Dynamically include another resource
<jsp:forward>Forward request to another resource
<jsp:param>Add parameters to include or forward
<jsp:plugin>Download and run a Java plugin (like Applet)

🔥 Short Examples for Each:

1. <jsp:useBean>

Create or find a JavaBean instance.

<jsp:useBean id="user" class="com.example.User" scope="session" />
  • If no user exists in session, create it.

2. <jsp:setProperty>

Set property values inside a JavaBean.

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

Sets user.setName("Stanley");

You can also populate properties automatically from request parameters:

<jsp:setProperty name="user" property="*" />

➔ Sets all matching properties.

3. <jsp:getProperty>

Retrieve property values from a bean.

<p>Welcome, <jsp:getProperty name="user" property="name" />!</p>

Output: Welcome, Stanley!

4. <jsp:include>

Include another file at request time (dynamic inclusion).

<jsp:include page="footer.jsp" />
  • Runs footer.jsp and inserts its output here.

You can also pass parameters:

<jsp:include page="header.jsp">
    <jsp:param name="title" value="Home Page" />
</jsp:include>

5. <jsp:forward>

Forward request internally to another resource.

<jsp:forward page="login.jsp" />
  • Stops further processing and sends request to login.jsp.

You can also pass parameters similarly with <jsp:param>.

6. <jsp:plugin>

Embed Java applets or JavaBeans dynamically.

<jsp:plugin type="applet" code="MyApplet.class" width="300" height="300">
    <jsp:params>
        <jsp:param name="color" value="blue" />
    </jsp:params>
</jsp:plugin>

(Not very common anymore — applets are obsolete today.)

📌 Important points about Action Tags:

  • They are server-side only: the browser never sees them.
  • They provide a declarative, tag-based alternative to embedding Java code (<% %>).
  • Some actions, like <jsp:include>, happen at runtime (not at translation time).

🛠️ Why are Action Tags Important?

  • Cleaner and more readable JSPs (instead of mixing lots of Java code).
  • Help integrate JavaBeans with the UI layer easily.
  • Make JSPs easier to maintain and extend.
  • Support separation of concerns: JSPs handle presentation; JavaBeans handle business data.

🎯 Final Recap:

AspectDescription
What are they?Built-in JSP tags for predefined tasks
Look like?XML-style, <jsp:actionName>
Why use them?Cleaner, more modular, server-side dynamic behavior
This entry was posted in Без рубрики. Bookmark the permalink.