Re: ServletContext.getContextPath() ?
Nic Ferrier <[email protected]> 19 Jul 2004 12:21:06 +0100
| Newsgroups | gmane.comp.java.advanced-servlets |
|---|---|
| Message-ID | <[email protected]> |
=?iso-8859-1?Q?Endre_St=F8lsvik?= <[email protected]> writes: > I must be missing something.. > > Where can I find HttpServletRequest.getContextPath()-like information, > when only having the ServletContext? > > See, when I start up the web application, I'd like to check for some > extra config-information based on what the web application is named. > > In ServletContext's JavaDoc, it says: > " There is one context per "web application" per Java Virtual Machine. > (A "web application" is a collection of servlets and content installed > under a specific subset of the server's URL namespace such as /catalog and > possibly installed via a .war file.) " > > But I -can't- get to this information (the "/catalog" part) w/o having a > request to work from? I'm just wondering if I have lost half of my brain > this weekend, or if something else is missing.. > > (I guess I in many circumstances could do some dirty trix with > context.getRealPath("foo") og getResource("foo"), and then parsing the > result for the context's name, but HEY this is really really bad, and not > what you'd want.) It's not possible for a webapp to know about where it is mapped until a request comes in. It's stupid I know... I campaigned for some time to have that changed but there was a belief that apps wouldn't be portable if this sort of information was knowable. I think that webapps are now less portable because people do stuff like this: public class MyServlet extends HttpServlet { final static String path="/mywebapp"; . . . } Which *really* reduces portability. I think what really needs to be done is that Servlet.init() looks like this: public void init (Document webappDeploymentDescriptor) throws ServletException; in other words, the init method is passed the deployment descriptor as a DOM. The other versions of init() could be done via a default implementation of the above (maybe in GenericServlet): public void init (Document ....) throws ... { NodeList initParameters = webappDeploymentDescriptor.getElementsByTagName("context-param"); final Hashtable params = new Hashtable(); for (int i = 0; i < initParamters.getLength(); i++) { Element param = (Element) initParameters.item(i); CharacterData name = (CharacterData) param.getElementsByTagName("param-name").item(0); CharacterData value = (CharacterData) param.getElementsByTagName("param-value").item(0); params.put(name.getData(), value.getData()); } init(new ServletConfig () { String getInitParameter (String name) { return (String) params.get(name); } Enumeration getInitParameterNames () { return params.keys(); } ServletContext getServletContext () { return _context; } }); } I don't see what's wrong with providing access to the DOM of the webapp. If people were really worried about making the servlet API depend like this then there should maybe be an extension servlet on top of HttpServlet that webapp containers could auto detect and pass a DOM to. Nic ------------------------ Yahoo! Groups Sponsor --------------------~--> Yahoo! Domains - Claim yours for only $14.70 http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/saFolB/TM --------------------------------------------------------------------~-> Before posting a question, try to find your answer here: <http://www.egroups.com/links/advanced-servlets> Announcements should go to: [email protected] To Post a message, send it to: [email protected] To Unsubscribe, send a blank message to: [email protected] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/advanced-servlets/ <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/