Run a JSP file from within a servlet, then get the result into a string?

I'm trying to create a very simple MVC structure with a composite view. Right now it's just has to be a simple:

--

HEADER (common on all pages)

CONTENT (dynamic)

FOOTER (common)

-

I've got a FrontController class which currently loads the layout with a header and footer, but I'm struggling to see a way to include a dynamic body in the JSP file it dispatches the request to:

The layout.jsp file:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Personal Website</title>

<link rel="stylesheet" type="text/css" href="/css/main.css" />

</head>

<body>

Header here

<div>

<h1><%= request.getAttribute("contentFile") %></h1>

<jsp:include page="<%= (String)request.getAttribute("contentFile") %>" flush="true" />

</div>

Footer here

</body>

</html>

The FrontController servlet:

/**

* The FrontController which all web requests communicate to.

* The front controller delegates requests to individual action controllers.

* @author Chris Corbyn <chris@w3style.co.uk>

*/

package org.w3style.controllers;

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

* FrontController class.

*/

publicclass FrontControllerextends HttpServlet{

/**

* Handle GET requests over HTTP.

* @param HttpServletRequest The request

* @param HttpServletResponse The response

* @throws ServletException

* @throws IOException

*/

publicvoid doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException{

req.setAttribute("contentFile","/view/home.jsp");//This will actually be dynamic

ServletContext app = getServletContext();

RequestDispatcher disp;

disp = app.getRequestDispatcher("/view/_layout/layout.jsp");

disp.forward(req, resp);

}

}

This works, but it goes against my desired logic. Right now this happens:

Front controller dispatches (currently just GET)

Front Controller sets the content-body path

Front Controller dispatches to layout.jsp

layout.jsp includes the dynamic part at runtime.

What I want to happen:

Front Controller dispatches

Front controller executes another servlet (an action controller) for the requested page

Result of that execution stored in a string (the markup basically)

Front Controller dispatches to layout.jsp which includes the dynamic content

The problem with blindly including a JSP file with no action controller being run first is that each page has no control over things like headers. I also like to add functionality for disabling the layout.jsp too (for AJAX requests etc).

I know it's a lot to ask, but can anyone post a basic example of how they include a dynamic body into a common template. The MVC stuff is not so important... I can fathom that out myself :)

I'm coming from a PHP5 background to JSP so I need to rewire my brain a bit :P

[4390 byte] By [d11wtqa] at [2007-11-27 9:52:48]
# 1
I think maybe this article does what I need. Well, maybe it doesn't do what I need but I can adapt it: http://java.sun.com/blueprints/patterns/CompositeView.htmlIt is a lot more advanced than the stuff I've learned so far though :( Good learning experience I guess :D
d11wtqa at 2007-7-13 0:21:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...