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