This example demonstrates the usage of init parameters to configure Java web applications
Init parameters are great for setting up variables, that may require changes in the future. For example you can use init parameters to store your database connection or store external API keys or URLs. The benefit of using init parameters is, that you can easily change your configuration without recompiling the whole application.
Annotations vs. Deployment Descriptor
In our previous example we demonstrated the usage of Annotations to configure Servlets. Although you can use annotation to set init parameter I will not advise you to do so. The reason is, that if you use annotations to set up your init params you have to recompile your application every time you need to change a parameter. Using deployment descriptors to set up init parameter is much better. Once you put the init param values in your web.xml file and you need to change them, all you need to do is re-start the application with changed values. No recompile, no redeployment required!
@WebInitParam Annotation
Following example shows the usage of @WebInitParam Annotation in servlet declaration
@WebServlet( name = "servletParamAnnotation", urlPatterns = {"/servletParamAnnotation"}, initParams = { @WebInitParam(name = "server", value = "https://javatutorial.net"), @WebInitParam(name = "api-key", value = "h6Thd5guE4Kl12g3") } ) public class InitParamServletAnnotations extends HttpServlet { // ... }
As already mentioned you will need to recompile and redeploy your application if you choose to set your init params like this.
Context Init Parameters
If you want to share parameters across your entire application – context init parameters are good choice to do so. Every Servlet in your application shares these init parameters, and their values are the same across all servlets. You can set context parameters in your web.xml file by using <context-param>, <param-name> and <param-value> tags like this:
<context-param> <param-name>url</param-name> <param-value>https://javatutorial.net</param-value> </context-param> <context-param> <param-name>api-key</param-name> <param-value>h6Thd5guE4Kl12g3</param-value> </context-param>
Later you can use this parameters in your servlets or other components as easy as this:
package net.javatutorial.tutorials; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletContextParams extends HttpServlet { private static final long serialVersionUID = -3462096555274971485L; @Override protected void doGet(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException { ServletContext c = this.getServletContext(); PrintWriter writer = response.getWriter(); writer.append("URL: ").append(c.getInitParameter("url")).append(", API KEY: ") .append(c.getInitParameter("api-key")); } }
Servlet Init Parameters
The <init-param> tag in web.xml file, creates an init parameter specific to a given Servlet.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Servlet with Init Parameters</display-name> <servlet> <servlet-name>servletInitParams</servlet-name> <servlet-class>net.javatutorial.tutorials.ServletInitParams</servlet-class> <init-param> <param-name>url</param-name> <param-value>https://javatutorial.net</param-value> </init-param> <init-param> <param-name>api-key</param-name> <param-value>h6Thd5guE4Kl12g3</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>servletInitParams</servlet-name> <url-pattern>/servletInitParams</url-pattern> </servlet-mapping> </web-app>
Within your Servlet you can call the parameters like this:
package net.javatutorial.tutorials; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletInitParams extends HttpServlet { private static final long serialVersionUID = -1242096555274971485L; @Override protected void doGet(HttpServletRequest reqest, HttpServletResponse response) throws ServletException, IOException { ServletConfig c = this.getServletConfig(); PrintWriter writer = response.getWriter(); writer.append("URL: ").append(c.getInitParameter("url")).append(", API KEY: ") .append(c.getInitParameter("api-key")); } }
Note the difference between context parameters, which are used like this:
ServletContext c = this.getServletContext();
and init parameters:
ServletConfig c = this.getServletConfig();