Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: Why my class works bad in a servlet?


Message #1 by Edward <zhangsc@n...> on Sat, 18 May 2002 10:01:04 +0800
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

  Return to Index