Basic JSP Templates

There is a very basic choice for templating creation with JSP based on “includes. It could be useful to response quickly to a client, very little project, avoid to use heavy technologies, or we want to deploy our project on every application server.

The technique creates little JSP code chunks for header and footer. After create all pages from a template.

With our Eclipse go to New … > Dynamic Web Project, we will call basic_templating

The basic template for every page will be:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
 
<jsp:include page="/style/templates/styles.jsp"></jsp:include>
 
<jsp:include page="/style/templates/header.jsp"></jsp:include>
 
<!-- CONTENIDOS -->
 
<jsp:include page="/style/templates/footer.jsp"></jsp:include>

Then we can define all chosen styles in the file /style/templates/styles.jsp:

    <!-- Enlaces relativos a hojas de estilo desde un nivel 2 -->
<link href="/style/style.css" rel="stylesheet" type="text/css" media="screen" />
<link href="/style/print.css" rel="stylesheet" type="text/css" media="print" />

The header and footer for all the application will be:
/style/templates/header.jsp
/style/templates/footer.jsp

We can see the result in the following screenshot:

Basic templating with JSP

I keep the project in this eclipse zip file

Leave a Reply

Your email address will not be published. Required fields are marked *