How to read environment variables from a JSP.
Moderators: Jon, Steve, Ian, Dave
-
- Ebase User
- Posts: 94
- Joined: Fri Sep 14, 2007 12:18 pm
- Location: The Netherlands
- Contact:
How to read environment variables from a JSP.
Ebase allows you to read out environment variables that are set-up in the WEB-INF/web.xml file. How do you read those variables out of the file from a JSP file?
0 x
-
- Ebase User
- Posts: 94
- Joined: Fri Sep 14, 2007 12:18 pm
- Location: The Netherlands
- Contact:
Answer: How to read environment variables from a JSP.
In the <%@ page ... %> directive, add the following between the quotes defining the import if not already defined:
The line should now look something like this:
Add the following, adapted to your situation:
The <%! ... %> block is called a declaration block and is used to define variables and in page functions that can be used within the whole page. It is only necessary to place variables within this block that you are planning to use within a function. In page functions need to be declared in this block. Calls to functions are placed in <% ... %> tags.
Code: Select all
javax.naming.Context and javax.naming.InitialContext
or
javax.naming.*
Code: Select all
<%@ page language="java" import="com.ebasetech.ufs.kernel.*, javax.naming.*"%>
Code: Select all
<%!
// My Variable Declarations.
String MY_VAR1 = null;
String MY_VAR2 = null;
String MY_VAR3 = null;
String MY_VAR4 = null;
...
%>
<%
// Obtain our environment naming context
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
MY_VAR1 = (String) envCtx.lookup("MY_VAR1");
MY_VAR2 = (String) envCtx.lookup("MY_VAR2");
MY_VAR3 = (String) envCtx.lookup("MY_VAR3");
MY_VAR4 = (String) envCtx.lookup("MY_VAR4");
...
} catch (Exception e) {
%>
Error: <%=e%>
<%
}
%>
0 x
Who is online
Users browsing this forum: No registered users and 5 guests