Action tags in JSP are used to perform action on particular page. This Action tags are
1. include
2. forward
3. useBean
Action tags are used to transfer control from one parent page to another particular page. Action tags are also used in server side inclusion for using javaBean property methods directly in JSP page without using core java code.
1. include action tag
Include action tag is almost similar to include directive. This include can be static or dynamic. This include file is first compiled and output of this file is inserted with parent file.
Static include
<jsp:include page="relativeURL" flush="{true|false}" />
Dynamic include
<jsp:include page="relativeURL" flush="{true|false}">
<jsp:param name="parameterName" value="parameterValue" />
</jsp:include>
JSP Action tags include can be static or dynamic. Static include is simple and
Look like as
<jsp:include page="header.jsp" />
Example of static include in Action tags
header.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Header page</title>
<style>
.header
{
background-color:#CBE0E7;
font-weight:bold;
text-align:center
}
</style>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="header">Home</td>
<td> </td>
<td class="header">Products</td>
<td> </td>
<td class="header">Services</td>
<td> </td>
<td class="header">Company Profile</td>
<td> </td>
<td class="header">Register</td>
<td> </td>
<td class="header">Login</td>
<td> </td>
<td class="header">About Us</td>
<td> </td>
</tr>
</table>
</body>
</html>
staticAction.jsp
<%@ page language="java" import="java.io.*" errorPage="" %>
<html>
<head>
<title>Static include in Action tags JSP</title>
</head>
<body>
<jsp:include page="header.jsp" />
</body>
</html>
Example of dynamic include action tag in JSP with param tag
header.jsp
<%@ page language="java" %>
<html>
<head>
<title>Header page</title>
<style>
.header
{
background-color:#CBE0E7;
font-weight:bold;
text-align:center
}
</style>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="header"><%=request.getParameter("variable1")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable2")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable3")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable4")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable5")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable6")%></td>
<td> </td>
<td class="header"><%=request.getParameter("variable7")%></td>
<td> </td>
</tr>
</table>
</body>
</html>
dynamicAction.jsp
<%@ page language="java" errorPage="" %>
<html>
<head>
<title>Dynamic include in Action tags JSP</title>
</head>
<body>
<jsp:include page="header.jsp">
<jsp:param name="variable1" value="Home" />
<jsp:param name="variable2" value="Products" />
<jsp:param name="variable3" value="Services" />
<jsp:param name="variable4" value="Company Profile" />
<jsp:param name="variable5" value="Register" />
<jsp:param name="variable6" value="Login" />
<jsp:param name="variable7" value="About Us" />
</jsp:include>
</body>
</html>
2. Forward action tag
Forward action tag in JSP transfer the control of one JSP page to another JSP with specify URL. Forward also can be static or dynamic as include action. When we use param tags, it is dynamic forward with having values. Static forward is simple forward without having param tags.Forward is used when we need to jump from one page to another if error occur or if work is completed in parent page. E.g we are inserting data in database with using insert statement. After finishing insert, control of page should go to successful insertion of data in database page or failure of insertion go to error page. This can be done forward action tag.
<jsp:forward page="relativeURL | or <%= expression %>" />
e.g
<jsp:forward page="success.jsp" />
This is static forward, dynamic forward
<jsp:forward page="relativeURL">
&l use for Java Beans. First we know little about Java Bean what is Java Bean, Java Bean is simple java class having number of method and can have business logic code inside method of Java Bean. These components can be reusable by different number of JSPs as per requirement. Java bean can have simple getter setter method or complex business logic. Java beans are not more than a plain java class and help to separate a design code and logic code. Java bean need to compile manually as java compiler does.
Simple java bean example java bean should copy in webapps/jsp/WEB-INF/classes/com/myApp of tomcat
Example of java bean with useBean action tag in JSP
FirstBean.java
package com.myApp;
public class FirstBean {
public String sName=null;
public int iClass=0;
public int iMarks=0;
public int iMaxMarks=0;
public String getSName() {
return sName;
}
public void setSName(String name) {
sName = name;
}
public int getIClass() {
return iClass;
}
public void setIClass(int class1) {
iClass = class1;
}
public int getIMarks() {
return iMarks;
}
public void setIMarks(int marks) {
iMarks = marks;
}
public int getIMaxMarks() {
return iMaxMarks;
}
public void setIMaxMarks(int maxMarks) {
iMaxMarks = maxMarks;
}
public double getDPercent() {
double dPer=(double)(iMarks*100)/iMaxMarks;
return dPer;
}
}
useBean.jsp
<%@ page language="java" errorPage="" %>
<jsp:useBean id="myLog" class="com.myApp.FirstBean" scope="page"/>
<%
myLog.setSName("Amit");
myLog.setIClass(10);
myLog.setIMarks(89);
myLog.setIMaxMarks(120);
%>
<html>
<head>
<title>useBean Action tags JSP</title>
</head>
<body>
<%=myLog.getSName() %> Percent is <%=myLog.getDPercent()%>
</body>
</html>
scope of bean can
1. page (attribute will work in same page)
2. application (attribute will work in whole application)
3. session (attribute will work in session)
4. request (attribute will work in request one time and destroy)
This useBean create instance of java bean class. The attribute of bean myLog get object of FirstBean after instantiate.
|