• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList throws error in JSP page

 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm using Tomcat 5.5.4 with JDK/JRE 1.5.0_04. I have set my environment variables accoring to the directions and have written some other JSP pages that work fine, so I think everything is set up correctly.

Whenever I try to compile and run the following code in a JSP page, I get the error noted below. I am new to Java, but this seems pretty basic to be getting an error... Thanks for your help!

<%
@ page import = "java.util.*";
ArrayList myList = new ArrayList();
%>

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

An error occurred at line: 53 in the jsp file: test.jsp
Generated servlet error:
Syntax error on tokens, Expression expected instead
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%
@ page import = "java.util.*";
ArrayList myList = new ArrayList();
%>


You can't mix directives and scriplets in this manner.

 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem on the name. The only rule I remember seeing was "be nice", so I must have overlooked the stringent naming policy.

Okay, so if I can't mix directives and scriplets in that manner, then what manner can I mix them in? And if no manner at all, how might I accomplish what I'm trying to accomplish?

Thanks,

Ben
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for adjusting your display name so quickly!

The code example at the bottom of my post shows the correct syntax.
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear!

I feel like real jerk. Your post was so subtle that I thought for some reason you were reposting my code (perhaps because I forgot the code tags).

Now I realize what you mean about not putting directives and scriptlets within the same set of <%%>.

As a somewhat unrelated aside, any idea why there cannot be a space between the <% and the @ character in the line:



Is the "@" used for supressing the literal output of exceptions that might be thrown? Like I said, I'm new to Java.

Thanks again,

Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the "@" used for supressing the literal output of exceptions that might be thrown? Like I said, I'm new to Java.
Thanks again,
Ben


The <%@ ... %> is a directive tag, not a scriptlet tag.
This isn't really a Java issue as much as it is a JSP specific issue.

I always recommend learning servlets before JSP.
JSP is an abstraction layer that results in machine generated Servlet code.
If you understand the underlying principals (which are as clear as the keys on a piano in basic servlet code) it's a lot easier to figure out what a JSP is really doing.

One tip: A lot of app servers (such as Tomcat) keep the generated servlet code around so you can see exactly what your JSP is being turned into.
[ July 12, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ben (the other Ben) said, the <%@ sequence is an introducer for a JSP directive. Other introducers are: <%! <%= and <%. If you put a space in there, the JSP translator gets confused.
 
Ben Johnson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh, that makes a lot more sense. I didn't realize that <%!, <%@, etc. were entities entirely separate from the unadorned <%. That is very sound advice, Ben, and I have indeed been focusing my efforts on learning servlets before much else.

Thanks again, to both of you.

Ben
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!