yeny314
yeny314

Reputation: 41

JSP tags vs expression language

JSP tag below

<jsp:useBean id=”employee” class=”employee”/>
<jsp:getProperty name=”employee” property=”name”/>

Another used EL

{employee.name}

Assuming the JSPs compiled properly, how would the 2 pages differ if you had a student object properly populated in the session prior to accessing the page and another when the student object is null.

Can someone explain this to me clearer?

Upvotes: 0

Views: 1013

Answers (1)

diogo
diogo

Reputation: 3977

JSP Tags:

The official Sun documentation says:

The <jsp:useBean> element locates or instantiates a JavaBeans component.

It first attempts to locate an instance of the bean. If the bean does not exist, <jsp:useBean> instantiates it from a class or serialized template.

To locate or instantiate the bean, <jsp:useBean> takes the following steps, in this order:

  1. Attempts to locate a bean with the scope and name you specify.
  2. Defines an object reference variable with the name you specify.
  3. If it finds the bean, stores a reference to it in the variable. If you specified type, gives the bean that type.
  4. If it does not find the bean, instantiates it from the class you specify, storing a reference to it in the new variable. If the class name represents a serialized template, the bean is instantiated by java.beans.Beans.instantiate.
  5. If has instantiated (rather than located) the bean, and if it has body tags or elements (between and ), executes the body tags.

The tag will scan in all scopes, in order of page, request, session and application:

<jsp:useBean id="beanInstanceName" scope="page|request|session|application" ... >

EL:

It only finds (never creates) attributes in all the same scopes (and same piority order) we have for the beans. Be sure to use the right EL keywords to access them.


Getting back to your question, for both strategies, if you have the student object set in the scope, both will get it properly as well as handle its attributes.

Otherwise, useBean tag would create a new object and store it on the specified scope, while EL would simply print nothing, once it knows when the object is null and doesn't try to access its properties.

Upvotes: 2

Related Questions