Actions in JSP
IV. Actions
JSP Home
JSP actions use the construct in XML syntax to control the behavior of the servlet engine.
The JSP specification provided a standard tag called Action tag, that can be used within JSP code and allows us to eliminate Scriptlet code from the JSP code.
Some actions tags given below.You will learn about each of them with examples in this tutorial.
<jsp:useBean>
<jsp:setProperty>
<jsp:getProperty>
<jsp:param>
<jsp:include>
<jsp:plugin>
These actions perform some specific tasks and are predefined. They provide functionalities like
- Dynamic insertion of a file in jsp
- Controlling behavior of the servlet engine
- Forwarding a user to another page
- Controlling logic flow between pages
How to use bean in JSP
A bean is a Java class Object.
Declaring a Bean in JSP
Syntax:
<jsp:useBean id="bean id" scope="page" class="Class Name" type="Bean Classname" %>
Exmple:
<jsp:useBean id="mybean" scope="page" class="Myclass" type="Myclass" %>
Reading property value
Syntax:
<jsp:getProperty name="objname" perperty="propname" />
Exmple:
Setting property value
Syntax:
<jsp:setProperty name="objname" perperty="property name" value="property value" />
Exmple:
<jsp:setProperty name="objname" perperty="name" value="abc" />
useBeanDemo.jsp
<jsp:useBean id="mybean" scope="page" class="Myclass" type="Myclass" %> <% =mybean.setName("Hello from Jsp"); %> <% =mybean.getName() %> <jsp:setProperty name="objname" perperty="name" value="abc" />
Myclass.java (javabean)
public class Myclass{
String name=null;
public void setName(String n)
{
name=n;
}
public String getName()
{ return name;
}
}
greet.java
public class greeter {
public greeter() { }
public String greetme(String s)
{
return "we welcome..."+s;
}
}
greet.jsp
<html>
<body>
<jsp:useBean id='bean1' class='greeter'>
<%
String s = request.getParameter ("text1");
String r = bean1.greeteme(s);
out.println(r);
%>
</body>
</html>
greet.html
<html>
<body>
<form method="post" action="greeter.jsp">
<input type="text" name="text1">
<input type="submit" name="sub">
</form>
</body>
</html>
Difference between include directive and <jsp:include> action
include: It is static, it is included at compile time we can pass parameter with query string.
<jsp:include>: Dynamic, it is dispatching.
pageAction.jsp
<%@ page contentType="text/html" pageEncoding="UTF-8"%> <jsp:include page="Pagedemo.jsp" /> <% out.println("<br><h1>Inside page</h1>"); %>
Using custom tag library and custom iterator.
IteratorTag.java
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class IteratorTag extends BodyTagSupport{
private int counts = 0;
private BodyContent bodyContent;
public void setBodyContent(BodyContent bodyContent) {
this.bodyContent = bodyContent;
}
public void setCounts(int counts) {
this.counts = counts;
}
public int doStartTag() throws JspException {
if (counts >0) {
return EVAL_BODY_TAG;
} else {
return SKIP_BODY;
}
}
public int doEndTag() throws JspException {
try {
if(bodyContent != null) {
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
} catch(IOException e) {}
return EVAL_PAGE;
}
public int doAfterBody() throws JspException {
if (counts >1) {
counts--;
return EVAL_BODY_TAG;
} else {
return SKIP_BODY;
}
}
}
taglib.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems,Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>custom</shortname>
<tag>
<name>iterator</name>
<tagclass>IteratorTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>counts</name>
<required>true</required>
</attribute>
<info>IteratorTag</info>
</tag>
</taglib>
CustomIterator.jsp
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="custom" %>
<html>
<head>
<title>Custom Iterator Tag</title>
</head>
<body bgcolor="#0033cc">
<font color="white">
<H1>Welcome! to Custom Iterator </H1>
Custom Iterator tag starts...
<br><hr>
<custom:iterator counts="3">
<br>3 times it would print this line
</custom:iterator>
<br><hr>
Rest Body Content of JSP page
</font>
</body>
</html>
To run this program follow these steps:
1. Create and save IteratorTag.java
2. Compile and put IteratorTag.class into classes folder of your web application folder.
3. Create taglib.tld file and place it into WEB-INF folder.
4. Create and save CustomIterator.jsp
5. Start Tomcat web server and type following URL in your browser's address bar:
http://localhost:8080/Example/CustomIterator.jsp
Comments
Post a Comment