1

I need to implement a servlet with java and a tomcat server. I also need to use the MVC pattern.

So the model part is clear to me. But how do I seperate view and controler in this case? I thought my httpServlet class is my view, but how do I then implement the controller?

5
  • httpServlet is the Controller and View is you page which is use to display data,like jsp Commented Apr 21, 2015 at 8:30
  • Your servlets would be your controller, you understand model and view can be anything jsp, servlet responses, htmls etc. Commented Apr 21, 2015 at 8:31
  • @Gospel , do I store the jsp in the model? Commented Apr 21, 2015 at 8:31
  • the model is a kind of data,you can think the model is the data that you want to display in you view Commented Apr 21, 2015 at 8:36
  • 1
    exactly what you need. thejavageek.com/2013/08/11/… Commented Apr 21, 2015 at 9:14

2 Answers 2

3

Model is your business data that you deal with. and finally you sent it to client to render in view(JSP)

View is your Jsp Pages which controller sends to the client, based on client request.

Controller is your Servlet which accept the client request and execute your business logic and select appropriate view(JSP) and return it to client.

see the below Example where TestServlet is your Controller, Index.jsp is you view.

public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //business logic that deal with the your Model


        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
               rd.forward(request, response);
        }


}
Sign up to request clarification or add additional context in comments.

2 Comments

great, thanks. So I will be creating objects of my model in the controller. And if I understand correct, I will have a couple of controllers... how do I forward these objects to the other controllers? Can I include them in the request/response?
Exactly you can have separate controller for each actions login,signup... you can put your model object into request parameter request.setParameter("your model") or you can set in session depends on the requirements and there is no need to call one controller withing another controller.
0

The httpServlet is the controller, The servlet needs to forward request to a JSP(Jsp is termed as view).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.