Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Get ServletContext

Thread view: 
aerotops - 06 Sep 2007 21:51 GMT
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.
Lew - 06 Sep 2007 22:47 GMT
> Hi,
>
[quoted text clipped - 8 lines]
> 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#getServl
etConfig
()>
<http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletConfig.html#getServle
tContext
()>

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

Signature

Lew

Lew - 06 Sep 2007 23:04 GMT
> 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
[quoted text clipped - 11 lines]
> 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#getServl
etContext
()>

or
<http://java.sun.com/javaee/5/docs/api/javax/servlet/GenericServlet.html#getServl
etConfig
()>
 with
<http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletConfig.html#getServle
tContext
()>

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.

Signature

Lew

Robert Klemme - 08 Sep 2007 21:55 GMT
> 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
[quoted text clipped - 11 lines]
> 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/ServletContextLis
tener.html


    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.