trouble resolving a class with JSP

  • Follow


this may seem like a stupid question, but i cant figure it out...

i have a simple JSP, in which I want to call a static method in another
class, but tomcat says it can't find the other class. The class I want
to call is no within a package, it is in the same directory as the jsp
file.

here is my jsp code:

<%@ page import="java.util.Enumeration"%>
<%@ page import="java.io.File"%>

<html>
<body>
<form action="unzip.jsp" method=post>
	<SELECT	NAME="File"	SIZE=4>
		 <%  File dir = new File("/myDir");

			for(File tempFile : dir.listFiles()) {    %>
		<OPTION><%=tempFile%></OPTION>
		<% } %>

	</SELECT>
	<INPUT TYPE=SUBMIT VALUE=Press>
</form>
<B>Form Content</B><BR>
<TABLE>
<%  Enumeration parameters = request.getParameterNames();
         String parameterName = (String)parameters.nextElement();
         String parameterValue = request.getParameter(parameterName);

         if(parameterValue.endsWith(".zip")) {
            Utils.unzipFile(new File(parameterValue));
		 }
	     %>
</body>
</html>

thanks!

0
Reply anikkar (32) 11/3/2005 6:40:32 PM

Well, what class and method are you getting the error for?

anikkar@gmail.com wrote:
> this may seem like a stupid question, but i cant figure it out...
> 
> i have a simple JSP, in which I want to call a static method in another
> class, but tomcat says it can't find the other class. The class I want
> to call is no within a package, it is in the same directory as the jsp
> file.
> 
> here is my jsp code:
> 
> <%@ page import="java.util.Enumeration"%>
> <%@ page import="java.io.File"%>
> 
> <html>
> <body>
> <form action="unzip.jsp" method=post>
> 	<SELECT	NAME="File"	SIZE=4>
> 		 <%  File dir = new File("/myDir");
> 
> 			for(File tempFile : dir.listFiles()) {    %>
> 		<OPTION><%=tempFile%></OPTION>
> 		<% } %>
> 
> 	</SELECT>
> 	<INPUT TYPE=SUBMIT VALUE=Press>
> </form>
> <B>Form Content</B><BR>
> <TABLE>
> <%  Enumeration parameters = request.getParameterNames();
>          String parameterName = (String)parameters.nextElement();
>          String parameterValue = request.getParameter(parameterName);
> 
>          if(parameterValue.endsWith(".zip")) {
>             Utils.unzipFile(new File(parameterValue));
> 		 }
> 	     %>
> </body>
> </html>
> 
> thanks!
> 
0
Reply giganews1 (35) 11/3/2005 6:53:49 PM


anikkar@gmail.com wrote:

> this may seem like a stupid question, but i cant figure it out...
>
> i have a simple JSP, in which I want to call a static method in another
> class, but tomcat says it can't find the other class. The class I want
> to call is no within a package, it is in the same directory as the jsp
> file.

[ something i deleted ]


The class file generated by javac should be present in the
TomcatDir/webapps/myapp/WEB-INF/classes folder.

Also, if you dont want to do that, and your jsp, static class .java and
..class files are in the same folder, then check if you have "."
(current dir) in your classpath.

It will work, but it is a bad idea after all. Avoid classes with
default package. You can try this later, but do it. default package
creates lot of confusion when you have large number of files. (I once
had trial.class in 3 places, and i was confused as to why java is not
executing what I am telling it to).

Put your static class in a package. The class file should be present in
the classes folder, with the correct package hierarchy.

eg. if you have
package com.myname.test;
in your java file,
you should have the generated class file in
classes/com/myname/test folder hierarchy. Dont worry about classpath,
when tomcat runs your app, it sets the classpath.

Post your error if problem not resolved.

0
Reply Halcyon.Wild (147) 11/3/2005 7:56:44 PM

Hi HalcyonWild,

thanks for the reply. I agree with you about packaging classes, it is
much better practice. I am just running a few test applications to try
and get a feel for how JSPs work.

So from what you are saying, my class files should be in the WEB-INF
directory? i'm guessing this goes for any jar files i wish to reference
as well.

back to my problem, I am getting the following exception from tomcat:

exception

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

An error occurred at line: 18 in the jsp file: /unzip.jsp
Generated servlet error:
/usr/src/jakarta-tomcat-5.0.27/work/hosting/test.lambandtunafish.com/_/org/apache/jsp/unzip_jsp.java:71:
cannot find symbol
symbol  : variable Utils
location: class org.apache.jsp.unzip_jsp
            Utils.unzipFile(new File(parameterValue));
            ^
1 error

Here is the JSP file:

<%@ page import="java.util.Enumeration"%>
<%@ page import="java.io.File"%>
<html>
<body>
<form action="unzip.jsp" method=post>
	<SELECT	NAME="File"	SIZE=4>
		 <%  File dir = new File("/home/content/b/u/d/budweiser/html/test");

			for(File tempFile : dir.listFiles()) {    %>
		<OPTION><%=tempFile%></OPTION>
		<% } %>

	</SELECT>
	<INPUT TYPE=SUBMIT VALUE=Press>
</form>
<B>Form Content</B><BR>
<%  Enumeration parameters = request.getParameterNames();
         String parameterValue = request.getParameter("File");

         if(parameterValue.endsWith(".zip")) {
            Utils.unzipFile(new File(parameterValue));
		 }
	     %>
</body>
</html>

Here is my Util class:

import java.io.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;

public class Utils {

	/* this method can introduce errors when user's have restricted access
to File System */
    public static void copy(InputStream in, OutputStream out) throws
IOException {
        // synchronize to prevent other threads from accessing either
stream while
        // they are being read
        synchronized (in) {
            synchronized (out) {
                byte[] buffer = new byte[256];
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
            }
        }
    }

	/**
     * Creates the directory structure in which the file resides
     * @param file
     * @throws IOException
     */

    public static void createTreeForFile(File file) throws IOException
{
        File parent;
        if ((parent = file.getParentFile()) != null) {
            if (parent.exists()) {
                return;
            }
            createTreeForFile(parent);
        }
        parent.mkdir();
    }

    public static void unzipFile(File zip) throws IOException {
        FileInputStream fin = new FileInputStream(zip);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;

        while ((ze = zin.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());

            createTreeForFile(new File(ze.getName()));
            FileOutputStream fout = new FileOutputStream(ze.getName());
            copy(zin, fout);
            zin.closeEntry();
            fout.close();
        }

        zin.close();
    }
}

0
Reply anikkar (32) 11/3/2005 10:12:23 PM

Hi HalcyonWild,

thanks for the reply. I agree with you about packaging classes, it is
much better practice. I am just running a few test applications to try
and get a feel for how JSPs work.

So from what you are saying, my class files should be in the WEB-INF
directory? i'm guessing this goes for any jar files i wish to reference
as well.

back to my problem, I am getting the following exception from tomcat:

exception

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

An error occurred at line: 18 in the jsp file: /unzip.jsp
Generated servlet error:
/usr/src/jakarta-tomcat-5.0.27/work/hosting/test.lambandtunafish.com/_/org/apache/jsp/unzip_jsp.java:71:
cannot find symbol
symbol  : variable Utils
location: class org.apache.jsp.unzip_jsp
            Utils.unzipFile(new File(parameterValue));
            ^
1 error

Here is the JSP file:

<%@ page import="java.util.Enumeration"%>
<%@ page import="java.io.File"%>
<html>
<body>
<form action="unzip.jsp" method=post>
	<SELECT	NAME="File"	SIZE=4>
		 <%  File dir = new File("/home/content/b/u/d/budweiser/html/test");

			for(File tempFile : dir.listFiles()) {    %>
		<OPTION><%=tempFile%></OPTION>
		<% } %>

	</SELECT>
	<INPUT TYPE=SUBMIT VALUE=Press>
</form>
<B>Form Content</B><BR>
<%  Enumeration parameters = request.getParameterNames();
         String parameterValue = request.getParameter("File");

         if(parameterValue.endsWith(".zip")) {
            Utils.unzipFile(new File(parameterValue));
		 }
	     %>
</body>
</html>

Here is my Util class:

import java.io.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;

public class Utils {

	/* this method can introduce errors when user's have restricted access
to File System */
    public static void copy(InputStream in, OutputStream out) throws
IOException {
        // synchronize to prevent other threads from accessing either
stream while
        // they are being read
        synchronized (in) {
            synchronized (out) {
                byte[] buffer = new byte[256];
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
            }
        }
    }

	/**
     * Creates the directory structure in which the file resides
     * @param file
     * @throws IOException
     */

    public static void createTreeForFile(File file) throws IOException
{
        File parent;
        if ((parent = file.getParentFile()) != null) {
            if (parent.exists()) {
                return;
            }
            createTreeForFile(parent);
        }
        parent.mkdir();
    }

    public static void unzipFile(File zip) throws IOException {
        FileInputStream fin = new FileInputStream(zip);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;

        while ((ze = zin.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());

            createTreeForFile(new File(ze.getName()));
            FileOutputStream fout = new FileOutputStream(ze.getName());
            copy(zin, fout);
            zin.closeEntry();
            fout.close();
        }

        zin.close();
    }
}

0
Reply anikkar (32) 11/3/2005 10:30:47 PM

anikkar@gmail.com wrote:

> Hi HalcyonWild,
>
> thanks for the reply. I agree with you about packaging classes, it is
> much better practice. I am just running a few test applications to try
> and get a feel for how JSPs work.
>
> So from what you are saying, my class files should be in the WEB-INF
> directory? i'm guessing this goes for any jar files i wish to reference
> as well.

-----------

Well, that is not really important. Your jar files might reside
anywhere on the computer, just make sure they are in the classpath.

Also, you need to understand how tomcat runs. It includes your
webinf/classes folder in the classpath while executing your jsp, and
the TOMCATHOME/common/lib or server/lib folder (I am sorry I might not
be exact with folder name spellings, I dont have tomcat on my computer,
and I dont need to use it these days). So you might also copy your jar
files into common/lib (or server/lib) and it would be fine. Or keep
them just where they are and include the path of the jar in your
classpath.

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

>
> back to my problem, I am getting the following exception from tomcat:
>
> exception
>
> org.apache.jasper.JasperException: Unable to compile class for JSP
>
> An error occurred at line: 18 in the jsp file: /unzip.jsp
> Generated servlet error:
> /usr/src/jakarta-tomcat-5.0.27/work/hosting/test.lambandtunafish.com/_/org/apache/jsp/unzip_jsp.java:71:
> cannot find symbol
> symbol  : variable Utils
> location: class org.apache.jsp.unzip_jsp
>             Utils.unzipFile(new File(parameterValue));
>             ^
> 1 error
-------------

It means that it does not know anything about Utils class. If Utils is
part of your test with JSP, just keep generated class file in
Webinf/classes folder. It should work. See how Tomcat sets classpath
before it runs, in the start.bat or start.sh file. If Utils is a part
of some jar, include jar in classpath of the computer. (export in *nix,
control panel->system->advanced->environment vars in Win2K).

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

0
Reply Halcyon.Wild (147) 11/7/2005 1:01:50 PM

<anikkar@gmail.com> wrote in message
news:1131053844.915126.104840@g14g2000cwa.googlegroups.com...
> Hi HalcyonWild,
>
> thanks for the reply. I agree with you about packaging classes, it is
> much better practice. I am just running a few test applications to try
> and get a feel for how JSPs work.
>
> So from what you are saying, my class files should be in the WEB-INF
> directory? i'm guessing this goes for any jar files i wish to reference
> as well.
>
> back to my problem, I am getting the following exception from tomcat:
>
> exception
>
> org.apache.jasper.JasperException: Unable to compile class for JSP
>
> An error occurred at line: 18 in the jsp file: /unzip.jsp
> Generated servlet error:
>
/usr/src/jakarta-tomcat-5.0.27/work/hosting/test.lambandtunafish.com/_/org/a
pache/jsp/unzip_jsp.java:71:
> cannot find symbol
> symbol  : variable Utils
> location: class org.apache.jsp.unzip_jsp
>             Utils.unzipFile(new File(parameterValue));
>             ^
> 1 error
>
> Here is the JSP file:
>
> <%@ page import="java.util.Enumeration"%>
> <%@ page import="java.io.File"%>

<snip>

You need to import your Utils class to use it, and you'll need to put it
into a package in order to do that.


0
Reply nospam693 (54) 11/8/2005 4:59:43 AM

Tim B wrote:
> <anikkar@gmail.com> wrote in message
> news:1131053844.915126.104840@g14g2000cwa.googlegroups.com...
> > Hi HalcyonWild,
> >
> > thanks for the reply. I agree with you about packaging classes, it is
> > much better practice. I am just running a few test applications to try
> > and get a feel for how JSPs work.
> >
> > So from what you are saying, my class files should be in the WEB-INF
> > directory? i'm guessing this goes for any jar files i wish to reference
> > as well.
> >
> > back to my problem, I am getting the following exception from tomcat:
> >
> > exception
> >
> > org.apache.jasper.JasperException: Unable to compile class for JSP
> >
> > An error occurred at line: 18 in the jsp file: /unzip.jsp
> > Generated servlet error:
> >
> /usr/src/jakarta-tomcat-5.0.27/work/hosting/test.lambandtunafish.com/_/org/a
> pache/jsp/unzip_jsp.java:71:
> > cannot find symbol
> > symbol  : variable Utils
> > location: class org.apache.jsp.unzip_jsp
> >             Utils.unzipFile(new File(parameterValue));
> >             ^
> > 1 error
> >
> > Here is the JSP file:
> >
> > <%@ page import="java.util.Enumeration"%>
> > <%@ page import="java.io.File"%>
>
> <snip>
>
> You need to import your Utils class to use it, and you'll need to put it
> into a package in order to do that.



Oh yes, I missed that. Apart from the classpath and packaging, you need
to import the class that you want to use.

0
Reply Halcyon.Wild (147) 11/8/2005 9:30:25 AM

Thanks for the replies.

I actually got the classpath working correctly, and I have a bit of a
better understanding of how tomcat and JSPs work.

unfortunately, I can't look at the tomcat folders because i don't have
access through my host.

I still cant get the above code to work, but that is because I cant
seem to create a file through a jsp. I have tried the simplest of
examples, and I get access denied...

<%@ page import="java.io.*"%>
<html>
<body>
<%
out.println("Test");
BufferedWriter fp = null;
      String str = new String("blah blah");

     try {
         fp = new BufferedWriter(new
FileWriter("/home/content/b/u/d/budweiser/test/temp2.txt", true));

         fp.write(str, 0, str.length());
         fp.newLine();

         fp.flush();
         fp.close();
out.println("Success");
     }
     catch (IOException e) {
	out.println(e.getMessage());
     }
	     %>
</body>
</html>

I tried contacting the godaddy's customer support (which is pretty
bad), and I generally get the answer that i should make sure the file
permissions are correct (through an ftp application, which still
doesn't work), but that doesn't solve the problem of the jsp creating
the file...

0
Reply anikkar (32) 11/8/2005 7:36:57 PM

anikkar@gmail.com wrote:
[ stuff deleted ]
> unfortunately, I can't look at the tomcat folders because i don't have
> access through my host.


You need access, if you want to include jars. You should be able to
modify environment variables. As a temp measure, if you have access,
you might want to modify the startup.bat file, and append to the
classpath using set CLASSPATH=%CLASSPATH%;c:\myclasses
(Or export classpath=$CLASSPATH;somepath , in case of *nix)


> I still cant get the above code to work, but that is because I cant
> seem to create a file through a jsp. I have tried the simplest of
> examples, and I get access denied...
>

You need read write access to your folder. Why dont you install Tomcat
locally on your pc, and try here. Later you can create a war and deploy
on the remote machine.

> I tried contacting the godaddy's customer support (which is pretty
> bad), and I generally get the answer that i should make sure the file
> permissions are correct (through an ftp application, which still
> doesn't work), but that doesn't solve the problem of the jsp creating
> the file...

tell the admin to give you read write permissions on your folder.

0
Reply Halcyon.Wild (147) 11/9/2005 10:23:34 AM

HalcyonWild wrote:
> tell the admin to give you read write permissions on your folder.

Thats seems to be the source of my problems. The last answer I got it
that godaddy doesn't allow Java file output permissions, which makes
absolutely no sense to me, so i've just been going back and forth with
them.

thanks for your help and suggestions!

0
Reply anikkar (32) 11/9/2005 5:32:22 PM

On 9 Nov 2005 09:32:22 -0800, "Arash Nikkar" <anikkar@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>Thats seems to be the source of my problems. The last answer I got it
>that godaddy doesn't allow Java file output permissions, which makes
>absolutely no sense to me, so i've just been going back and forth with
>them.

If you need a more Java-friendly ISP, see
http://mindprod.com/jgloss/ispvendors.html
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
0
Reply my_email_is_posted_on_my_website (4731) 11/10/2005 1:36:22 AM

11 Replies
21 Views

(page loaded in 0.273 seconds)

Similiar Articles:


















7/5/2012 12:15:03 PM


Reply: