 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
|
|
Shri Kanaskar |
|
|
|
A JSP Example using JavaBeans |
|
|
|
This is an example of JavaBean that acts as a counter It has a single init property, count, which that holds the current number of times the beans property has been accessed. It contains get and set methods. Counter.java public class Counter{ int count // Initialize bean on creation public Counter(){ // Parameterless constructor.
}
public int getCount() { //Get Property count++; //count property has been incremented with every request return this.count; }
public void setCount(int count) { //set property this.count = count; }
} |
|
|
|
JSP CODE |
|
|
|
BeanCounter.jsp <HTML> <HEAD> <TITLE>JSP Bean counter example</Title> </HEAD> <BODY> <!-- Set the scriting language to java --> <%@ page language = "java" %> <%@ page import="Counter" %>
<!-- Instantiate teh Bean Counter --> <jsp:useBean id="counter" scope="session" class="Counter" />
<!-- Set the bean;s count property to the request parameter "count --> <jsp:setProperty name="counter" property="count" param="count" />
<% // Display the current value of the property count pot.println("Count from Scriplet code : " + counter.getCount() + "<BR>"); %>
<!-- Get the bean's count property using the getproperty --> Count from jsp:getproperty : <jsp:getproperty nae = "counter" property="count" /> <BR> </BODY> </HTML> |
|
|
|
 |
|
|
|
 |
|