You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Wyn Easton <wy...@yahoo.com> on 2000/11/18 14:10:20 UTC

RE: Integrating servlet output into JSP

-- 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();

--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 ...

=====
Wyn Easton
wyn_easton@yahoo.com

__________________________________________________
Do You Yahoo!?
Yahoo! Calendar - Get organized for the holidays!
http://calendar.yahoo.com/

Re: Integrating servlet output into JSP

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
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
included.

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 <http://java.sun.com/products/servlet/download.html>
for more information.

> Wyn Easton

Craig McClanahan