JSP Tutorials

MVC in JSP
Previous Home Next
adplus-dvertising

MVC in JSP

In MVC is used for Model View with Controller and presentation logic ,business logic.

In MVC is basically Controller with interface between View and Model of incoming requests.

MVC model to get the state of the application ,we can also have business logic.

MVC View is display as a presentaion which means UI(User Interface).

Some basic advantages of MVC

  1. Navigation Control is centralized
  2. Easy access to large application

Example

MVC using servlet(controller),jsp(view component)and Java Bean class(model).
  1. index.jsp a page that gets input from the user.
  2. ControllerServlet.java a servlet that acts as a controller.
  3. login-success.jsp and login-error.jsp files acts as view components.
  4. web.xml file for mapping the servlet.
index.html
    <form action="ControllerServlet" method="post">  
    Name:<input type="text" name="name"><br>  
    Password:<input type="password" name="password"><br>  
    <input type="submit" value="login">  
    </form>  

    <form action="ControllerServlet" method="post">  
    Name:<input type="text" name="name"><br>  
    Password:<input type="password" name="password"><br>  
    <input type="submit" value="login">  
    </form> 
ControllerServlet
package com.javatpoint;  
    import java.io.IOException;  
    import java.io.PrintWriter;  
    import javax.servlet.RequestDispatcher;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    public class ControllerServlet extends HttpServlet {  
        protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            response.setContentType("text/html");  
            PrintWriter out=response.getWriter();  
              
            String name=request.getParameter("name");  
            String password=request.getParameter("password");  
              
            LoginBean bean=new LoginBean();  
            bean.setName(name);  
            bean.setPassword(password);  
            request.setAttribute("bean",bean);  
              
            boolean status=bean.validate();  
              
            if(status){  
                RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");  
                rd.forward(request, response);  
            }  
            else{  
                RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");  
                rd.forward(request, response);  
            }  
          
        }  
      
        @Override  
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
            doPost(req, resp);  
        }  
    }  

    package com.javatpoint;  
    import java.io.IOException;  
    import java.io.PrintWriter;  
    import javax.servlet.RequestDispatcher;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    public class ControllerServlet extends HttpServlet {  
        protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                throws ServletException, IOException {  
            response.setContentType("text/html");  
            PrintWriter out=response.getWriter();  
              
            String name=request.getParameter("name");  
            String password=request.getParameter("password");  
              
            LoginBean bean=new LoginBean();  
            bean.setName(name);  
            bean.setPassword(password);  
            request.setAttribute("bean",bean);  
              
            boolean status=bean.validate();  
              
            if(status){  
                RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");  
                rd.forward(request, response);  
            }  
            else{  
                RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");  
                rd.forward(request, response);  
            }  
          
        }  
      
        @Override  
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
                throws ServletException, IOException {  
            doPost(req, resp);  
        }  
    }  
Previous Home Next