1

I'm building a web application that has a number of pages. index.html is the main page. which has a few <a> tabs that each one links to a different Servlet.

now, i have a general CSS design to my web app.

i want that each web page that is received from the Servlet will have that design.

the first idea i had is to add the CSS code to the top and bottom of the Servlet's response but i think is code replication. the next idea i had was to receive the Servlet's response with ajax, into a single div that holds the new page's content.

thank you

ro-E
  • 279
  • 1
  • 5
  • 16

1 Answers1

2

The reason JSPs exist is so you don't do things like this. Returning HTML in a Java String is cumbersome and error prone.

With a JSP, you can include different parts of other JSPs. For example, you could have a JSP that includes the CSS links. Then you can have all JSPs link to it. Here's some info related to that.

If you want to go even further, use a web framework. For example, the Stripes Framework is very easy to use and has the concept of "layouts". You can use this concept to avoid DRY violations even more. It will allow you to avoid saying, "include my header file" in every JSP you can create.

And, there are some frameworks that don't even use JSPs. For example, Wicket.

The one thing all these web frameworks have in common is they avoid generating the HTML directly in a Servlet. They abstract those details away into a more convenient construct.

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • Or use JSF that's default Java EE MVC framework for web applications – Luiggi Mendoza Sep 03 '13 at 20:15
  • @LuiggiMendoza sure, I don't know much about that. My understanding is it's a component framework like Wicket is. But it's just as valid. The point is, "avoid generating html in servlets" – Daniel Kaplan Sep 03 '13 at 20:16