User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 391,184 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,660 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 1272 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 3
Reputation: pesho318i is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pesho318i pesho318i is offline Offline
Newbie Poster

a simple jsp question

  #1  
Nov 12th, 2007
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)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 120
Reputation: sillyboy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
sillyboy sillyboy is offline Offline
Junior Poster

Re: a simple jsp question

  #2  
Nov 13th, 2007
<%! %> 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
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: pesho318i is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pesho318i pesho318i is offline Offline
Newbie Poster

Re: a simple jsp question

  #3  
Nov 14th, 2007
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
Reply With Quote  
Join Date: Feb 2006
Posts: 1,360
Reputation: masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough masijade is a jewel in the rough 
Rep Power: 8
Solved Threads: 117
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: a simple jsp question

  #4  
Nov 14th, 2007
Well, don't mix them. Eliminate the scriptlets altogether. You should be doing this stuff in Beans, and the like.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Nov 2007
Posts: 3
Reputation: pesho318i is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pesho318i pesho318i is offline Offline
Newbie Poster

Re: a simple jsp question

  #5  
Nov 14th, 2007
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 ! ! !
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JSP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 7:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC