4

What I want to achieve is something similar to a master page in asp.net. I'm following a tutorial, but I may have missed something cause I have added my header.jspf and footer.jspf to the WEB-INF/jspf folder and index.jsp is outside of WEB-INF. I have added info in web.xml so that certain jsp-pages should automatically add the header and footer. The problem might be that index.jsp can't access anything inside the WEB-INF folder, but I thought I had solved that in a previous step in the tutorial. When I run the project, all I get is what's left of index.jsp after I remove all the header and footer stuff.

I don't want to use: <%@include file="header.jspf" %> and <..jsp:include...>.

Screenshot:

Screenshot of the project

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/index.jsp</url-pattern>
            <url-pattern>/WEB-INF/view/*</url-pattern>
            <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
            <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
        </jsp-property-group>
    </jsp-config>
</web-app>

header.jspf:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Webshop</title>
</head>
<body>
     <h1>Webshop</h1>

footer.jspf:

</body>
</html>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Erik L
  • 126
  • 4
  • 15
  • 1
    Why you don't want to use <%@include file="header.jspf" %> and <..jsp:include...> ?? – Javaguru Oct 01 '11 at 09:59
  • Well it's no biggie, I just wanted a way to not have to add the same stuff to every jsp, but two rows to add to each jsp isn't a problem. I just wanted to know if there is a way to add the header and footer via the web.xml file. – Erik L Oct 01 '11 at 10:07
  • So.. you could use SiteMesh. You can create a template page, used for every page in your project, and only the specific site content is in the actual jsp-sites then. – Javaguru Oct 01 '11 at 10:14
  • Explained on: http://uxt.ucsd.edu/uxtuistack/resources/sitemesh/index.html and http://tim.oreilly.com/pub/a/onjava/2004/09/22/sitemesh.html – Javaguru Oct 01 '11 at 10:15
  • Thanks, I'll have a look at that. – Erik L Oct 01 '11 at 10:20
  • If you change your url pattern to `*.jsp` it should work. I have a similar url pattern to yours (`/WEB-INF/view/*`) working on Jetty, but it fails on Tomcat 6 – Phil Mander Mar 30 '12 at 11:09

1 Answers1

0

I'm also doing that tutorial, and when I use Tomcat as the server it won't include the header and the footer, I have to use the glassfish server, is there any way to make tomcat include the header and the footer?

EDIT:

Replacing the default tag with this one seems to have solved the problem using tomcat as the server

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
rkrdo
  • 1,051
  • 1
  • 9
  • 15
  • Welcome to StackOverflow. Please, use the *Ask Question* button in the upper-right corner to ask new questions. – The_Fox Nov 17 '11 at 07:49