|
In the previous chapter, you learned how to use a JSP template to customize output from a servlet by dispatching to the new page. You can use the same type of technique with XTP pages as well. Consider the servlet shown in Listing 5.1.
Listing 5.1: The ViewAccount servlet.
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import javax.sql.*; public class ViewAccount extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ServletContext app = getServletContext(); RequestDispatcher disp = app.getRequestDispatcher("viewAll.xtp"); disp.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
When a request is made of the ViewAccount servlet, the code automatically forwards the request to the viewAll.xtp page, just like forwarding to a JSP page. In most cases, the servlet obtains information from a database or through variables stored for use by the XTP template.
|