-1

I have one variable with return url from payment gateway.

private static String response_url = "http://1.1.1.1:8001/abc/xyz.html";
// private static String response_url = "http://www.something.com/xyz.html";

First variable is for local server and the second one is of production environment, I comment and un-comment and uncomment the variable according to the environment.

<action name="viewResponseFile" class="controller.RAction" method="viewResponseFile">
    <result name="success">somepage.jsp</result>
</action>

controller is mapped on that xyz.html and it is return URL from payment gateway. Now I want to make it generic.

What I am planning to do is set that variable on existing Listener class.

<listener>
     <listener-class>in.com.Initializer</listener-class>
</listener>

I tried to create HttpServletRequest using ServletRequestAware to get RequestURI but had no success.

public class Initializer implements ServletContextListener
{
    ServletContext context; 


    public Initializer()
    {

    } 

    public void contextInitialized(ServletContextEvent contextEvent)
    {
        // set-up code
    }

    public void contextDestroyed(ServletContextEvent contextEvent)
    {

    }

}

So how can achieve these requirements?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74

1 Answers1

1

Obviously comment and uncomment is not a viable way, it involves work and could cause serious mistakes;

but you should consider that also providing it from, for example, a .properties file during the build process is not good, because, even if automated, the build differs between the different environments. What if you add a property in the dev.properties and forget to add it in the prod.properties ? What if the file is modified by another user with an editor using another character encoding ?

The best way is to make these parameters external.

Your web application should not incorporate them, it should instead ask them to the environment: a configuration table in a database, a set of environment variables, etc...

This way the WAR is always the same, and the different values are provided by different servers (then it'll be up to the system administrators to write the right variables in the right places, but that's normal).

You can then be sure that an error that happens only on one environment cannot be a code problem.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • In case of environment variable approach, I will have to set the environment on `N` number of servers. in case of DB approach creating table for single URL is not feasible. Isn't there any way by which I can pick `localhost:8080\project_name\` (dynamic hostname) in a Listener class? – Govinda Sakhare Feb 27 '17 at 10:53
  • Usually a webapp have a lot of settings differing between dev and prod environment, or even that are equals between the environments, but that can change in the future, and for that reason they're parameterized in a table or in env variables... BTW, for the single URL case, can't you read the *referer* from the relative request's header ? – Andrea Ligios Feb 27 '17 at 13:17