Get ServletContext

Discussion in 'Java' started by aerotops, Sep 6, 2007.

  1. aerotops

    aerotops Guest

    Hi,

    I would like to know if there is a convenience method for obtaining
    the servlet context for an application. I have read through many posts
    about how this can be done via the servlet config object. My problem
    is as:

    I have a Java class (a utility class) running in a webapp. I would
    like to set a XSLT transformer object as an application level
    attribute so I don't have to read it from the file system on every
    request. Since the XSLT does not change, I think this would be a
    better solution than using caching.

    Currently, I have created a servlet which loads on startup and sets
    the transformer object as a application level attribute, I would like
    to not use an init servlet just for this purpose, rather, get the
    ServletContext in a clean way and set the attribute to it.

    Thanks.
     
    aerotops, Sep 6, 2007
    #1
    1. Advertising

  2. aerotops

    Lew Guest

    aerotops wrote:
    > Hi,
    >
    > I would like to know if there is a convenience method for obtaining
    > the servlet context for an application. I have read through many posts
    > about how this can be done via the servlet config object. My problem
    > is as:
    >
    > I have a Java class (a utility class) running in a webapp. I would
    > like to set a XSLT transformer object as an application level
    > attribute so I don't have to read it from the file system on every
    > request. Since the XSLT does not change, I think this would be a
    > better solution than using caching.


    <http://java.sun.com/javaee/5/docs/api/javax/servlet/Servlet.html#getServletConfig()>
    or
    <http://java.sun.com/javaee/5/docs/api/javax/servlet/GenericServlet.html#getServletConfig()>
    <http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletConfig.html#getServletContext()>

    Retrieve your transformer from the context in your servlet and pass it on down
    to your utility method.

    --
    Lew
     
    Lew, Sep 6, 2007
    #2
    1. Advertising

  3. aerotops

    Lew Guest

    aerotops wrote:
    > I would like to know if there is a convenience method for obtaining
    > the servlet context for an application. I have read through many posts
    > about how this can be done via the servlet config object. My problem
    > is as:
    >
    > I have a Java class (a utility class) running in a webapp. I would
    > like to set a XSLT transformer object as an application level
    > attribute so I don't have to read it from the file system on every
    > request. Since the XSLT does not change, I think this would be a
    > better solution than using caching.
    >
    > Currently, I have created a servlet which loads on startup and sets
    > the transformer object as a application level attribute, I would like
    > to not use an init servlet just for this purpose, rather, get the
    > ServletContext in a clean way and set the attribute to it.


    <http://java.sun.com/javaee/5/docs/api/javax/servlet/GenericServlet.html#getServletContext()>

    or
    <http://java.sun.com/javaee/5/docs/api/javax/servlet/GenericServlet.html#getServletConfig()>
    with
    <http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletConfig.html#getServletContext()>

    Retrieve your transformer from the context in your servlet and pass it on down
    to your utility method.

    It doesn't have to be from the "init" servlet, albeit that's a great way to do
    it. Why, may I ask, are you opposed to the idiom?

    Otherwise, just do it from your controller servlet.

    --
    Lew
     
    Lew, Sep 6, 2007
    #3
  4. On 06.09.2007 22:51, aerotops wrote:
    > I would like to know if there is a convenience method for obtaining
    > the servlet context for an application. I have read through many posts
    > about how this can be done via the servlet config object. My problem
    > is as:
    >
    > I have a Java class (a utility class) running in a webapp. I would
    > like to set a XSLT transformer object as an application level
    > attribute so I don't have to read it from the file system on every
    > request. Since the XSLT does not change, I think this would be a
    > better solution than using caching.
    >
    > Currently, I have created a servlet which loads on startup and sets
    > the transformer object as a application level attribute, I would like
    > to not use an init servlet just for this purpose, rather, get the
    > ServletContext in a clean way and set the attribute to it.


    http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContextListener.html

    robert
     
    Robert Klemme, Sep 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal
    Replies:
    1
    Views:
    622
  2. Rick Roberts
    Replies:
    0
    Views:
    393
    Rick Roberts
    Aug 10, 2003
  3. RJGraham
    Replies:
    3
    Views:
    3,357
    Huang Kai
    Nov 27, 2003
  4. natG
    Replies:
    6
    Views:
    621
  5. Replies:
    2
    Views:
    10,399
Loading...

Share This Page