Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Servlets Code Examples

Accessing servlet jndi environment variables

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
Accessing servlet jndi environment variables package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * It is better to use environment variables within a J2EE * context for values that can be changed a deployment time * instead of Context attributes within the web.xml. This * way you don't have to update the web.xml to change the * values. * * <env-entry> <description>the guy responsible for this site</description> <env-entry-name>webmaster</env-entry-name> <env-entry-value>[email protected]</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry> */ public class AccessingServletJndiEnvironmentVariables extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); String webMaster = null; try { // get a handle on the JNDI root context Context ctx = new InitialContext(); // and access the environment variable for this web component webMaster = (String) ctx.lookup( "java:comp/env/webmaster" ); } catch( NamingException ex ) { ex.printStackTrace(); } pw.println( "the web master is -> " + webMaster ); } }