Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

MVC in JSP


MVC stands for Model View and Controller. It is a design pattern that 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

model2

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.Kreationnext;
  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.         String name=request.getParameter(“name”);
  15.         String password=request.getParameter(“password”);
  16.         LoginBean bean=new LoginBean();
  17.         bean.setName(name);
  18.         bean.setPassword(password);
  19.         request.setAttribute(“bean”,bean);
  20.         boolean status=bean.validate();
  21.         if(status){
  22.             RequestDispatcher rd=request.getRequestDispatcher(“login-success.jsp”);
  23.             rd.forward(request, response);
  24.         }
  25.         else{
  26.             RequestDispatcher rd=request.getRequestDispatcher(“login-error.jsp”);
  27.             rd.forward(request, response);
  28.         }
  29.     }
  30.     @Override
  31.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  32.             throws ServletException, IOException {
  33.         doPost(req, resp);
  34.     }
  35. }
File: LoginBean.java
  1. package com.Kreationnext;
  2. public class LoginBean {
  3. private String name,password;
  4. public String getName() {
  5.     return name;
  6. }
  7. public void setName(String name) {
  8.     this.name = name;
  9. }
  10. public String getPassword() {
  11.     return password;
  12. }
  13. public void setPassword(String password) {
  14.     this.password = password;
  15. }
  16. public boolean validate(){
  17.     if(password.equals(“admin”)){
  18.         return true;
  19.     }
  20.     else{
  21.         return false;
  22.     }
  23. }
  24. }
File: login-success.jsp
  1. <%@page import=“com.Kreationnext.LoginBean”%>
  2. <p>You are successfully logged in!</p>
  3. <%
  4. LoginBean bean=(LoginBean)request.getAttribute(“bean”);
  5. out.print(“Welcome, “+bean.getName());
  6. %>
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.   <servlet>
  7.   <servlet-name>s1</servlet-name>
  8.   <servlet-class>com.Kreationnext.ControllerServlet</servlet-class>
  9.   </servlet>
  10.   <servlet-mapping>
  11.   <servlet-name>s1</servlet-name>
  12.   <url-pattern>/ControllerServlet</url-pattern>
  13.   </servlet-mapping>
  14. </web-app>

mvcoutput2.jpg

mvcoutput3

Comments

comments