• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem reading env-entry  RSS feed

Ed Faulk
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

[ September 22, 2004: Message edited by: Ed Faulk ]
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!