jsp and classpath

Y

Yamin

Hi all,

I'm just started off learning a bit of jsp and jservlets. I got the basic
setup to run. But I'm having a slight problem here.

The SETUP:

Apache 2.0 / Tomcat 5.0
-testing and working...using samples
***************************************
The jsp page = test.jsp
G:\www/Tomcat 5.0\webapps\test
test.jsp
G:\www\Tomcat 5.0\webapps\test\WEB-INF
web.xml
G:\www\Tomcat 5.0\webapps\test\WEB-INF/classes
crazyClass.java, crazyClass.class, crazyClass.jar
G:\www\Tomcat 5.0\webapps\test\WEB-INF\lib
catalina-root.jar
***************************************
test.jsp
************************************
<HTML>
<BODY>
<H1><%= "Test!" %></h1><%= "at " +
java.util.Calendar.getInstance().getTime() %>
<H2><%= " Line test" %></h2><%= "at " + crazyClass.getValue() %>
</BODY>
</HTML>
*************************************

crazyClass.java (compiles fine)
**********************************
import java.io.*;
import java.text.*;
import java.util.*;

public class crazyClass
{
public static int x = 0;

public static String getValue()
{
x++;
return Integer.toString(x);
}
}
*************************************

THE PROBLEM:
within test.jsp, if I don't include the line: <H2><%= " Line test"
%></h2><%= "at " + crazyClass.getValue() %> everything works fine and I get
the date printed out. Once I include the line, I get a tomcat error saying
cannot resolve symbol crazyClass

I guess this means it can't find crazyClass.class. As far as my
understanding goes, by putting crazyClass.class in the WEB-INF/classes
directory, it should be found by the compiler when it compiles test.jsp? Or
is that not right and I have to explicity change the java classpath each
time I have a new project to add to tomcat?

If its the classpath problem, anyone know what the norm is to solve it?
Change the CLASSPATH environment var? Add a classpath option to the tomcat
JAVA VM configuration?

Thanks for any help,

Yamin
 
D

Dmytro Sheyko

Yamin said:
Hi all,

I'm just started off learning a bit of jsp and jservlets. I got the basic
setup to run. But I'm having a slight problem here.

The SETUP:
***************************************
G:\www\Tomcat 5.0\webapps\test\WEB-INF/classes
crazyClass.java, crazyClass.class, crazyClass.jar
G:\www\Tomcat 5.0\webapps\test\WEB-INF\lib
catalina-root.jar
***************************************

Move crazyClass.jar from WEB-INF/classes to WEB-INF/lib

Web Server adds jars and zips from WEB-INF/lib into classpath of web
application. But not from WEB-INF/classes
 
Y

Yamin

Dmytro Sheyko said:
Move crazyClass.jar from WEB-INF/classes to WEB-INF/lib

Web Server adds jars and zips from WEB-INF/lib into classpath of web
application. But not from WEB-INF/classes

Hey Dmytro,

I tried that, and I get the same error message
*********************************************************
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 4 in the jsp file: /test.jsp
Generated servlet error:
[javac] Compiling 1 source file

G:\www\Tomcat
5.0\work\Catalina\localhost\test\org\apache\jsp\test_jsp.java:52: cannot
resolve symbol
symbol : variable crazyClass
location: class org.apache.jsp.test_jsp
out.print( "at " + crazyClass.getValue() );
^
1 error
*********************************************************

Yamin
 
D

Dmytro Sheyko

Move crazyClass.jar from WEB-INF/classes to WEB-INF/lib

Web Server adds jars and zips from WEB-INF/lib into classpath of web
application. But not from WEB-INF/classes

Hey Dmytro,

I tried that, and I get the same error message
*********************************************************
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 4 in the jsp file: /test.jsp
Generated servlet error:
[javac] Compiling 1 source file

G:\www\Tomcat
5.0\work\Catalina\localhost\test\org\apache\jsp\test_jsp.java:52: cannot
resolve symbol
symbol : variable crazyClass
location: class org.apache.jsp.test_jsp
out.print( "at " + crazyClass.getValue() );
^
1 error
*********************************************************

Hello Yamin,

I made a test and got the same error as you pointed.

As far as I understand, the JSP compiler treats crazyClass as a variable but
not as a class. The workaround is to put class into a package.

Consider slightly changed example:

**********************************************************************
WEB-INF\lib
crazyClass.jar
index.jsp
**********************************************************************
<!-- index.jsp -->
<%@page import="test.crazyClass"%>
<HTML>
<BODY>
<H1><%= "Test!" %></h1><%= "at " +
java.util.Calendar.getInstance().getTime() %>
<H2><%= " Line test" %></h2><%= "at " + crazyClass.getValue() %>
</BODY>
</HTML>

**********************************************************************
// test/crazyClass.java
package test;

import java.io.*;
import java.text.*;
import java.util.*;

public class crazyClass
{
public static int x = 0;

public static String getValue()
{
x++;
return Integer.toString(x);
}
}
**********************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top