Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2004

Tip: Looking for answers? Try searching our database.

How do I read environment variable in Tomcat?

Thread view: 
Ed Faulk - 20 Sep 2004 02:28 GMT
I have an application that needs to read the <env-entry> information. At
present I can use  the following code to read the initialization parameters:

ServletConfig srvCfg = getServletConfig();
String sSomeData = servCfg.getInitParameter("SomeData");

But I'd like to be able to read the data from the <env-entry>. Obviously
this is a servlet and it's running under Tomcat 5.0.

Thanks,
Ed
Real Gagnon - 20 Sep 2004 02:55 GMT
> But I'd like to be able to read the data from the <env-entry>.
> Obviously this is a servlet and it's running under Tomcat 5.0.

Context ctx = new InitialContext(  );
String  envVar = (String) ctx.lookup("java:comp/env/envVar");

See
<http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources-howto.html>

Bye.
Signature

Real Gagnon  from  Quebec, Canada
* Looking for Java or PB snippets ? Visit Real's How-to  
* http://www.rgagnon.com/howto.html

Ed Faulk - 20 Sep 2004 04:30 GMT
> > But I'd like to be able to read the data from the <env-entry>.
> > Obviously this is a servlet and it's running under Tomcat 5.0.
[quoted text clipped - 3 lines]
>
> See

<http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-resources-howto.html>

Yes, I tried that. 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);
  out.println("Got the environment context...");
  sName = "MailServer";
  String  envVar = (String) envCtx.lookup(sName);
  out.println("envVar: " + envVar);
 }
 catch (NamingException e) {
  out.println("NamingException, problem looking up: \"" + sName +
"\"\nproblem 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 other ideas?

Ed


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.