Jetty setInitParameter is NOT initializing any parameter

做~自己de王妃 提交于 2019-12-22 05:35:10

问题


I've embedded Jetty, and I'm trying to set an initialization parameter.

The main class Main creates a servlet of Cgi which extends CGI.

Within Main, I have the following code:

ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
context2.setContextPath("/cgi");
context2.setResourceBase("./cgi-bin");
context2.setInitParameter("commandPrefix", "perl");
context2.addServlet(new ServletHolder(new Cgi()), "/");
server.setHandler(context2);

Within Cgi, I check to see the parameter:

public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println(servletConfig.getInitParameter("commandPrefix"));
        super.init(servletConfig);
}

Each time, it prints out null for the getInitParameter call. Then when the Cgi does indeed NEED to use this, it doesn't, because it's not set. Why could this be happening?


回答1:


You're setting the InitParameter on the ServletContextHandler, but you should be setting it on the ServletHolder.

(It's somewhat confusing, I know)




回答2:


You've set a context init parameter, not a servlet init parameter. So you need to retrieve it as a context init parameter instead of as a servlet init parameter.

System.out.println(servletConfig.getServletContext().getInitParameter("commandPrefix"));

Alternatively, you can of course also set it as a servlet init parameter instead, but this way the parameter will only be available to the associated servlet only, not to all other servlets running in the same context. This may or may not be what you want, depending on the concrete functional requirement.



来源:https://stackoverflow.com/questions/9216650/jetty-setinitparameter-is-not-initializing-any-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!