Ken A wrote:
> Hello,
> Can anyone point me to an example, or give me an example of how to use
> the include method?
>
> public void include(ServletRequest request,
> ServletResponse response)
> throws ServletException,
> java.io.IOException
>
> Includes the content a resource (servlet, JSP page, HTML file) on the
> Web server generates in the response this servlet sends to another
> servlet. In essence, this method enables programmatic server-side
> includes.
>
> I am confused as to what makes up the request and response...
> Thanks,
> Ken A
>
Most commonly, you will simply pass on the request and response objects that
you received. For example, let's assume that you had a JSP page
"/copyright.jsp" and you wanted it's output included in the page being
generated by your servlet. You might do something like this:
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("....."); // All the output from this servlet
RequestDispatcher rd =
getServletContext().getRequestDispatcher("/copyright.jsp");
rd.include(request, response); // Pass our original parameters on
out.flush();
}
The JSP page that you called gets the original request (modified a little per
the Servlet API spec), and has access to the same response that you are
generating so that it's output gets included in your own.
If you wanted to include static text instead of something dynamic, it is
easier to use getResourceAsStream() instead:
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("....."); // All the output from this servlet
InputStream is =
getServletContext().getResourceAsStream("/copyright.html");
BufferedReader in =
new BufferedReader(new InputStreamReader(is));
while (true) {
String line = in.readLine();
if (line == null)
break;
out.println(line);
}
in.close();
out.flush();
}
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html