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
WAR-----Original Message----- From: Robin Curts [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 13, 2004 9:26 AM To: [EMAIL PROTECTED] 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
thisfile 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
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 * [EMAIL PROTECTED] * http://www.robincurts.com * (813) 786-8634 */
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- /** * Robin Curts * [EMAIL PROTECTED] * http://www.robincurts.com * (813) 786-8634 */
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
