|
Here's a pretty simple example of how to forward from a servlet to a JSP. It's hardly worth writing about, except that I can never remember how to do it when I need it, so I end up scrounging around to find a book or some old code where I've done it before. Since this is supposed to be a core dump of my brain, I thought I might as well put it here so I can find it more easily later. (Ahh, the beauty of databases and search facilities. :)
Okay, so imagine that you want to forward from a servlet to a JSP. Suppose the name of the JSP is "searchResults.jsp". Here's the code that will forward to that JSP: String nextJSP = "/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);
Note that this code also assumes that you have the two objects request and response available from your servlet. These come with your doGet() and doPost() method signatures, so it's not much of an assumption.
Anyway, that's all there is to it. Just make sure you don't forget that last line. I did that one time while teaching a class, and for the life of me I couldn't figure out why we weren't seeing the JSP. No errors showing up anywhere, but we were never seeing the JSP. Hate it when that happens.
|