Neal Cabage wrote:
> I am using a servlet to perform some logic, and it then forwards to a JSP.
> The problem is that the URL (in the display) still says the name URL of
> the servlet not the JSP, and thus the relative paths of the links in the JSP
> are all messed up. They're trying to link relative to the servlet, not the
> JSP.
One way to deal with this is to have all your servlets and JSP pages at the same
"directory level", so that relative references work correctly. To do this, you
would map your servlets to a name component with one level, like this:
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.mycompany.mypackage.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
Now, you can have a reference in your JSP page "/index.jsp" (relative to the
context path -- see below for more info) to a servlet path like "myservlet", and
your servlet can render links that are relative to the document root as well.
Alternatively, if you need multiple directory levels, you can include the following
tag in the <head> section of your generated page:
<base href="/qaTool/index.jsp">
to have the relative links resolved correctly. In the Struts Framework
<http://jakarta.apache.org/struts> there is a custom tag that does this for you
automatically, so all you have to say is:
<form:base/>
and it renders the appropriate base tag based on the address of the page itself.
>
> *AND* I can't seem to figure out a way to specify an absolute path using the
> following
> syntax ... it won't accept one. ANy thoughts?
>
> ...
> String URL = "/qaTool/index.jsp";
> ServletContext sc = getServletContext();
> RequestDispatcher rd = sc.getRequestDispatcher(URL);
> rd.forward(request,response);
>
If "/qaTool" is the context path for your web application, change the first line
above to say:
String URL = "/index.jsp";
because the path you specify for a request dispatcher must be relative to the
context path. The nice thing about this is that you can install your webapp on a
different context path and it will still work.
>
> Thanks
>
> Neal
>
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets