tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Integrating servlet output into JSP
Date Sat, 18 Nov 2000 18:50:38 GMT
Wyn Easton wrote:

> -- for carnell - I lost the message link
> I had a moment to look at the Tomcat source and in
> RequestDispatcherImpl saw that the service() method is called
> for RequestDispatcher's forward() and include().
> JSP's _jspService() is like a regular servlet's service() method.
> So, if you want to include an already written Servlet that outputs HTML
> in the doGet() method from a JSP you'll have to call the doGet()
> from the Servlet's service() method.
> You could also add a parameter to the path when creating the
> RequestDispatcher that would allow you to
> call the doGet() when you are including the Servlet from your JSP and
> act as it did before if not included from your JSP.
> Like this:
> --IN JSP--
> RequestDispatcher rd =
>  request.getRequestDispatcher("aServlet?JspInclude=true");
> rd.include();

It is actually easier than this ... you do not need a scriptlet at all:

    <jsp:include page="aServlet?JspInclude=true" flush="true"/>

> --IN aServlet service() method--
> String jspIncld = (String)request.getParameter("JspInclude");
> if (jspIncld != null)
> if ( jspIncld.equals("true"))
> {
>   doGet(request,response);
>   return;
> }
> ... do normal servlet processing ...

There is also a standard way to tell whether your servlet or JSP page has been

The servlet container will set several request attributes that identify the
request URI and other path elements of the included servlet.  For instance, in
your servlet you can test if you are included like this:

    if (request.getAttribute("javax.servlet.include.request_uri") != null) {
        ... I have been included ...
    } else {
        ... I have not been included ...

so you don't even need a special query parameter for this purpose.  See the
servlet 2.2 specification <>
for more information.

> Wyn Easton

Craig McClanahan

View raw message