when you normally run the program, it will look for your text file in the
current directory. When you run it from Tomcat, the current directory is
usually TOMCAT_HOME or CATALINA_HOME or possibly your documents folder. You
need to located the file for the class by resolving its location on the
harddrive.
btw, is your file called error.data or error.txt? You first call it one
thing, then another in your message
chanoch
----- Original Message -----
From: "Edward" <zhangsc@n...>
To: "Java 2 Enterprise Edition" <j2ee@p...>
Sent: Saturday, May 18, 2002 3:02 AM
Subject: [j2ee] Why my class can't work properly in the servlet?
> Hello.
> I have a question,it is very puzzle to me.I have a class named
PreReadError,it works well,it can read data from the file "error.data".For
example:
>
> ...
> public static void main()
> {
> String str;
> PreReadError test=new PreReadError();
> //notice here
> str=(String )test.getTable("Error0");
> System.out.println("Found Error0");
> System.out.println("It's value is "+str);
> ...
> }
>
> It works well.When I run it,it will show:
> Found Error0
> It's value is 3
>
> But when I use this class "PreReadError" in a servlet,for example:
>
> ...
> public class GetUserIdentity extends HttpServlet
> {
> public void doPost(HttpServletRequest request,HttpServletResponse
response)
> throws ServletException,IOException
> {
> PreReadError test=new PreReadError();
> PrintWriter out=response.getWriter();
> out.println("<html>");
> out.println("<body>");
> //notice here
> String str=(String )test.getTable("Error0");
> out.println("<p> It's value is "+str+"</p>");
> ....
> }
>
> When I run GetUserIdentity,it should show:
> It's value is 3
> But I got another result,it shows:
> It's value is null
>
> I put my files in follows:
> I use Apache Tomcat4.0 again,I install Apache Tomcat4.0 into D:\Program
Files\Apache Tomcat4.0
> I put my class GetUserIdentity.class and PreReadError.class into follows
folds:
> I put them into D:\Program Files\Apache
Tomcat4.0\Webapps\examples\WEB-INF\classes
> then I also put the file error.txt into D:\Program Files\Apache
Tomcat4.0\Webapps\examples\WEB-INF\classes
> when I run it ,I can't get proper result.If the error.txt location is
false,where it should be? I'm a newbie of JavaServlet,would you give me
detailed steps to correct it?
> Please help me!
> Thanks in advances!
> Edward
>