How to Use a JSP as Template
It's easy to use a JSP to create your servlet's response and even pass arguments to the JSP.
The following JSP is stored in "/WEB-INF/jsps/time.jsp" and expects a variable 'myTime' containing a java.util.Date:
<?xml version="1.0" encoding="UTF-8"?>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!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">
<head><title>Time</title></head>
<body>
Time: <fmt:formatDate value="${myTime}" pattern="yyyy-MM-dd HH:mm:ss"/>
</body>
</html>
To invoke the JSP, get a RequestDispatcher instance for the JSP from your ServletContext and tell the dispatcher to forward to the JSP. Variables are passed to the JSP by setting request attributes (more).
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/WEB-INF/jsps/time.jsp");
request.setAttribute("myTime", new Date()); // pass current time
rd.forward(request, response); // invoke JSP and return (more)
}

