Including several files within a servlet response

P

pepito188

Hello, I have a simple web application using servlets. When doGet is
called, I want to create the response by including 3 different files:
header.html, footer.html and the resource asked by the browser.

How can I do that? I tried RequestDispatcher, but it seems that only
works if you wan't to include ONE resource in the
HttpServletResponse... (Am I right?)

Any suggestions?

Thanks.
 
L

Lew

Hello, I have a simple web application using servlets. When doGet is
called, I want to create the response by including 3 different files:
header.html, footer.html and the resource asked by the browser.

How can I do that? I tried RequestDispatcher, but it seems that only
works if you wan't to include ONE resource in the
HttpServletResponse... (Am I right?)

Put the view part in a JSP rather than a ".java" servlet. Use either or both
of <%@include ... %> and <jsp:include ... /> to include other JSP fragments.
 
P

pepito188

Put the view part in a JSP rather than a ".java" servlet. Use either or both
of <%@include ... %> and <jsp:include ... /> to include other JSP fragments.

OK, but how could that be done in a servlet rather than with JSP?
 
L

Lew

OK, but how could that be done in a servlet rather than with JSP?

Badly.

You should separate the model components into logic classes called directly or
indirectly from a controller servlet that forwards the view to JSPs.

You're describing a view component that belongs in JSPs.
 
T

Thomas Hawtin

Thou knows how to read text resources (ServletContext.getResource on
"/WEB-INF/...")? Thou knows how to write text out to the servlet response?
You should separate the model components into logic classes called
directly or indirectly from a controller servlet that forwards the view
to JSPs.

One doesn't have to use JSPs. There are plenty of template systems out
there. One doesn't have to use these either.

JSPs do make it easy to start with a page designed in HTML - one only
need to change the extension. For a complicated site one would want to
end up with Java code ("a servlet") writing to a SAX-like interface.
Adding "scriptlets" to the JSP takes you on a nice path between the two
extremes. JSP tags actually takes you down a blind alley.

Tom Hawtin
 
P

pepito188

Thou knows how to read text resources (ServletContext.getResource on
"/WEB-INF/...")? Thou knows how to write text out to the servlet response?


Yes I know how to do that, but first of all, my resources are static
html pages, so they are not stored under WEB-INF. I was wondering if
there exists an easy way to do that avoiding reading files, in a
similar way that RequestDispatcher does.
 
T

Thomas Hawtin

Yes I know how to do that, but first of all, my resources are static
html pages, so they are not stored under WEB-INF. I was wondering if
there exists an easy way to do that avoiding reading files, in a
similar way that RequestDispatcher does.

There's no reason why static pages should not be stored under WEB-INF.
Anything that is not directly accessible should be in that directory
(unless it's a static web site, that should usually be everything apart
from META-INF).

If thou wants to go all the way back through the servlet mechanism, thou
should be able to use getRequestDispatcher multiple times. Beware that
this will not necessarily treat the files as static. When one uses
<jsp:include page="..."> as opposed to <%@ include file="..."> the JSP
is compiled to use getRequestDispatcher multiple times (one can probably
look at the source intermediate code depending upon implementation).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top