Monday 23 September 2013

jsp:useBean

 <jsp:useBean>

One of the most important and most widely used jsp action is <jsp:useBean>

  • Lets see what is this jsp:useBen? 




  • Lets say that you have a web application and as soon as the user logs in the request post to the user servlet or the user servlet whatever, and we know that Bean is nothing but a set of getters and setter methods(getId(),setId(),getName(),setName(..)) which identifies the state of an object. Lets say that this is a user bean( picture of person) which has id and name stored in this bean  so I have getters and setters for user id and user name.
  • So whenever a user logs into a system, this servlet invokes a user Bean and it sets the values of the user id and user name to whatever the user has enter in login screen.
  • Now when the request is passed to a jsp page


  • Ultimately I want to pass this user name and id to this jsp page so that it can be displayed on the jsp page, How can I do it? How this jsp page use this Bean values ? which is set at the servlet
  • We know that we have different scopes : request, sessions, page, applicaiton . We can set attributes values to that scope as well.
  • So one thing I can do is, "request.setattribute( "id") whatever id,  and pass those request attributes  whatever r1, r2 to the jsp page and display the those values using "request.get attributes" 
  • So it is a small applicaiton, just have to get two values from my jsp pages , i can use request attribute do something but later I have 10 values, to be passed to jsp page from servlet to jsp, am I use ten request attributes statements? to get 10 different attributes? No..right. So what Im going to do is Im going to create set all the values in the Bean(in setters methods) and Im going to pass that Bean to the jsp page.
  • So Im going to say, request.setAttribute("userBean", userBean) and set this bean value in my user bean attribute, and Im going to pass that bean to my jsp page
  • I can now fetch values of the id and name from the user bean in my jsp page.
  • In my servlet I can say request.setAttribute("userBean", userBean)
  • on the jsp page, how do I display that user Bean , how can I the value of id and name, Here I can use the <jsp:useBean../> action to get the values of the user bean object

  • Ok, lets see the jsp:useBean action, I just explained that when you have a bean in your servlet and you set some properties to that bean in your servlet and you want to save properties value, and you want to write on jsp page you can do that using the <jsp:useBean> action but it actually the jsp:useBean action not used to retrieve values of the bean class.
  • Ok, lets see the jsp:useBean action, I just explained that when you have a bean in your servlet and you set some properties to that bean in your servlet and you want to save properties value, and you want to write on jsp page you can do that using the <jsp:useBean> action but it actually the jsp:useBean action not used to retrieve values of the bean class.

Defn: 
Instantiate the bean class, use bean class properties , and set property values

Syntax:
<jsp:useBean id="{beanInstanceName}"
      scope="page/request/session/application"
      class="{package.class}"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>

  • In fact we have jsp methods actions for that what purpus of the jsp useBean action is instantiate the bean class, we know that when your coding in java when you want to values of a particular object, first you need to create a reference of that object, Lets say i have to use bean class:

  • Here frist Im create ub reference variable for my userBean class and i can use the methods of userbean class, saying ub.getId(), or ub.getName()
  • In a similar manner thre has to be certain reference which you need to create in your jsp as well so if you want to use the userBean object with the values which had set, we need to have certain reference so that you can call methods that userBean and retrieve the values.
  • The following syntax creates the reference and instance 
UserBean ub=new UserBean();
<jsp:useBean id="{beanInstanceName}"
      scope="page/request/session/application"
      class="{package.class}"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>
  • So in the previous example we said with attribute with name userBean (ex: request.setAttribute("userBean", userBean)) and we set the userbean object to that, so whatever attribute values we have here, we need to provide the same name in id for the user bean action element (ie. useBean id="userBean")
<jsp:useBean id="userBean"
      scope="request"
      class="ub.UserBean"
     type="{package.class}"
     beanName=""{package.beanName}">
</jsp:useBean>


  • In this example(request.setAttribute("userBean", userBean) ) we set the useBean attribute at the request scope so scope would be "request" and id would be "userBean" and class is nothing but a object class for example if "bean" is a object of UserBean, then class will become "UserBean" class, and what does type mean nothing but reference type , so you can have different type of class , you can have a different class name in the class, and you can have different class name like type as well because you know that you can create instance of a sub class lets say employee is a sub class and Person is a super class because Employee is a Person , here Employee is a subclass and Person is a super class , basically what you can do is ? when you say employee emp, person emp, Person p=new Employee(); you can do like this right, here Employee is nothing but a class where as type is nothing but Person. (i.e: class="Employee" type="Person" )  so you can have a differen class and you can have different object type(ex: Person p=new Employee();). and you can have different reference type as well. So here class is nothing but object type and the type is nothing but reference type.

Next is beanName:


  • When you put this syntax in jsp:useBean syntax in jsp page quickly tries to find bean with user bean name( in this example user bean name is : "UserBean")  but again it had not found any attribute set this attribute name in the given "scope "  its simply creates a new bean that name for you.
  • Now we going to see how to instantiate a bean in jsp page but how do you really use the values of the properties of those bean in your jsp page we can do that using the <jsp:getProperty> action (http://improve-jsp.blogspot.in/2013/09/implicit-objects-jspgetproperty-action.html)

No comments:

Post a Comment