File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Problem reading env-entry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Problem reading env-entry" Watch "Problem reading env-entry" New topic
Author

Problem reading env-entry

Ed Faulk
Greenhorn

Joined: Sep 22, 2004
Posts: 1
I have several servlets in an application and they need to access some information in the environment entries. I could, of course, put the entry in each servlet descriptor, but having them in one place is so much more convenient. In any case, I seem to be unable to access the environment context.

Here's a sample piece of code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.naming.*;

public class EnvTest extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
String sName = "java:comp/env";

try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup(sName);
sName = "MessageOption";
String envVar = (String) envCtx.lookup(sName);
out.println("envVar: " + envVar);
}
catch (NamingException e) {
out.println("NamingException, problem looking up: \"" + sName +
"\"<br>problem is: " + e);
}
}
}

which results in the following:

NamingException, problem looking up: "java:comp/env"
problem is: javax.naming.NameNotFoundException: Name java:comp is not bound
in this Context

Any ideas?

Ed
[ September 22, 2004: Message edited by: Ed Faulk ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem reading env-entry