How to use a WebSphere variable in a Java app

Home > Search > How-to

To understand how variables work in WebSphere, let's consider a scenario where an application will be deployed to a few different WebSphere servers, where one WebSphere server is using for development (dev), one is used for integration testing (int), and one is used for production (prod).


If the app has markup that is environment specific, this creates challenges and introduces risk, as the markup of the application would need to be modified for each environment. For example, the below markup includes "dev" for the development environment. Instead of putting environment specific markup in the app, WebSphere variables can be used. It is also noteworthy that a properties file could be used, but it usually does not make much sense to use a properties file, since WebSphere can take care of this using variables. Additionally, less markup is needed in the Java app when using WebSphere variables.

private String username = "dev_username";
private String password = "dev_password";
private String jdbc     = "jdbc:mysql://"; 


There are a couple different places you can create variables in WebSphere. They can be created at Environment > WebSphere variables or at Servers > Server Types > WebSphere application servers > your JVM > Java and Process Management > process definition > Java Virtual Machines > Custom properties. For this scenario, where an application needs to use the variable, the variable will be created at Servers > Server Types > WebSphere application servers > your JVM > Java and Process Management > process definition > Java Virtual Machines > Custom properties.


Create the variable

  1. In the left panel of the WebSphere admin console, select Servers > Server Types > WebSphere application servers.
  2. Select the JVM that contains the app.
  3. Expand Java and Process Management and select process definition.
  4. Select Java Virtual Machines.
  5. Select Custom properties.
  6. Select New.
  7. Create the name and value of the variable and select OK.
  8. Select Save.
  9. Restart the JVM for this change to take effect.

In this example, a variable named ENV with a vaule of "dev" was created.


Configure your Java app to use the variable

In this example, a class named "Environment" contains the following markup. This class creates a variable named env, and the env variable gets the value of the ENV variable from WebSphere.

package com.example.main;

public class Environment {	
    public String env;

    public Environment() {
        env = System.getProperty("ENV");


You can now use the env variable in the class. One option would be to use the following markup, which would allow you to use the env variable in a JSP page.

    public String getEnvironment(){
        return env;


Another option would be to use the env varialbe in the class.

private String username = env+"_username";
private String password = env+"_password";
private String jdbc     = "jdbc:mysql://"env+""env+"_database"; 


If you are going to be passing the env variable onto a JSP page, in the JSP page, you would import "com.sample.main.Environment". Then, create a new object (foo in this example). In Environment foo = new Environment();. The text "Environment" must be used since the class is named "Environment". Using foo, get the value of the env variable. Be careful to use "out.print" and not "out.println". 

<%@page import="com.sample.main.Environment"%>

  Environment foo = new Environment();
  String env = foo.getEnvironment();
  out.print("Environment : " + env;


Now, once the app has been deployed to WebSphere, the environment should be displayed.

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter in the box below so that we can be sure you are a human.