File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Using lists in a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Using lists in a jsp" Watch "Using lists in a jsp" New topic
Author

Using lists in a jsp

Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
I'd like to print a list in my jsp. For example this is the code:

<%@ page language="java" import="java.util.*,java.lang.*" %>
<% List<String> l1 = new List();
l1.add("Hi!");
l1.add(" How");
l1.add(" are");
l1.add(" you?");
ListIterator<String> iter = l1.iterator(); %>
<html>
<title>Test</title>
<body>
<%
while(iter.hasNext()) { %>
<%= iter %>
<% iter.next();
} %>
</body>
</html>

And when I try to run it... the following error appears :

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /index.jsp
Generated servlet error:
Syntax error on token "<", invalid AssignmentOperator

An error occurred at line: 3 in the jsp file: /index.jsp
Generated servlet error:
Syntax error on token "=", != expected

An error occurred at line: 3 in the jsp file: /index.jsp
Generated servlet error:
Syntax error on token "<", invalid AssignmentOperator

An error occurred at line: 3 in the jsp file: /index.jsp
Generated servlet error:
Syntax error on token "=", != expected

Can someone help me please? Did I make a mistake or what should I do?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Is your container perhaps not JDK 1.5 compatible?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
yes I think it is. The java version is 1.5.0_06
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

How do you know?
tebrik ahmet
Greenhorn

Joined: Apr 20, 2006
Posts: 1
your code is almost completely wrong
you cant use an iterator in this way

after controlling whether list has more elements or not at the while loop, you must first get the first element with it.next(); method and then you can print the content
just like that in short: <%=it.next();%>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

Once you've acertained that your container is JDK 1.5 aware, you'll need to check your syntax carefully. As tebrik pointed out, it has lots of issues. Since the "<" character is so prevalent, it's hard to tell exactly what it's complaining about. This is just one of the many reasons not to embed Java scriptlets in your JSP pages and to factor Java code out into beans or servlet controllers.
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
You mean like this?

<% while(iter.hasNext()) { %>
<%= iter.next() %>
<% } %>

it doesn't works too?
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
To find the jdk version, I've made:
java -version
I think that I've installed only one jdk. Is there another mean to know that more precisely?
[ April 20, 2006: Message edited by: sebastien cazaux ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
What server are you using? Tomcat 5.5?
By default Tomcat5.5 uses the Eclipse JDT compiler to compile JSPs.
That compiler is only good up to JDK1.4

http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html
See the section on "configuration" - particularly the last couple of paragraphs.

next thing- If you are going to use the Java 1.5 syntax for types, you may as well use their improved for statement as well - you don't even need an Iterator that way.



However the even better solution in my view would be to use a JSTL forEach loop with your list.



No scriptlet code required for the loop at all.

Cheers,
evnafets
[ April 20, 2006: Message edited by: Stefan Evans ]
Pierre Peron
Greenhorn

Joined: Apr 20, 2006
Posts: 11
I just solved the problem! My server was Tomcat5.5 using Eclipse JDT compiler by default. I had to set the jdk 1.5

Thanks to everybody for your help
anurag priya
Ranch Hand

Joined: Dec 19, 2000
Posts: 72
can you please tell where did you change the compilation option .
thanks in advance,
Anurag.


Anurag priya
SCJP 2(83%); Brain Bench Certified for: Masters level EJB2.0(97%) - Masters level J2EE (83%) - BEA-WebLogic Application Server8.1 (86% )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
You could make this page a lot more compact and pleasant to read and debug (leave out the directive attributes with suitable default settings and use scriptless pages where possible):In a production application, you'd probably want to take the list creation code and put it in a servlet. So overall you'd only have:Much better!


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using lists in a jsp