Announcement Announcement Module
Collapse
No announcement yet.
How to get init-params in HttpRequestHandler servlet implementation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get init-params in HttpRequestHandler servlet implementation?

    Hi,

    I've implemented a servlet as a simple class that implements HttpRequestHandler. My web.xml has my servlet defined as class org.springframework.web.context.support.HttpReques tHandlerServlet and my servlet-name is the same as my servlet class id. All that works. I am able to use DI to inject beans into my servlet class, and everything works as expected.

    However, I would like to specify some init-params for my servlet in web.xml, but have no idea how to access those parameters in my servlet class. I've done some looking on Google, but haven't found a solution.

    Can anyone point me in the right direction please?

    Thanks!

    Eric

  • #2
    How to get init-params in HttpRequestHandler servlet implementation

    In WEB.XML change the servlet mapping giving the url pattern.

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>(URL_PATTERN)</url-pattern>
    </servlet-mapping>

    Comment


    • #3
      Originally posted by teagan05 View Post
      In WEB.XML change the servlet mapping giving the url pattern.
      Thanks - but that doesn't answer the question. Given the following defn:
      Code:
         <servlet>
              <servlet-name>localization.xml.LocalizationServlet</servlet-name>
              <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
           <init-param>
                  <param-name>debug</param-name>
                  <param-value>0</param-value>
              </init-param>
              <init-param>
                  <param-name>listings</param-name>
                  <param-value>true</param-value>
              </init-param>
            </servlet>
          <servlet-mapping>
              <servlet-name>localization.xml.LocalizationServlet</servlet-name>
              <url-pattern>/localization/*</url-pattern>
          </servlet-mapping>
      How do I get my LocalizationServlet class to access the values of debug and listings?

      Thanks,

      Eric

      Comment


      • #4
        Have you tried:

        Code:
        getServletConfig().getInitParameter("debug");
        getServletConfig().getInitParameter("listings");

        Comment


        • #5
          getServletConfig() is a method from which class? It does not exist anywhere in the HttpRequestHandler interface. Is there some other interface that I can implement to get access to the params? What class are you extending otherwise?

          Tx,
          Eric

          Comment


          • #6
            getServletConfig() is a member of HttpRequestHandlerServlet. It is inherited from javax.servlet.GenericServlet whichi HttpRequestHandlerServlet extends.

            Actually it appears you can also just call getInitParameter() directly without having to called getServletConfig() first. It was added as a convenience method.

            So in your HttpRequestHandlerServlet class all you need to do is:

            Code:
            this.getInitParameter("debug");
            this..getInitParameter("listings");

            Comment

            Working...
            X