Question

I am in the process of trying to understand JSP and was wondering which of the variants below would be preferred to output nested data in a JSP page.

Which one of these variations (if any...) would you prefer to encounter if you had to maintain a web app? Why? Or is there another, superior way of achieving the same goal?

This is about changes in a legacy application, I am restricted to JSP 2.1. Please don't suggest switching to an entirely different technology :-)

1:

<%=mainBean.getMemberOne().getSomeAttribute() %>

2:

${mainBean.memberOne.someAttribute}

3:

<bean:write name="mainBean" property="memberOne.someAttribute" />

Remark: For all variations we have this somewhere:

<jsp:useBean id="mainBean" scope="session" class="com.acme.MainBean" />
Was it helpful?

Solution

Don't use scriptlets because it will make your code harder to maintain and less readable. Also, you would not be required to seperate view and program logic from each other, so this is the worst one:

<%=mainBean.getMemberOne().getSomeAttribute() %>

The other ones, I would say, are a matter of taste. I prefer ${mainBean.memberOne.someAttribute}, because it is short and easy to read. However, be careful using this method because of Cross Site Scripting attacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top