Results 1 to 6 of 6

Thread: How to get init-params in HttpRequestHandler servlet implementation?

  1. #1

    Default 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. #2
    Join Date
    Jul 2011
    Posts
    3

    Default 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>

  3. #3

    Default

    Quote 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

  4. #4

    Default

    Have you tried:

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

  5. #5

    Default

    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

  6. #6

    Default

    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");

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •