How to get init parameter of servlet at web.xml from welcome jsp page?

1.1k Views Asked by At

I created index.jsp page and binded path /index to it in web.xml.
It's also displayed when accessing the root of application as welcome page. It has three init parameters.

The problem is I can access then from JSP code by config.getInitParameter() if the path is full [host:port]/[appName]/index, the parameters are accessed normally.
If I try to navigate to application root [host:port]/[appName]/ welcome page is displayed but init parameters can't be accessed. config.getInitParameter() method returns null.

How to configure welcome page properly in web.xml if I want to get servlet init parameters?

2

There are 2 best solutions below

1
On

Do you have index.jsp defined as a "welcome-file" in web.xml? It sounds like the container is doing a redirect. Defining index.jsp as a welcome-file should fix that.

Hope that helps.

2
On

Usually in Java, if you want to use / to access a Java EE context, you would either:

  1. Mount it on the ROOT context.
  2. Use a reverse proxy (like nginx).

The second method is the most common solution. In this case the request goes Browser -> Reverse Proxy / Load Balancer -> One or more application servers.

This has several advantages.

  1. You can handle SSL handshaking on the Reverse Proxy.
  2. Your application server can deliver content as fast as it can to the Reverse Proxy (which is usually faster than to the browser) so it doesn't tie up the connection as long (spoon feeding).
  3. You can display meaningful error pages even if the Java EE container isn't running.
  4. Having a different domain, or subdomain for serving each context is trivial.