How to read environment variables from a JSP.

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

AJDulk
Ebase User
Posts: 94
Joined: Fri Sep 14, 2007 12:18 pm
Location: The Netherlands
Contact:

How to read environment variables from a JSP.

#1

Postby AJDulk » Fri Nov 23, 2007 12:52 pm

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

AJDulk
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.

#2

Postby AJDulk » Fri Nov 23, 2007 12:53 pm

In the <%@ page ... %> directive, add the following between the quotes defining the import if not already defined:

Code: Select all

javax.naming.Context and javax.naming.InitialContext

or

javax.naming.*
The line should now look something like this:

Code: Select all

<%@ page language="java" import="com.ebasetech.ufs.kernel.*, javax.naming.*"%>
Add the following, adapted to your situation:

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 &#123;
    Context initCtx = new InitialContext&#40;&#41;;
    Context envCtx = &#40;Context&#41; initCtx.lookup&#40;"java&#58;comp/env"&#41;;
    MY_VAR1 = &#40;String&#41; envCtx.lookup&#40;"MY_VAR1"&#41;;
    MY_VAR2 = &#40;String&#41; envCtx.lookup&#40;"MY_VAR2"&#41;;
    MY_VAR3 = &#40;String&#41; envCtx.lookup&#40;"MY_VAR3"&#41;;
    MY_VAR4 = &#40;String&#41; envCtx.lookup&#40;"MY_VAR4"&#41;;
    ...
  &#125; catch &#40;Exception e&#41; &#123;
%>
  Error&#58; <%=e%>
<%
  &#125;
%>
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.
0 x


Who is online

Users browsing this forum: No registered users and 15 guests