• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using lists in a jsp

 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your container perhaps not JDK 1.5 compatible?
 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I think it is. The java version is 1.5.0_06
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know?
 
tebrik ahmet
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like this?

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

it doesn't works too?
 
Pierre Peron
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1692
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell where did you change the compilation option .
thanks in advance,
Anurag.
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic