Beans are simply Java classes that are written in a standard format, three simple points as follows,
. A bean class must have a zero-argument(default) constructor
. A bean class should have no public instance variables(fields).
. Persistent values should be accessed through methods called getXxx and setXxxx.
Using Beans:
<jsp:useBean id="beanName" class="package.Class" />
comments: Best practice - The JSP page Should never creates the objects. the servlet, not the JSP page, should create all the data objects. So, to guarantee that the JSP will not create objects, you should use
<jsp:useBean .. type = "package.Class" /> instead.
<jsp:getProperty name="beanName" property="propertyName" />
<jsp:setProperty name="beanName" property="propertyName" value="propertyValue" />
e.g.
<FORM ACTION="SaleEntry.jsp">
Item ID: <INPUT TYPE="TEXT" NAME="itemID"><BR>
Number of Items: <INPUT TYPE="TEXT" NAME="numItems"><BR>
Discount Code: <INPUT TYPE="TEXT" NAME="discountCode"><P>
<INPUT TYPE="SUBMIT" VALUE="Show Price">
</FORM>
SaleEntry.jsp
<jsp:useBean id="entry" class="coreservlets.SaleEntry" />
<jsp:setProperty
name="entry"
property="itemID"
param="itemID" />
<jsp:setProperty
name="entry"
property="numItems"
param="numItems" />
<jsp:setProperty
name="entry"
property="discountCode"
param="discountCode" />
or <jsp:setProperty name="entry" property="*" />
Sharing Beans,
<jsp:useBean ... scope="session" />
. A bean class must have a zero-argument(default) constructor
. A bean class should have no public instance variables(fields).
. Persistent values should be accessed through methods called getXxx and setXxxx.
Using Beans:
<jsp:useBean id="beanName" class="package.Class" />
comments: Best practice - The JSP page Should never creates the objects. the servlet, not the JSP page, should create all the data objects. So, to guarantee that the JSP will not create objects, you should use
<jsp:useBean .. type = "package.Class" /> instead.
<jsp:getProperty name="beanName" property="propertyName" />
<jsp:setProperty name="beanName" property="propertyName" value="propertyValue" />
e.g.
<FORM ACTION="SaleEntry.jsp">
Item ID: <INPUT TYPE="TEXT" NAME="itemID"><BR>
Number of Items: <INPUT TYPE="TEXT" NAME="numItems"><BR>
Discount Code: <INPUT TYPE="TEXT" NAME="discountCode"><P>
<INPUT TYPE="SUBMIT" VALUE="Show Price">
</FORM>
SaleEntry.jsp
<jsp:useBean id="entry" class="coreservlets.SaleEntry" />
<jsp:setProperty
name="entry"
property="itemID"
param="itemID" />
<jsp:setProperty
name="entry"
property="numItems"
param="numItems" />
<jsp:setProperty
name="entry"
property="discountCode"
param="discountCode" />
or <jsp:setProperty name="entry" property="*" />
Sharing Beans,
<jsp:useBean ... scope="session" />
Comments
Post a Comment