Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set context-params programmatically in embedded jetty?

Looking at the following example of an embedded Jetty Example: http://musingsofaprogrammingaddict.blogspot.com.au/2009/12/running-jsf-2-on-embedded-jetty.html

The following code sample is given (below.

The author then goes on an gives an example of referring to context params in a web.xml file. eg

...
<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
  <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
...

My question is - if I want to do everything in a Java class - is there a way to set context-params programmatically?

public class JettyRunner {

    public static void main(String[] args) throws Exception {

        Server server = new Server();

        Connector connector = new SelectChannelConnector();
        connector.setPort(8080);
        connector.setHost("127.0.0.1");
        server.addConnector(connector);

        WebAppContext wac = new AliasEnhancedWebAppContext();
        wac.setContextPath("/myapp");
        wac.setBaseResource(
            new ResourceCollection(
                new String[] {"./src/main/webapp", "./target"}));
        wac.setResourceAlias("/WEB-INF/classes/", "/classes/");

        server.setHandler(wac);
        server.setStopAtShutdown(true);
        server.start();
        server.join();
    }
}
like image 662
hawkeye Avatar asked Mar 17 '12 04:03

hawkeye


People also ask

What is the difference between INIT param and context param?

<init-param> defines a value available to a single specific servlet within a context. <context-param> defines a value available to all the servlets within a context.

What is a context in Jetty?

With context file for the webapp, they probably mean the jetty. xml configuration file, which can also be used on a per-app basis, when you name it jetty-web. xml and place it in your WEB-INF directory (alongside the web. xml configuration file).

What are context params in web xml?

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.

How do you use embedded Jetty?

We can now send a request to the /heavy/async endpoint – that request will be handled by the Jetty in an asynchronous way: String url = "http://localhost:8090/heavy/async"; HttpClient client = HttpClientBuilder. create(). build(); HttpGet request = new HttpGet(url); HttpResponse response = client.


1 Answers

In your case

wac.setInitParameter("com.sun.faces.expressionFactory",
                     "com.sun.el.ExpressionFactoryImpl")

will do.

like image 129
xeye Avatar answered Oct 19 '22 18:10

xeye