Basics & Types
- The purpose of JSP actions is to specify activities to be performed when a page is processed (As opposed to JSP directives which are processed during translation)
- Include action
- Forward action
- useBean action
- getProperty action
- setProperty action
- plugIn action
- Text
- Element
- Attribute
- Body
- Fallback
- Param
- Params
Syntax
JSP tags do not use <%, but just the < character.
Predefined tags start with jsp: characters. For instance, jsp:include is a predefined tag that is used to include other pages.
<jsp:action-name attribute-list>
<jsp:subaction-name subaction-attribute-list />
</jsp:action-name>
Include
Include action used for including resources in a JSP page
- Include directive includes resources in a JSP page at translation time
- Include action includes response of a resource into the response of the JSP page
- Changes in the included resource reflected while accessing the page.
- Normally used for including dynamic resources
Example
- <jsp:include page=“inlcudedPage.jsp”>
- Includes the the output of includedPage.jsp into the page where this is included.
Forward
- Forwards the response to other web specification resources
- Forwarded only when content is not committed to other web application resources
- Otherwise an IllegalStateException is thrown
- Can be avoided by setting a high buffer size for the forwarding jsp page
Example
- <jsp:forward page=“Forwarded.html”>
- Forwards the request to Forwarded.html
Param
- Used in conjunction with Include & Forward actions to include additional request parameters to the included or forwarded resource
- The <jsp:param> is used to provide information in the form of key/value pair.
Example
<jsp:forward page=“Param2.jsp”>
<jsp:param name=“FirstName” value=“Sanjay”>
</jsp:forward>
- This will result in the forwarded resource having an additional parameter FirstName with a value of Sanjay
useBean
Creates or finds a Java object with the defined scope.
- Object is also available in the current JSP as a scripting variable
- The bean class must be kept in "<WebContextRoot>\WEB-INF\classes" within a proper package directory structure.
Syntax:
<jsp:useBean id=“name”
scope=“page | request | session | application”
class=“className” type=“typeName” |
bean=“beanName” type=“typeName” |
type=“typeName” />
At least one of the type and class attributes must be present
Example
<jsp:useBean id=“myName” scope=“request” class=“java.lang.String”>
<% firstName=“Sanjay”; %>
</jsp:useBean>
get/setProperty
getProperty is used in conjunction with useBean to get property values of the bean defined by the useBean action
Example (getProperty)
<jsp:getProperty name=“myBean” property=“firstName” />
setProperty is used to set bean properties
Example (setProperty)
<jsp:setProperty name=“myBean” property=“firstName” value=“Sanjay”/>
plugIn
The <jsp:plugin> action is used to generate browser-dependent HTML code (OBJECT or EMBED) that displays and executes a Java Plugin software (Java applet or a JavaBean component) in the current JSP page. The <jsp:params> and <jsp:fallback> actions are optional sub elements.
Example
<jsp: plugin type=“applet” code=“MyApplet.class” codebase=“/”>
<jsp:params>
<jsp:param name=“myParam” value=“122”/>
</jsp:params>
<jsp:fallback><b>Unable to load applet</b></jsp:fallback>
</jsp:plugin>
text
The <jsp:text> action can be used to write template text in JSP pages and documents. Following is the simple syntax for this action:
<jsp:text>Template data</jsp:text>
The body fo the template cannot contain other elements; it can only contain text and EL expressions ( Note: EL expressions are explained in subsequent chapter).
The <jsp:element> Action
The <jsp:attribute> Action
The <jsp:body> Action
The <jsp:element>, <jsp:attribute> and <jsp:body> actions are used to define XML elements dynamically. The word dynamically is important, because it means that the XML elements can be generated at request time rather than statically at compile time.