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

12 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
  5. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    Digital Marketing Training in Chennai

    Aws Training in Chennai

    Selenium Training in Chennai

    ReplyDelete
  6. I simply want to give you a huge thumbs up for the great info you have got here on this post.
    AWS Training in chennai

    AWS Training in bangalore

    ReplyDelete
  7. Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  8. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    Data science training in tambaram
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore
    Data Science training in marathahalli
    Data Science training in btm

    ReplyDelete
  9. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete