Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Including several files within a servlet response

Thread view: 
pepito188@gmail.com - 11 Aug 2007 12:13 GMT
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.
Lew - 11 Aug 2007 12:50 GMT
> Hello, I have a simple web application using servlets. When doGet is
> called, I want to create the response by including 3 different files:
[quoted text clipped - 3 lines]
> 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.

Signature

Lew

pepito188@gmail.com - 11 Aug 2007 13:08 GMT
> pepito...@gmail.com wrote:
> > Hello, I have a simple web application using servlets. When doGet is
[quoted text clipped - 10 lines]
> --
> Lew

OK, but how could that be done in a servlet rather than with JSP?
Lew - 11 Aug 2007 13:41 GMT
>> pepito...@gmail.com wrote:
>>> Hello, I have a simple web application using servlets. When doGet is
[quoted text clipped - 10 lines]
>
> 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.

Signature

Lew

Thomas Hawtin - 11 Aug 2007 14:20 GMT
>> OK, but how could that be done in a servlet rather than with JSP?

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
pepito188@gmail.com - 11 Aug 2007 15:37 GMT
> > pepito...@gmail.com wrote:
>
> >> OK, but how could that be done in a servlet rather than with JSP?
>
> 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.
Thomas Hawtin - 11 Aug 2007 16:15 GMT
>>> pepito...@gmail.com wrote:
>>>> OK, but how could that be done in a servlet rather than with JSP?
[quoted text clipped - 5 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.