I have servlet with following code:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Person p = new Person("Mike");
req.setAttribute("person", p);
RequestDispatcher view = req.getRequestDispatcher("/result.jsp");
view.forward(req, resp);
}
I have two choices for printing person's name inside result.jsp. Using <jsp:getProperty>
or Expression language
.
Simple EL code:
<!DOCTYPE html>
<html><body>
Welcome ${person.name}
</body></html>
Or using jsp:getProperty like this:
<!DOCTYPE html>
<html><body>
<jsp:useBean id="person" type="com.example.Person" class="com.example.Person" scope="request"/>
Welcome <jsp:getProperty name="person" property="name"/>
</body></html>
From my understanding, both these codes for getting name either by ${person.name}
or <jsp:getProperty name="person" property="name"/>
call findAttribute()
. But there is one major difference. Code written in EL doesn't need <jsp:useBean>
, while <jsp:getProperty>
works only combined with <jsp:useBean>
.
We might ask how does ${person.name}
know what type of object is "person". Well, it uses something like this. My question is how can't <jsp:getProperty>
work the same way as EL as stated in given link? Why can't it call getClass(),getMethod() and "spare" us struggle of typing class,type,id
inside <jsp:useBean>
? Is it simply because EL is newer, thus providing us with less code needed to type, or there is something else hiding behind this that I am not seeing?