getProperty() in JSP

Discussions

Web tier: servlets, JSP, Web frameworks: getProperty() in JSP

  1. getProperty() in JSP (4 messages)

    Hi Guys! I am new to JSP. I need a clarification.

          What is the pourpose of using the name attribute in the <jsp:getProperty> tag? Though the name of the bean has been set in the begining of the JSP file in the <jsp:useBean name="DemoBean1" class="DemoBean">, what is the pourpose of using the <jsp:getProperty name="DemoBean1" property="UserName"> tag?

          Will there be any major differnce if I do not specify the name attribute in the <jsp:getProperty > tag?

    Threaded Messages (4)

  2. getProperty() in JSP[ Go to top ]

    The "name" attribute stands for the name of an object (usually an instance of a bean) as declared in a <jsp:useBean> element.
    <jsp:getProperty name="DemoBean1" property="UserName"> instructs the container to get the property "UserName" of the bean "DemoBean1". If you dont specify the name, how will the container know which bean's property you want to retrieve...?

    Also, you *must* create or locate a Bean instance with <jsp:useBean> before you can use <jsp:getProperty> for that bean.
  3. getProperty() in JSP[ Go to top ]

    <jsp:getProperty name="demoBean1" property="userName" /> is functionally equivalent to <%= demoBean1.getUserName() %>. It prints the value of the user name property in the JSP.

    The main advantage of the <jsp:getProperty> tag is that it is a tag, rather than a Java scriptlet, and therefore easier for non-Java programmers to use. A common JSP development strategy (ofter called Model-2 or Model-View-Controller) is to seperate the work done by Java developers and page developers. Java developers produce Servlets/JavaBeans and page developers are HTML/JavaScript specialistics that do not know any Java. The page developers therefore use the JSP tag syntax to interact with JavaBean data instead of scriptlets.

    In practice, <jsp:useBean> and <jsp:getProperty> are not enough to completely satisfy this development strategy. Most Java web develop organizations use more sophisticated frameworks with custom tags to fill in the missing pieces (like iterations). Struts and JSTL are two popular frameworks.
  4. getProperty() in JSP[ Go to top ]

    If you define two or more beans objects in one jsp page, how will container identifies from which object it has to get method to done.
  5. getProperty() in JSP[ Go to top ]

    When you use the <jsp:useBean> tag, you specify the "id" of the bean you are using as an attribute. This "id" acts like a variable name holding the bean:

    <jsp:useBean id="demoBean1" class="com.domain.DemoBean" scope="session">

    This id must match the name attribute in your <jsp:getProperty> tag and any scripting variables that reference the bean:

    <jsp:getProperty name="demoBean1" property="size">
    <%= demoBean1.getSize() %>

    If you want multiple beans, simply give them different ids.

    Finally, the id name must also match the name used to store the bean in the appropriate scope. Therefore, you should be careful to make sure that all your request, session and application names are valid Java identifies (e.g. alphanumeric beginning with a letter):

    session.setAttribute("demoBean1", demoBean1);