JSP - SPLessons
boy2 Chapter 27

JSP Action Tags

JSP - SPLessons

JSP Action Tags

shape Description

The JSP Action Tags are used to perform a some specific action task. There are many JSP action tags. The actions are used during request processing where as directive tags are used for translation phase. For controlling the behavior of the servlet engine, the JSP action tags are used as constructs in XML syntax.By using this action tags, a file can be inserted dynamically. To reuse JAVABean components, forward the request and response to another page, or create HTML for Java module.

shape Table

Tags Description
Jsp:forward Forwards the request and response to other sources
Jsp:include includes other resources
Jsp:element To define  XML elements at runtime.
Jsp:getproperty Prints the property values of the bean
Jsp:useBean Creates bean object
Jsp:plugin Embeds another resource
Jsp:param Set the parameter values. It is used mostly in forward and include pages.
Jsp:setProperty Sets the property estimations in bean objects.
Jsp:attribute Defines XML elements attributes dynamically.
Jsp:body Defines XML elements body.

JSP: forward

shape Description

The functionality forward action tag is to forward the request and response to another resource like html or JSP.
Syntax:

<jsp:forward page=“relativeURL | <%= expression %>” />

//jsp:forwar tag with parameters

<jsp:forward page=“relativeURL | <%= expression %>”>  <jsp:param name=“parametername” value=“parametervalue | <%=expression%>” />

</jsp:forward>

shape Example

Below is an example to jsp:forward tag.

forwardtag.jsp

<html>  
	<body>
               <h2>this is page,here included another file that is printdate.jsp</h2>

                <jsp:forward page="printdate.jsp">  
                <jsp:param name="name" value="WWW.SPLESSONS.COM" />  
                </jsp:forward>
		  
      </body>  
</html>  

Hete used JSP forward tag for the printdate.jsp .
printdate.jsp

 
<html>  
	<body>  
		  
		<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>  
		<%= request.getParameter("name") %> 
		  
	</body> 
</html>   

request.getParameter() method is used to get the name from forwardtag.jsp.
Output:

While compiling the code forwardtag.jsp file should be in active mode,output will be as follows with current time followed by parameter name.

JSP: include

shape Description

The include action tag is utilized to incorporate the substance of the other asset like JSP, HTML, Servlet.JSP include action tag incorporates the substance at solicitation time so its better for element pages since it is utilized for future changes.
Syntax

<jsp:include page=“relativeURL | <%= expression %>” />

//jsp:include action tag with parameter

<jsp:include page=“relativeURL | <%= expression %>”>

<jsp:param name=“parametername” value=“parametervalue | <%=expression%>” />

</jsp:include>

JSP: useBean

shape Description

The useBean action tag is used to instantiate a bean class. When bean object is not created, it instantiates the bean. If bean object is available already, it doesn’t rely on upon the bean extent of the object.
Syntax

<jsp:useBean id= “instanceName” scope= “page | request | session | application” class“packageName.className” type= “packageName.className” beanName=“packageName.className | <%= expression >” >
</jsp:useBean> 

shape Example

Below is an example to usebean action tag.

 useBean.jsp

<jsp:useBean id="obj" class="com.Question">

           <%
                 String name = obj.message();
                 out.println("Enter a name");
                 (
                 <h2>your name is "+name+"</h2>
                 ");
            %>

</jsp:useBean>

 Question.java

package com;

      public class Question 
       {
                public String message(){
		
		String name = "john";
		
		return name;
	}
}

Output:

Below is an output with name that was provided in .java file.

JSP: setProperty

shape Description

The jsp: setProperty activity tag is utilized to set property estimations in a bean utilizing setter method. The setProperty uses java bean for the development of web application.
Syntax

<jsp:setProperty name=“instanceOfBean” property= “*” |   

property=“propertyName” param=“parameterName”  |  

 property=“propertyName” value=“{ string | <%= expression %>}”   

/> 


JSP: getProperty

shape Description

The jsp: getProperty action tag is utilized to get property values in a bean using getter method. This getProperty is used for web application with java bean.
Syntax

<jsp:getProperty name=“instanceOfBean” property=“propertyName” />  

shape Example

Below is an example to set and get properties.

setgetproperty.html


<form action="process1.jsp" method="post">  
			Name:<input type="text" name="name">
  
			Password:<input type="password" name="password">
  
			Email:<input type="text" name="email">
  
			<input type="submit" value="register"> 
</form>

Here just created three text boxes for Name, Password, Email and submit button.
process1.jsp

<jsp:useBean id="u" class="User"></jsp:useBean>  
<jsp:setProperty property="*" name="u"/> 
  
Record:
  
<jsp:getProperty property="name" name="u"/>
  
<jsp:getProperty property="password" name="u"/>
  
<jsp:getProperty property="email" name="u" />
 

Here get property on the name, password, email.
User.java

public class User {  
		private String name,password,email;  
		//setters and getters  
}  

Output

Output will be as follows with three text fields and one register button.

JSP: plugin

shape Description

The plugin action is utilized to insert the applet or bean into a JSP page, This tag is used when there is a need of plugin to run a Bean class or applet.
Syntax

<jsp: plugin type = “bean” code = “nameOfClassFile
codebase = “directoryNameOfClassFile”>

</jsp: plugin>


JSP: element

shape Description

The element tags are utilized to define XML elements at runtime.
Syntax

<jsp:element name=”xmlElement“></jsp:element>

<jsp:attribute name=”xmlAttribute“></jsp:attribute>

JSP:text

shape Description

jsp:text action tag is utilized to write the text template in JSP pages.The body template contains only text and EL expressions.
<jsp: text> Template data </jsp: text>

Summary

shape Key Points

  • The jsp:plugin action tag is used to embed applet in the JSP file.
  • Action tags to control the flow between the pages.
  • jsp:fallback will be used  in jsp:plugin to scan  the message.