hey guys,

I'm realtively new to jsp, and I want to create java objects and use them later in the jsp code.
Here's what I do:

<%! 
public static java.util.List attrTypesArray = new java.util.ArrayList(50); 
public static boolean myBool;
attrTypesArray.add("aaaa"); 
pageContext.setAttribute("attrTypesArray", attrTypesArray);
pageContext.setAttribute("myBool", myBool);
myBool = attrTypesArray.contains("aaaa"); 
%>
<c:out value='${myBool}'/>

However, I get errors of the sort:

Unable to compile class for JSP: 
An error occurred at line: 65 in the jsp file: /file.jsp
Syntax error on token(s), misplaced construct(s)

<%! %> tags are used for declarations. I recommend you don't use this for anything other than declaring your variables, and this is probably the cause of your issues.

Just use normal scriptlets <% %> for your actual statements.

Good luck

hey, you are right abut the declaration tags. However the problem seems to be different - I cannot mix jsp and jstl tags (particularly, I cannot use the myBool variable in the c:out tag)

Therefore I still need your help. I have to go through the elements of a collection (preferably using jstl <c:forEach>) but for each element I want to check whether I have already passed through another one with the same attribute_1. If yes, then I print it's attribute_2, if not - I print both attributes.
So, obviously I need to keep track whether another element with the same attribute_1 has been already checked in the "forEach" loop. Hence, I think I should use a container like ArrayList or something similar to check whether myArrayList.contains(myAttribute_1).

Is there a way to keep track of these attributes in jstl, or I should use pure jsp ?!?
... or can I create the ArrayList in jsp and then somehow use it within the jstl <c:forEach> tag?

maybe my problem description is a littel confusing - sorry - just ask if you don't understand something.

:)
Pesho

Well, don't mix them. Eliminate the scriptlets altogether. You should be doing this stuff in Beans, and the like.

ok :)
but what if in the JSP tags I want to use info passed by the servlet?

For instance, in my servlet I have the following:

Animal myAnimal = new Animal();
// Set attributes
request.setAttribute("myAnimal", myAnimal);
getServletContext().getRequestDispatcher("/myJSP.jsp").forward(request, response);

Now in the jstl tags of myJSP.jsp I can use the following:

<c:forEach items='${myAnimal.body.legs}' var="leg">
   ......
</c:forEach>

BUT I cannot use the following:

<% for(int i=0; i<myAnimal.getBody().getLegs().length; i++) { %>
   ......
<% } %>

... it complains that myAnimal cannot be resolved

Could you please explain why I cannot use "myAnimal" in the plain JSP tags???


Thans a lot ! ! !

This article has been dead for over six months. Start a new discussion instead.