views:

930

answers:

4

Hi I am creating a web application using Netbeans and servlets. I have used some css into my pages. Is there a way how I can put the banner and menu which every servlet will have in one place so I do not need to rewrite this in every servlet?

Thank you

A: 

Include a JSP file containing the common fragments, e.g.

<%@include page="..." />

You could also set up a common header/footer arrangement and include the top and bottom bits at the start and end of each file.

Rob
+2  A: 

With facelets this would be cake. Since you are using servlets, try making a base Servlet class that just contains the header, menu, etc. code.

Then, have each child override, say, getBody:

Here is the parent (pseudocode):

class Template extends HttpServlet {
    doGet()
    {
        write getHeader();
        write getMenu();
        write getBody();
    }
}

class SamplePage extends Template {
    getBody()
    {
        //put body HTML here
    }
}

Then each child will be templated by Template.

Zack
I'll work on this idea =) thnxI'm even considering to using JSPs now :/
Lily
If using JSP is an option, it's much more ideal for creating viewable pages than Servlets. A JSP page ends up being a Servlet anyways..
Zack
if i was to use the above mentioned base servlet, I would include my html code (which makes use of the css) in my Template class? Is that correct. Then I would call the method in the separate servlets and I will simply add an override annotation? Am I understanding this correctly?Sorry if the question seems too simple :/
Lily
yeah...include anything that will stay the same across pages in your Template servlet, and just override the getBody(), which won't actually be everything in the literal <body> tag, just the portion of the page that changes. You could have a template.css stylesheet for styling the Template and have different stylesheets for each other page, if needed...depends how much styling the individual pages need.
Zack
A: 

Well, I just define a base servlet class that all my individual servlet classes override. Then I put my common elements in files that I read in from my servlets, and where necessary call my "output page with header and footer" method. No facelets or anything with "X" or "EE" in the title-- just a bit of good ole' Java...

Neil Coffey
A: 

I would suggest using something like Apache Tiles or SiteMesh over the standard JSP @include functionality. Those libraries are much more powerful and flexible and will lead to much more maintainable JSP code.

Nathan Voxland