Thursday 31 March 2016

MVC IN JSP

MVC stands for Model View and Controller. It is a design patternthat separates the business logic, presentation logic and data.
Controller acts as an interface between View and Model. Controller intercepts all the incoming requests.
Model represents the state of the application i.e. data. It can also have business logic.
View represents the presentaion i.e. UI(User Interface).

Advantage of MVC (Model 2) Architecture

  1. Navigation Control is centralized
  2. Easy to maintain the large application
mvc architecture

If you new to MVC, please visit Model1 vs Model2 first.


Example of following MVC in JSP

In this example, we are using servlet as a controller, jsp as a view component, Java Bean class as a model.
In this example, we have created 5 pages:
  • index.jsp a page that gets input from the user.
  • ControllerServlet.java a servlet that acts as a controller.
  • login-success.jsp and login-error.jsp files acts as view components.
  • web.xml file for mapping the servlet.
File: index.jsp

  1. <form action="ControllerServlet" method="post">  
  2. Name:<input type="text" name="name"><br>  
  3. Password:<input type="password" name="password"><br>  
  4. <input type="submit" value="login">  
  5. </form>  
File: ControllerServlet

  1. package com.javatpoint;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import javax.servlet.RequestDispatcher;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. public class ControllerServlet extends HttpServlet {  
  10.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  11.             throws ServletException, IOException {  
  12.         response.setContentType("text/html");  
  13.         PrintWriter out=response.getWriter();  
  14.           
  15.         String name=request.getParameter("name");  
  16.         String password=request.getParameter("password");  
  17.           
  18.         LoginBean bean=new LoginBean();  
  19.         bean.setName(name);  
  20.         bean.setPassword(password);  
  21.         request.setAttribute("bean",bean);  
  22.           
  23.         boolean status=bean.validate();  
  24.           
  25.         if(status){  
  26.             RequestDispatcher rd=request.getRequestDispatcher("login-success.jsp");  
  27.             rd.forward(request, response);  
  28.         }  
  29.         else{  
  30.             RequestDispatcher rd=request.getRequestDispatcher("login-error.jsp");  
  31.             rd.forward(request, response);  
  32.         }  
  33.       
  34.     }  
  35.   
  36.     @Override  
  37.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  38.             throws ServletException, IOException {  
  39.         doPost(req, resp);  
  40.     }  
  41. }  
File: LoginBean.java

  1. package com.javatpoint;  
  2. public class LoginBean {  
  3. private String name,password;  
  4.   
  5. public String getName() {  
  6.     return name;  
  7. }  
  8. public void setName(String name) {  
  9.     this.name = name;  
  10. }  
  11. public String getPassword() {  
  12.     return password;  
  13. }  
  14. public void setPassword(String password) {  
  15.     this.password = password;  
  16. }  
  17. public boolean validate(){  
  18.     if(password.equals("admin")){  
  19.         return true;  
  20.     }  
  21.     else{  
  22.         return false;  
  23.     }  
  24. }  
  25. }  
File: login-success.jsp

  1. <%@page import="com.javatpoint.LoginBean"%>  
  2.   
  3. <p>You are successfully logged in!</p>  
  4. <%  
  5. LoginBean bean=(LoginBean)request.getAttribute("bean");  
  6. out.print("Welcome, "+bean.getName());  
  7. %>  
File: login-error.jsp

  1. <p>Sorry! username or password error</p>  
  2. <%@ include file="index.jsp" %>  
File: web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"   
  5. id="WebApp_ID" version="3.0">  
  6.     
  7.   <servlet>  
  8.   <servlet-name>s1</servlet-name>  
  9.   <servlet-class>com.javatpoint.ControllerServlet</servlet-class>  
  10.   </servlet>  
  11.   <servlet-mapping>  
  12.   <servlet-name>s1</servlet-name>  
  13.   <url-pattern>/ControllerServlet</url-pattern>  
  14.   </servlet-mapping>  
  15. </web-app>  

No comments:

Post a Comment