Frank W. Zammetti wrote:
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.util.*;
> import java.net.*;
> public class TestServlet extends HttpServlet {
> public void init(ServletConfig config) {
> try {
> ServletContext context = config.getServletContext();
> URL u = context.getResource("/test.fwz");
> System.out.println(u.getProtocol() + "://" + u.getHost() + ":" +
> u.getPort() + "/" + context.getServletContextName());
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
>
> >From my reading of the docs, getResource() should be returning a URL to
> that resource, which does exist (NPE if it didn't) which would have all
> the details needed. Unfortunately, the only detail that is coming up
> properly is the ServletContextName... I get JNDI for the protocol, -1 for
> the port and I think nothing for the host. Am I reading the docs wrong,
> should that actually NOT work, as I'm seeing? Because you can get at the
> ServletContext from a plug-in too, this seemed like the perfect answer.
>
Two things jump out at me:
1) It looks like it's container-dependent: "The servlet container must
implement the URL handlers and |URLConnection| objects that are
necessary to access the resource."
2) It's not making a "real" request so the container might short-circuit
the normal request procedure in order to return the resource as
efficiently as possible?
> Unfortunately, it has one minor drawback: it doesn't work :)
>
Minor quibble.
Dave