Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure <context-param> programmatically without web.xml

Is there any other way to configure a context parameter excluding the one which uses web.xml as in the below example?

<context-param>
    <param-name>com.sun.faces.numberOfLogicalViews</param-name>
    <param-value>10</param-value>
</context-param>
like image 915
HappyMeal Avatar asked Jul 13 '16 10:07

HappyMeal


People also ask

Is web xml necessary?

Since the servlet 3 specification (IIRC) a web. xml is not needed anymore.

How do I get context param?

The “context-param” tag is define in “web. xml” file and it provides parameters to the entire web application. For example, store administrator's email address in “context-param” parameter to send errors notification from our web application.


Video Answer


2 Answers

Since Servlet 3.0, you can programmatically set them via ServletContext#setInitParameter().

Just put below ServletContextListener anywhere in your web project. The @WebListener annotation will make the container to automatically pickup and run it during webapp startup.

@WebListener
public class Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext(); 
        servletContext.setInitParameter("com.sun.faces.numberOfLogicalViews", "10");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // NOOP.
    }

}
like image 67
BalusC Avatar answered Oct 19 '22 19:10

BalusC


If you're using embedded Tomcat, it is like this:

Context.getServletContext()
    .getServletRegistrations()
    .get("webdavservlet")
    .setInitParameter("listings", "true");
like image 30
Sridhar Sarnobat Avatar answered Oct 19 '22 19:10

Sridhar Sarnobat