timbmg
timbmg

Reputation: 3328

MVC pattern with http servlet

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?

Upvotes: 1

Views: 239

Answers (2)

Mitul Sanghani
Mitul Sanghani

Reputation: 230

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);
        }


}

Upvotes: 3

Sunil Rajashekar
Sunil Rajashekar

Reputation: 350

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

Upvotes: 0

Related Questions