First you should check how you declare readFile and errorFile:
PreReadError readfile;
String errorfile;
"errorFile" is a String object (readFile is the PreReadError object) and you
are trying to use a PreReadError method on a String.
str=(String )errorfile.getTable("Error0");
I presume that you mean:
str=(String )readfile.getTable("Error0");
Hope this helps.
-----Original Message-----
From: Edward [mailto:zhangsc@n...]
Sent: 29 May 2002 07:20
To: Servlets
Subject: [servlets] How to create and call context in servlet?
Importance: High
I have a Hash table,I want to place it into context of a servlet,so this
Hash table can be called by any users.My Hash table file named
"PreReadError.java",it is follows:
public class PreReadError
{
public PreReadError()
{
Hashtable table=new Hashtable();
table.put("Error0","Error");
table.put("Error1","Redirect");
}
public String getTable(String str)
{
String result=(String) table.get(str);
return result;
}
}
My servlet file named "GetUserIdentity.java" is follows:
import javax.servlet.ServletContext;
public class GetUserIdentity extends HttpServlet
{
PreReadError readfile;
String errorfile;
String str;
ServletContext context;
public GetUserIdentity()
{
context=getServletContext();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//Here,I want to create a context if it is not exist,else call it's
method directly
if(context.getAttribute("errorfile")==null)
{
readfile=new PreReadError();
context.setAttribute(errorfile,readfile);
}
str=(String )errorfile.getTable("Error0");
System.out.println("Message is:"+str);
}
}
}
When I compile it,it raise a error,it follows:
F:\login\ejb>javac -classpath .;f:\j2sdkee1.3.1\lib\j2ee.jar
GetUserIdentity.java
GetUserIdentity.java:55: cannot resolve symbol
symbol : method getTable (java.lang.String)
location: class java.lang.String
str=(String )errorfile.getTable("Error0");
^
1 error
I don't know how to put my Hashtable into a servlet and how to call it's
methods?
Any idea will be appreciated!
Thanks in advance.
Edward