tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <Yoav.Shap...@mpi.com>
Subject RE: Issue getting Context inside a servlet
Date Wed, 13 Oct 2004 14:03:25 GMT

Hi,
To read a resource (file, serialized, doesn't matter), use either
ServletContext#getResource or Class#getResource (or the
getResourceAsStream equivalents).  The former will get anything under
your docBase, including in WEB-INF.  The latter will get anything on
your classpath, including in WEB-INF/classes and WEB-INF/lib (but not
just WEB-INF, for example).

So if a properties file is at /WEB-INF/myFile.prop, you'd use something
like:
Properties myProps = new Properties();
myProps.load(getServletContext().getResourceAsStream("/WEB-INF/myFile.pr
op"));

To read a serialized file from /WEB-INF/myData.ser, you'd use something
like
Object myObj = new
ObjectInputStream(getServletContext().getResourceAsStream("/WEB-INF/myDa
ta.ser")).readObject();

I abbreviate my examples to make them neat for this message.  You might
want to break them out into multiple lines and add null checks, e.g. if
myFile.prop or myData.ser are not there, the getResourceAsStream call
will return null.

These approaches are safe, portable, efficient, and clean.

Yoav Shapira http://www.yoavshapira.com
 

>-----Original Message-----
>From: Robin Curts [mailto:rcurts@robincurts.com]
>Sent: Wednesday, October 13, 2004 9:56 AM
>To: Tomcat Users List
>Subject: Re: Issue getting Context inside a servlet
>
>Well I have some files that I want to have available to the web
>application (properties file, some serialized objects).  My first
>attempt was to add -Dhomedir=/path/to/some/directory when I started
>tomcat -- but that doesn't seem correct.  So then I wanted to get the
>path to the WEB-INF directory and put everything into there.  Neither
>solution seems all that great, and if you have any suggestions I'm all
>ears.
>
>By the way -- the getServletContext().getRealPath("/") was fantastic.
>Thanks for that tip.
>
>
>
>Shapira, Yoav wrote:
>
>>Hi,
>>Why are you even going down this route?  Any solution you end up with
>>will be tied to Tomcat and not portable at all.  There's also no
>>guarantee we'll keep these internal APIs consistent or available to
your
>>webapp in future Tomcat versions.
>>
>>If you do want to keep going down this path, you probably need to mark
>>your context as privileged, like the manager webapp does (see its
>>context.xml file for an example).
>>
>>The hack to get the DocBase getServletContext().getRealPath("/").
That
>>works for unpacked WARs but not packed WARs, where
getRealPath(anything)
>>returns null.  If you need the "docBase" simply as a URL, you can
always
>>do getServletContext().getResource("/") which will work in packed and
>>unpacked webapp deployments.
>>
>>If you elaborate a bit on why you need the docbase, we might be able
to
>>come up with better alternative approaches for you.
>>
>>Don't make your solution container-specific unless you really have to.
>>Stick to the letter and the spirit of the Servlet Spec, and you'll end
>>up with a portable webapp.
>>
>>Yoav Shapira http://www.yoavshapira.com
>>
>>
>>
>>
>>>-----Original Message-----
>>>From: Robin Curts [mailto:rcurts@robincurts.com]
>>>Sent: Wednesday, October 13, 2004 9:26 AM
>>>To: tomcat-user@jakarta.apache.org
>>>Subject: Issue getting Context inside a servlet
>>>
>>>What I am trying to accomplish:
>>>I'm trying to build a servlet that I can get the DocBase with, so I'm
>>>trying to locate org.apache.catalina.Context from the servlet.
>>>
>>>Tomcat Version: 5.0.28
>>>Java Version: 1.4.2_05
>>>
>>>What I'm doing:
>>>I am implementing ContainerServlet and this works great when I run
the
>>>servlet through a web application defined in
>>>$catalina.home/servers/webapps/<my_webapp>.  Pretty much in the same
>>>place as the manager servlet.  However when I run it as an installed
>>>
>>>
>>WAR
>>
>>
>>>file I am getting a nasty null pointer exception (running from
>>>$catalina.home/webapps/<my_webapp>).
>>>
>>>The issue:
>>>setWrapper() is never getting called when I have this inside the
>>>$catalina.home/webapps/ directory tree... only when I have it in
>>>$catalina.home/server/ directory tree.  Any thoughts on this?  When
>>>
>>>
>>this
>>
>>
>>>webapp goes live it will be installed as a WAR file and I won't be
able
>>>to control what directory it is in.  Any thoughts?
>>>
>>>Here is my servlet, pretty basic, just trying to find the Context and
>>>DocBase:
>>>//-- SERVLET START --
>>>import org.apache.catalina.*;
>>>import javax.servlet.http.*;
>>>import javax.servlet.*;
>>>import java.util.*;
>>>import java.io.*;
>>>
>>>public class Ctx extends HttpServlet implements ContainerServlet {
>>>
>>>   Context context;
>>>   Deployer deployer;
>>>   Wrapper wrapper;
>>>
>>>   public Ctx() {
>>>       context = null;
>>>       deployer = null;
>>>       wrapper = null;
>>>   }
>>>
>>>   public Wrapper getWrapper() {
>>>       return wrapper;
>>>   }
>>>
>>>   public void setWrapper(Wrapper wrapper) {
>>>       this.wrapper = wrapper;
>>>       try {
>>>           context = (Context)wrapper.getParent();
>>>           deployer = (Deployer)context.getParent();
>>>       } catch (NullPointerException e) {
>>>           context = null;
>>>           deployer = null;
>>>       }
>>>   }
>>>
>>>   public void destroy() {
>>>       // do nothing
>>>   }
>>>
>>>   public void init() {
>>>       // do nothing
>>>   }
>>>
>>>   public void doPost(HttpServletRequest request, HttpServletResponse
>>>response)
>>>   throws IOException, ServletException {
>>>       doGet(request, response);
>>>   }
>>>   public void doGet(HttpServletRequest request, HttpServletResponse
>>>response)
>>>   throws IOException, ServletException {
>>>       PrintWriter out = response.getWriter();
>>>       response.setContentType("text/plain");
>>>       out.println("My Context Object: " + context);
>>>       out.println("Doc Base: " + context.getDocBase());
>>>       out.flush();
>>>       out.close();
>>>   }
>>>}
>>>//-- SERVLET END --
>>>
>>>
>>>--
>>>/**
>>>* Robin Curts
>>>* rcurts@robincurts.com
>>>* http://www.robincurts.com
>>>* (813) 786-8634
>>>*/
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>>
>>>
>>
>>
>>
>>
>>This e-mail, including any attachments, is a confidential business
>communication, and may contain information that is confidential,
>proprietary and/or privileged.  This e-mail is intended only for the
>individual(s) to whom it is addressed, and may not be saved, copied,
>printed, disclosed or used by anyone else.  If you are not the(an)
intended
>recipient, please immediately delete this e-mail from your computer
system
>and notify the sender.  Thank you.
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>>
>
>
>--
>/**
> * Robin Curts
> * rcurts@robincurts.com
> * http://www.robincurts.com
> * (813) 786-8634
> */
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message