java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips arrow JavaServer Pages arrow Accessing bean components from JSP
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Accessing bean components from JSP E-mail
User Rating: / 50
PoorBest 

Example below shows a JSP-page that inserts values into a bean object and then displays the values again. One of the striking things about this JSP is that it does a fairly good bit of work but doesn't contain any explicit Java code.

When you add a bean to a JSP, you can either create a new bean or use an existing one. The JSP engine determines whether it needs to create a new bean for you based on the bean's id. While adding a bean to a page, you must at least give the bean an id (which is just a name) and the bean's class. The JSP engine first searches for an existing bean with the same id. If it doesn't find an existing bean, the JSP engine creates a new instance of the class you specified.

////////////// here is a source code of our bean:

package beans;

////////////// Our bean is serializable. This will allow the server to save
////////////// it as a part of JSP context during restart or 
////////////// recover operations

public class Person implements java.io.Serializable 
  protected String firstName;
  protected String lastName;
  protected int age;

  public Person() {
  }

  public String getFirstName() {
    return firstName; 
  }
  public void setFirstName(String aFirstName) {
    firstName = aFirstName; 
  }

  public String getLastName() {
    return lastName; 
  }

  public void setLastName(String aLastName) {
    lastName = aLastName; 
  }

  public int getAge() { 
    return age; 
  }
  public void setAge(int anAge) {
    age = anAge;
  }
}

////////////// here is a page accessing a bean:

<%@page contentType="text/html"%>

<%-- Create an instance of the bean --%>
<jsp:useBean id="man" class="beans.Person"/>

<html>
  <body>

  <%-- Copy the parameters into the bean --%>
  <jsp:setProperty name="man" property="*"/>

    The bean values are:<br>
    First Name: <jsp:getProperty name="man" property="firstName"/><br />
    Last Name: <jsp:getProperty name="man" property="lastName"/><br />
    Age: <jsp:getProperty name="man" property="age"/><br />

  </body>
</html>


 Related Tips

 
Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.