Action Tags in JSP


Working with JSP action Tags:

Action tags are a set of some basic tags, such as inserting other page resources, forwarding the request to another page, creating and locating the JavaBeans instances, and setting and retrieving bean properties, in JSP pages. Instead of using Java code, the programmer uses special JSP action tags to either link to a Java Bean set its properties, or get its properties.

These are the most commonly used action tags are :

  1. include
  2. forward
  3. param
  4. useBean
  5. setProperty
  6. getProperty

In this article I described the first three action tags, the next three action tags will be described in the next article.

Describing the Include Tag: The include action tag allows a static or dynamic resource such as HTML or JSP pages, specified by a URL, to be included in the current JSP while processing a request.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. The following code snippet shows the syntax of the include action :

<jsp: include attributes>

<!--zero or more jsp: param tags-->

</jsp: include>

Difference Between Include Directive and Include action:

Include Directive: The 'include' directive tag inserts the given page and includes content in the generated Servlet page during the translation phase of the JSP life cycle. It is generally used to include files, such as HTML, JSP, XML .txt file, into a JSP page statically. The snippet code use for it is:

<%@ include file= "index.jsp" %>

Include Action: The 'include' action tag is used to include the response generated by executing the specified JSP page or Servlet. Unlike the include directive tag, the include action tag accepts expressions. we can  decide the page to be included at runtime, whereas it is not possible with the include directive tag. The snippet code use for it is:

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

Describing the Forward Tag: The jsp:forward tag forwards the request to another resource. The resource can be dynamic or static.  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. The following code snippet shows the syntax of the forward action:

<jsp:forward attributes>

<!--zero or more jsp: param tags-->

</jsp:forward>

Like:<jsp:forward page= "Header.html"/>

Describing the Param Tag: The jsp:param tag allows us to a name and value pair as parameter to a dynamic resource, while including and forwarding the jsp page to another  jsp page. we can use more than one param tag if we want to more than one parameter. The following code snippet shows the syntax of the param action :

<jsp:param attributes />

Example: In this example i use the functionality of the discussed action tags:

index.jsp:

<Html>
<body bgcolor="skyblue">
<pre>
<form action="front.jsp">
<b>Number 1:</b><input type="text" name="f1"/>
<b>Number 2:</b><input type="text" name="f2"/><br><br>
<input type="submit" name="submit" value="Add"/><input type="submit" name="submit" value="Sub"/>
</form>
</pre>
</body>
</Html>

Front.jsp:

<%@page errorPage="/index.jsp" %>
<%
String s1=request.getParameter("f1");
String s2=request.getParameter("f2");
Integer.parseInt(s1);
Integer.parseInt(s2);
String submit=request.getParameter("submit");
if(submit.equals("Add")){
%>
<jsp:forward page="/Add.jsp"/>
<%
} else if(submit.equals("Sub")){
%>
<jsp:forward page="/Sub.jsp"/>
<%
} else{
%>
<jsp:forward page="/index.jsp"/>
<%}%>
 

Add.jsp:
<%
int a1=Integer.parseInt(request.getParameter("f1"));
int a2=Integer.parseInt(request.getParameter("f2"));
int result=a1+a2;
%>
<jsp:forward page="/Result.jsp">
<jsp:param name="result" value="<%=result%>"/>
</jsp:forward>

Sub.jsp:
<%
int a1=Integer.parseInt(request.getParameter("f1"));
int a2=Integer.parseInt(request.getParameter("f2"));
int result=a1-a2;
%>
<jsp:forward page="/Result.jsp">
<jsp:param name="result" value="<%=result%>"/>
</jsp:forward>
 

Result.jsp:

<%
String result=request.getParameter("result");
String submit=request.getParameter("submit");
%>
<html>
<body bgcolor="cyan">
<center>
<%
if(submit.equals("Add")){
%>
Result of Addition:<%=result%>
<%
}else{
%>
Result of Subtraction:<%=result%>
<%}%>
</center>
<jsp:include page="/index.jsp"/>
</body>
</html>

Output:

index.jsp:

index..gif

Result.jsp:

Addition

result1.gif

Subtraction

result2.gif

Up Next
    Ebook Download
    View all
    Learn
    View all