basic jsp and servlet question (JSP Model 2)
Hi
I want to make an website where i use JSP Model 2 architecture. However I got a basic question
1. I need to separate business logic from presentation with the use of jsp and servlets. Meaning I want no html code in the servlet. Can you give a simple example of how this can be done? If I map my implementation of httpServlet to a jsp page in web.xml and override doPost() and doGet(). The calls to the jsp page comes to the servlet as it should. I want to process some methods (calling sessionbeans or similar which in turn calls entitybeans) and then show the jsp page.
How do I show the jsp page without mixing html in the servlet as I've done below:
doGet(HttpServletRequest req, HttpServletResponse res)
{
PrintWriter p = response.getWriter();
p.print("<html><body>Hello world</body></html>");//I dont want to do //this, I want to display the JSP site
}[
doPost(HttpServletRequest req, HttpServletResponse res)
{
//doSomething
}
Message was edited by:
CbbLe
[1262 byte] By [
CbbLea] at [2007-11-27 10:38:43]

# 1
You should treat your servlet class much like a controller, where you can then use JSP as the view. The way you achieve this is to use the forward() method in RequestDispatcher.
Say you've got a servlet class org.yoursite.controller.YourController:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//Set some value for use in the JSP file associated wth me
req.setAttribute("greeting", "Hello world!");
//Done with our business logic, off we go to the JSP file
ServletContext app = getServletContext();
RequestDispatcher disp;
disp = app.getRequestDispatcher("/some/jsp/file.jsp);
disp.forward(req, resp);
}
Now in your JSP file:
<h1>Example</h1>
<div>
I just want to say <%= request.getAttribute("greeting") %>
</div>
Going to that servlet now executes business logic and then points to the JSP file for the view. You can forward from servlet to servlet too if needs be. The string you pass to forward() is whatever would be in the URI of the request so any <servlet-mapping> configurations in web.xml are used ;)
There is some pretty in-depth documentation on the J2EE blueprints website, namely service-to-worker and front-controller patterns. I dare say if you're looking for this sort of code you'll want to look at the composite view pattern too (also on blueprints).
# 2
I have this servlet for my index.jsp....If I do this code I get StackOverFlow. when site is started the call goes to doGet in servlet..then it forwards the site on to index.jsp and that call comes to doGet again. Meaning it will go into an infinite loop?
Can you show me how I should implement it your example? (My index.jsp page will have a loginform with password and username later (hence the servletname)
public class LoginServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
ServletContext app = getServletContext();
RequestDispatcher disp;
disp = app.getRequestDispatcher("/index.jsp");
try
{
disp.forward(request, response); //eternal loop?
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
CbbLea at 2007-7-28 18:56:14 >

# 3
You shoult have a flow like this:
index.jsp (a form with an action pointing to LoginServlet) -> LoginServlet.java (to process the form. Here, the dispatcher must be other than index.jsp or, as you said, you enter in an infinite loop) -> loggedin.jsp (another jsp page to tell to the user if the login was successful)
Manuel Leiria