Accessing java bean from JSP using jsp useBean tag

First of all write a java bean called "Address" and for the simplicity add only two fields "city" and "state" generate getters and setters and the java class should look something like this.

public class Address {

 private String city;
 private String state;

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }

 public String getState() {
  return state;
 }

 public void setState(String state) {
  this.state = state;
 }

}

Now, create a jsp page and add the below code snippet inside the body tag.


    <jsp:useBean id="add" class="Address">
        <jsp:setProperty name="add" property="city" value="Newyork" />
        <jsp:setProperty name="add" property="state" value="NY" />
    </jsp:useBean>
    City:
    <jsp:getProperty property="city" name="add" /><br> 
    State:
    <jsp:getProperty property="state" name="add" /><br> 
    <%-- instead of getProperty tag we can use below 
    expression and can get the same results
    City: ${add.city} <br> 
    State: ${add.state } --%>
Output:
Once after you run the jsp on your installed application server, the output should look something like this.
City: Newyork
State: NY

5 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
    Replies
    1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

      or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

      Delete
  2. java bean called "Address" and for the simplicity add only two fields "city" and "state" generate getters and setters and the java class.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  3. thaxxx for given information

    ReplyDelete
  4. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    Best Java Training Institute Chennai

    ReplyDelete