MVC (Model View Controller) in JSP

The Model 2 architecture is basically a Model-View-Controller (MVC) architecture that separates content  generation and content presentation. A web application can maximize its flexibility an dodularity by using MCV pattern. In MVC pattern M indicates Model (Java Bean or EJB), V indicates Controller (Servlet ) and V indicates View (JSP pages).
  1. Model : The model represents data and business-logic component. A model can serve multiple views. The model notifies views when it changes and lets the view query the model about its state. It also lets the controller access application functionality encapsulated by the model.
  2. View : This is th presentation component, also known as the user-infterface component. It gets data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller
  3. Controller : The controller defines application behavior. It dispatches user requests and selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a web application, user inputs are HTTP GET and POST requests. A controller selects the next view to display based on the user interactions and the outcome of the model operations
Example of MVC
Create Student table in Oracle 10g
When we create servlet form for record insertion in data base, we require a table for storing the data in data base. Here we create a 'Student' Table in oracle 10g.
CREATE TABLE Employee( emp_name varchar2(30) primary key, password varchar2(30) not null, emp_type varchar2(30) not null );
  • Employee.java
  • index.jsp
  • employee.jsp
  • report.jsp
  • UserController.java
  • ConnectionProvider.java
Employee.java
package bean; public class Employee { private String emp_name,password,emp_type; public void setEmp_name(String emp_name) { this.emp_name=emp_name; } public String getEmp_name() { return(emp_name); } public void setPassword(String password) { this.password=password; } public String getPassword() { return(password); } public void setEmp_type(String emp_type) { this.emp_type=emp_type; } public String getEmp_type() { return(emp_type); } }
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>Welcome Login page </title> </head> <body> <form name="form1" method="post" action="employee.jsp"> <fieldset><legend><font face="Courier New" size="+1" color="red">Login</font></legend> <table border="1"> <tr> <td width="160">Employee Name </td> <td width="144"><input type="text" name="emp_name" /> </td> </tr> <tr> <td>Password</td> <td><input type="text" name="password" /> </td> </tr> <tr> <td>Employee Type</td> <td> <select name="emp_type"> <option value="Admin">Admin </option> <option value="Manager">Manager</option> <option value="Seles_manager">Sales Manager</option> </select> </td> </tr> <tr> <td><input type="submit" value="Login" /></td> <td><input type="reset"></td> </tr> </table> </fieldset> </form> </body> </html>
employee.jsp
<%@page language="java" import="java.io.*"%> <jsp:useBean id="obj" scope="request" class="bean.Employee"> <jsp:setProperty property="emp_name" name="obj" value="<%=request.getParameter("emp_name")%>"/> <jsp:setProperty property="password" name="obj" value="<%=request.getParameter("password")%>"/> <jsp:setProperty property="emp_type" name="obj" value="<%=request.getParameter("emp_type")%>"/> </jsp:useBean> <jsp:forward page="UserController"> <jsp:param name="empName" value="<%=obj.getEmp_name()%>" /> <jsp:param name="password" value="<%=obj.getPassword()%>" /> <jsp:param name="empType" value="<%=obj.getEmp_type()%>" /> </jsp:forward>
report.jsp
<% out.println("Welcome"); %>
UserControllet.java
package ns; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Employee; /** * Servlet implementation class UserController */ public class UserController extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); String emp_name=request.getParameter("empName"); String password=request.getParameter("password"); String emp_type=request.getParameter("empType"); out.print("Employee Name" + emp_name); out.print("Employee Name" + password); out.print("Employee Name" + emp_type); int status=validate(emp_name,password,emp_type); if(status>0){ out.println("Login successful"); response.sendRedirect("report.jsp"); } else { out.println("Login Fail"); } } public static int validate(String emp_name,String password, String emp_type){ int login_status=0; try{ Connection con=ConnectionProvider.getCon(); PreparedStatement ps=con.prepareStatement( "select *from Employee where emp_name=? and " + "password=? and emp_type=?"); ps.setString(1,emp_name); ps.setString(2,password); ps.setString(3,emp_type); System.out.println(emp_name); System.out.println(password); System.out.println(emp_type); ResultSet rs=ps.executeQuery(); while(rs.next()) { login_status=1; } }catch(Exception e){ System.out.println(e); } return login_status; } }
ConnectionProvider.java
package ns; import java.sql.*; public class ConnectionProvider { static Connection con=null; static{ String DRIVER="oracle.jdbc.driver.OracleDriver"; String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe"; String USERNAME="sunil"; String PASSWORD="sunil"; try{ Class.forName(DRIVER); con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); } catch(Exception e){ System.out.println(e); } } public static Connection getCon(){ return con; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app > <display-name>MVCExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>UserController</display-name> <servlet-name>UserController</servlet-name> <servlet-class>ns.UserController</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserController</servlet-name> <url-pattern>/UserController</url-pattern> </servlet-mapping> </web-app>
Output
MVC
MVC
Download this example
1.) Deployed using Tomcat 6.0
2.) Deployed using Eclipse
 



 

Latest Tutorial


Most Visited Tutorial