On Sat, 16 Mar 2002, Alex Muc wrote:

> Date: Sat, 16 Mar 2002 17:12:02 -0500
> From: Alex Muc <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: Finding the name of the included page from the request object
>
> Hi,
>
> I've got the following setup:
>
> pageA.jsp
>   <jsp:include page="pageB.jsp">
>   </jsp:include>
>
> pageB.jsp
>    <@ include file="Header.jsp">
>
> Header.jsp
>    <!-- print out a common page header -->
>
> In the Header page, which is STATICALLY, included in pageB, which means
> we're really only dealing with pageA and pageB, I want to determine the
> name of pageB from the request (or response) object so that I can
> customize the page header that I'm displaying.  I hope this makes sense.
>
> I've looked through the mailing list and the javadocs for a method that
> will return me the information.  That is I'm looking for some method
> which will return "pageB.jsp" (or something similar) when called from
> pageB.jsp and "pageA.jsp" when called from pageA.jsp but will return
> "pageB.jsp" when called within pageB.jsp regardless of whether it is
> invoked directly by the browser or it is included in pageA.jsp.
>  Unfortunately all the methods that I've tried all return info about
> pageA.jsp, presumably because that is the one that is actually requested
> from the client.  I've tried the following methods:
> request.getContextPath()
> request.getPathInfo()
> request.getPathTranslated()
> request.getRequestURI()
> request.getServletPath()
> getServletContext().getRealPath(".")
> These methods all return Strings related to pageA, regardless of whether
> the methods are called from pageB.jsp which is dynamically included in
> pageA.
>
> So, my question is how can I find out the name of the current page being
> processed.  If I'm in pageB.jsp it should return "pageB.jsp" (or
> something similar) regardless of whether pageB.jsp is invoked directly
> from the client or indirectly through an include in pageA.jsp.
>

The <jsp:include> action is implemented in terms of
RequestDispatcher.include() in the servlet API.  If you grab the Servlet
2.3 spec <http://java.sun.com/products/servlet/download>, and read Section
8.3, you'll see that the container creates some request attributes that
define the request to the included page (pageB in your case) rather than
the including page.  They work for included JSP pages as well.

To make it possible to grab the right path no matter whether a page is
requested directly or included, you'd use logic like this:

  String servletPath =
   request.getAttribute("javax.servlet.include.servlet_path");
  if (servletPath == null) {
    servletPath = request.getServletPath();
  }

> Thank you for your help.
> Alex.
>

Craig


--
To unsubscribe:   <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>

Reply via email to