You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Micael Padraig Og mac Grene <ca...@harbornet.com> on 2001/10/30 19:15:29 UTC

Bravo Rickard: PageContext include

Rickard,

I feel this contribution was significant enough, that I would not be
violating the unwritten rules of this list by saying thanks for a job well
done.  I have pretty much sat back and let you work this out.  If I can put
my mind on a problem for you sometime (we all get stuck on rare occasions),
let me know.  Meanwhile, again, thanks very much.  I think I will take a
look at your book on RMI

Something is wrong with the process that led us to this point, maybe.  ;-)

Micael


-----Original Message-----
From: Rickard Öberg <ri...@xpedio.com>
To: Tomcat Developers List <to...@jakarta.apache.org>
Cc: xdev@allt.nu <xd...@allt.nu>
Date: Tuesday, October 30, 2001 6:38 AM
Subject: Re: PageContext include


>Rickard Öberg wrote:
>
>> Any bright ideas would be most welcome.
>
>Alright, I think have the "most working" code snippet now. The following
>works on Tomcat4, Orion, and Resin. It's not exactly pretty, and it will
>not work on Servlet 2.2 engines (Tomcat3.2 and Jetty for example), but
>ya can't make an omelette without breaking eggs (or something like that).
>
>Here we go:
>----
>IncludeTag.doEndTag should have this:
>String resourcePath = getContextRelativePath(request, result);
>RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
>rd.include(request,
>            new ServletResponseWrapperInclude(response,
>pageContext.getOut()));
>---
>Add this method to the tag (from the Tomcat4/Jasper code):
>private String getContextRelativePath(ServletRequest request,
>                                        String relativePath)
>{
>    if (relativePath.startsWith("/"))
>       return (relativePath);
>    if (!(request instanceof HttpServletRequest))
>       return (relativePath);
>    HttpServletRequest hrequest = (HttpServletRequest) request;
>    String uri = (String)
>    request.getAttribute("javax.servlet.include.servlet_path");
>    if (uri == null)
>       uri = hrequest.getServletPath();
>    return (uri.substring(0, uri.lastIndexOf('/')) + '/' + relativePath);
>}
>
>Then include these two static inner classes:
>public static class ServletResponseWrapperInclude
>    extends HttpServletResponseWrapper
>{
>    /**
>     * The PrintWriter writes all output to the JspWriter of the
>     * including page.
>     */
>    PrintWriter printWriter;
>    JspWriter jspWriter;
>    ServletResponse response;
>
>    public ServletResponseWrapperInclude(ServletResponse response,
>JspWriter jspWriter)
>    {
>       super((HttpServletResponse) response);
>       this.response = response;
>       this.jspWriter = jspWriter;
>       this.printWriter = new PrintWriter(jspWriter);
>    }
>
>    /**
>     * Returns a wrapper around the JspWriter of the including page.
>     */
>    public java.io.PrintWriter getWriter() throws java.io.IOException
>    {
>       return printWriter;
>    }
>
>    public ServletOutputStream getOutputStream() throws java.io.IOException
>    {
>       return new ServletOutputStreamWrapper(jspWriter);
>    }
>}
>
>public static class ServletOutputStreamWrapper
>    extends ServletOutputStream
>{
>    JspWriter jspWriter;
>
>    public ServletOutputStreamWrapper(JspWriter aJspWriter)
>    {
>       jspWriter = aJspWriter;
>    }
>
>    public void write(int b) throws IOException
>    {
>       jspWriter.write(b);
>    }
>}
>---
>
>Now don't forget to replace all occurances of pageContext.include in
>your tags with these little snippets, or your code will break...
>
>/Rickard
>
>--
>Rickard Öberg
>Software Development Specialist
>xlurc - Xpedio Linköping Ubiquitous Research Center
>Author of "Mastering RMI"
>Email: rickard@xpedio.com
>
>
>--
>To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
>For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>