Tuesday, 5 February 2013

STANDARD ACTION AND JAVA BEAN | J2EE Tutorial pdf

STANDARD ACTION AND JAVA BEAN

JSP standard actions are predefined custom tags that can be used to encapsulate common actions easily. There are two types of JSP standard actions: the first type is related to JavaBeans functionality, and the second type consists of all other standard actions.
Three predefined standard actions relate to using JavaBeans in a JSP: <useBean>, <setProperty>, and <getProperty>.
All We need to know about beans are three simple points:
1. A bean class must have a zero-argument (empty) constructor.
We can satisfy this requirement either by explicitly defining such a constructor or by omitting all constructors, which results in an empty constructor being created automatically. The empty constructor will be called when JSP elements create beans.
2. A bean class should have no public instance variables (fields).
3. Persistent values should be accessed through methods called getXxx and setXxx.
For example, if our Car class stores the current number of passengers, we might have methods named getNumPassengers (which takes no arguments and returns an int) and setNumPassengers (which takes an int and has a void return type).
First we will define these tags, we will create a simple example that uses them.
<jsp:useBean>
The <jsp:useBean> JavaBean standard action creates or looks up an instance of a JavaBean with a given ID and scope. The syntax of the <jsp:useBean> action is as follows:
<jsp:useBean id="name" class=”className” scope="page|request|session|application" >
body
</jsp:useBean>

id : The key associated with the instance of the object in the specified scope. This key is case-sensitive. The id attribute is the same key as used in the page.getAttribute() method.
scope : The life of the referenced object. The scope options are page, request, session, and application.
class : The fully qualified class name that defines the implementation of the object. The class name is case-sensitive.
<jsp:setProperty>
The <jsp:setProperty> standard action sets the value of a bean’s property. Its name attribute represents an object that must already be defined and in scope.
The syntax for the
<jsp:setProperty> action is as follows:
<jsp:setProperty name="beanName" propexpr />

 In the preceding syntax, the name attribute represents the name of the bean whose property we are setting, and propexpr can be represented by any of the following expressions:
property="*" |
property="propertyName" |
property="propertyName" param="parameterName" |
property="propertyName" value="propertyValue"
 

name : The name of the bean instance defined by a <jsp:useBean> action or some other action.
property : The bean property for which we want to set a value. If we set propertyName to an asterisk (*), then the action will iterate over the current ServletRequest parameters, matching parameter names and
value types to property names and setter method types, and setting each matched property to the value of the matching parameter. If a parameter has an empty string for a value, the corresponding property is left
unmodified.
param : The name of the request parameter whose value we want to set the named property to. A <jsp:setProperty> action cannot have both param and value attributes referenced in the same action.
value : The value assigned to the named bean’s property.
<jsp:getProperty>
The last standard action that relates to integrating JavaBeans into JSPs is <jsp:getProperty>. It takes the value of the referenced bean’s instance property, converts it to a java.lang.String, and places it on the output stream. The referenced bean instance must be defined and in scope before this action can be used. The syntax for the <jsp:getProperty> action is as follows:
<jsp:getProperty name="name" property="propertyName" />
name : The name of the bean instance from which the property is obtained, defined by a <jsp:useBean> action or some other action.
property : The bean property for which we want to get a value.
A JavaBean Standard Action Example
To learn how to use the JavaBean standard actions, let’s create an example. This example uses a simple JavaBean that acts as a counter. The Counter bean has a single int property, count, that holds the current number of times the bean’s property has been accessed. It also contains the appropriate methods for getting and setting this property.
package chapter;
public class Counter {
int count = 0;
public Counter() {
}
public int getCount() {
count++;
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Integrate this sample JavaBean into a JSP, using the JavaBean standard actions.
<!-- Set the scripting language to java -->
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>Bean Example</TITLE>
</HEAD>
<BODY>
<!-- Instantiate the Counter bean with an id of "counter" -->
<jsp:useBean id="counter" scope="session" class="chapter.Counter" />
<%
// write the current value of the property count
out.println("Count from scriptlet code : " + counter.getCount() + "<BR>");
%>
<!-- Get the the bean’s count property, -->
<!-- using the jsp:getProperty action. -->
Count from jsp:getProperty :
<jsp:getProperty name="counter" property="count" /><BR>
</BODY>
</HTML>
Compile the Counter class, move it to the /WEB-INF/classes/chapter/ directory, and copy the Counter.jsp file to the root directory and make request:--
<jsp:param>
The <jsp:param> action provides parameters and values to the JSP standard actions <jsp:include>, <jsp:forward>, and <jsp:plugin>. The syntax of the <jsp:param> action is as follows:
<jsp:param name="name" value="value"/>
<jsp:include>
The <jsp:include> standard action provides a method for including additional static and dynamic Web components in a JSP. The syntax for this action is as follows:
<jsp:include page="urlSpec" flush="true">
<jsp:param ... />
</jsp:include>
Note It is important to note the difference between the include directive and the include standard action. The directive is evaluated only once, at translation time, whereas the standard action is evaluated with every request.
<jsp:forward>
The <jsp:forward> standard action enables the JSP engine to execute a runtime dispatch of the current request to another resource existing in the current Web application, including static resources, servlets, or JSPs. The appearance of <jsp:forward> effectively terminates the execution of the current JSP.
Note A <jsp:forward> action can contain <jsp:param> subattributes. These subattributes act as parameters that will be forwarded to the targeted resource.
The syntax of the <jsp:forward> action is as follows:
<jsp:forward page="relativeURL">
<jsp:param .../>
</jsp:forward>
<jsp:plugin>
The last standard action we will discuss is <jsp:plugin>. This action enables a JSP author to generate the required HTML, using the appropriate client-browser independent constructs, to result in the download and subsequent execution of the specified applet or JavaBeans component. The <jsp:plugin> tag, once evaluated, will be replaced by either an <object> or <embed> tag, as appropriate for the requesting user agent. The attributes of the <jsp:plugin> action provide configuration data for the presentation of the embedded element. The syntax of
the <jsp:plugin> action is as follows:
<jsp:plugin type="pluginType" code="classFile" codebase="relativeURLpath">
<jsp:params>
</jsp:params>
</jsp:plugin>
type : The type of plug-in to include (an applet, for example)
code : The name of the class that will be executed by the plug-in
codebase : The base or relative path where the code attribute can be found
Examples:-- Using plugin tag
<%@ page language="java" %>
<jsp:plugin type="applet" code="myapplet" codebase="." width="300"
height="300" >
</jsp:plugin>
myapplet.java
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet {
public void paint(Graphics g) {
g.drawString("HELLO",100,100);
}
}

1 comment:

Interview questions answers pdf said...

Java Beans Interview Questions and Answers
http://allinterviewquestionsandanswerspdf.blogspot.in/2016/06/top-57-java-beans-interview-questions.html