{"id":3641,"date":"2020-06-08T22:05:33","date_gmt":"2020-06-08T16:35:33","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3641"},"modified":"2020-06-08T22:05:35","modified_gmt":"2020-06-08T16:35:35","slug":"mvc-project","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/mvc-project\/","title":{"rendered":"MVC Project"},"content":{"rendered":"\n<p>MVC is a software architectural pattern that is used for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_application_development\" rel=\"nofollow noopener\" target=\"_blank\">developing web applications<\/a>. It categorizes a given application into the three interconnected parts.&nbsp;<\/p>\n\n\n\n<p><strong>a) Model<\/strong> -It means the data. The model did not depend on the Controller or the View. We can say, it is a virtual representation of data used to perform any operation in the code.<\/p>\n\n\n\n<p><strong>b) View<\/strong> &#8211; View is responsible for displaying the UI to the user.<\/p>\n\n\n\n<p><strong>c) Controller<\/strong> &#8211; It mainly handles the incoming request coming from the browser and process these requests to implement the View.<\/p>\n\n\n\n<p>The Model View Controller(MVC) design pattern decouples these major components allowing us for code reusability and parallel development. In this pattern, the Controller receives all incoming requests from the browser and then works with the model to identify the data used to create a view. The View then uses the data and generates a final presentable UI.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-mvc-architecture\/\">MVC Architectural framework<\/a> is generally a term for a software architecture for implementing the user interfaces. You can separate the software&#8217;s source code into three different layers:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Model<\/li><li>View<\/li><li>Controller<\/li><\/ul>\n\n\n\n<p><strong>Let us have an example to create Login Form using JSP + Servlet + JDBC + MySQL Example<\/strong><\/p>\n\n\n\n<p>Now we will create an Employee Login Form, and we will validate employee username and password with the database.<\/p>\n\n\n\n<p><strong>1. Create table in MySQL<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE TABLE `login` (\n&nbsp;&nbsp;`user_name` varchar(40) NOT NULL,\n&nbsp;&nbsp;`pass_word` varchar(40) DEFAULT NULL,\n&nbsp;&nbsp;PRIMARY KEY (`user_name`)\n)<\/pre>\n\n\n\n<p><strong>2. Now insert SQL statement:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO `mysql_database`.`login` (`user_name`, `pass_word`) VALUES (\"Ram\", \"Ramesh\");<\/pre>\n\n\n\n<p><strong>3<\/strong>. <strong>LoginBean.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.io.Serializable;\npublic class LoginBean implements Serializable {\n&nbsp;&nbsp;&nbsp;&nbsp;private static final long serialVersionUID = 1 L;\n&nbsp;&nbsp;&nbsp;&nbsp;private String user_name;\n&nbsp;&nbsp;&nbsp;&nbsp;private String pass_word;\n\n&nbsp;&nbsp;&nbsp;&nbsp;public String getUsername() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return user_name;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void setUsername(String username) {\nthis.username = user_name;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public String getPassword() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return pass_word;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void setPassword(String password) {\nthis.password = pass_word;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong>4. LoginDao.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport test.login.bean.LoginBean;\n\npublic class LoginDao {\n&nbsp;&nbsp;&nbsp;&nbsp;public booleanvalidate(LoginBeanloginBean) throws ClassNotFoundException {\nboolean status = false;\n\nClass.forName(\"com.mysql.jdbc.Driver\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (Connection connection = DriverManager\n.getConnection(\"jdbc:mysql:\/\/localhost:3306\/mysql_database?useSSL=false\", \"root\", \"root\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Step 2:Create a statement using the connection object\nPreparedStatementpreparedStatement = connection\n.prepareStatement(\"select * from login where user_name = ? and pass_word = ? \")) {\npreparedStatement.setString(1, loginBean.getUsername());\npreparedStatement.setString(2, loginBean.getPassword());\n\nSystem.out.println(preparedStatement);\nResultSetrs = preparedStatement.executeQuery();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status = rs.next();\n\n\n&nbsp;}&nbsp;\ncatch (SQLException e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ process sql exception\nprintSQLException(e);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return status;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;private void printSQLException(SQLException ex) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (Throwable e: ex) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (e instanceofSQLException) {\ne.printStackTrace(System.err);\nSystem.err.println(\"SQLState: \" + ((SQLException) e).getSQLState());\nSystem.err.println(\"Error Code: \" + ((SQLException) e).getErrorCode());\nSystem.err.println(\"Message: \" + e.getMessage());\nThrowable t = ex.getCause();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (t != null) {\nSystem.out.println(\"Cause: \" + t);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t = t.getCause();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong>5. Create a LoginServlet.java<\/strong><\/p>\n\n\n\n<p>Let us create LoginServlet to process <a href=\"https:\/\/www.h2kinfosys.com\/blog\/servlet-request-and-response-objects\/\">HTTP request parameters <\/a>and redirect to the appropriate JSP page based on the employee login status. If login successfully validated with the database, then redirect to loginsuccess.jsp page otherwise redirect to login.jsp page.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.io.IOException;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\nimport javax.servlet.http.HttpSession;\n\nimport test.login.bean.LoginBean;\nimport test.login.database.LoginDao;\n\n@WebServlet(\"\/login\")\npublic class LoginServlet extends HttpServlet {\n&nbsp;&nbsp;&nbsp;&nbsp;private static final long serialVersionUID = 1 L;\n&nbsp;&nbsp;&nbsp;&nbsp;private LoginDaologinDao;\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void init() {\nloginDao = new LoginDao();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;protected void doPost(HttpServletRequest request, HttpServletResponse response)\n&nbsp;&nbsp;&nbsp;&nbsp;throws ServletException, IOException {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String username = request.getParameter(\"user_name\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String password = request.getParameter(\"pass_word\");\nLoginBeanloginBean = new LoginBean();\nloginBean.setUsername(user_name);\nloginBean.setPassword(pass_word);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (loginDao.validate(loginBean)) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/HttpSession session = request.getSession();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ session.setAttribute(\"username\",user_name);\nresponse.sendRedirect(\"loginsuccess.jsp\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {\nHttpSession session = request.getSession();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/session.setAttribute(\"user\", user_name);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/response.sendRedirect(\"login.jsp\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (ClassNotFoundException e) {\ne.printStackTrace();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n\n\n\n<p><strong>8. Create a login.jsp<\/strong><\/p>\n\n\n\n<p>Let us design login HTML form with following fields:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>username<\/li><li>password<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ page language=\"java\" contentType=\"text\/html; charset=ISO-8859-1\"\npageEncoding=\"ISO-8859-1\"%&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=\"ISO-8859-1\"&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div align=\"center\"&gt;\n&lt;h1&gt;Employee Login Form&lt;\/h1&gt;\n&lt;form action=\"&lt;%=request.getContextPath()%&gt;\/login\" method=\"post\"&gt;\n&lt;table style=\"with: 100%\"&gt;\n&lt;tr&gt;\n&lt;td&gt;UserName&lt;\/td&gt;\n&lt;td&gt;&lt;input type=\"text\" name=\"username\" \/&gt;&lt;\/td&gt;\n&lt;\/tr&gt;\n&lt;tr&gt;\n&lt;td&gt;Password&lt;\/td&gt;\n&lt;td&gt;&lt;input type=\"password\" name=\"password\" \/&gt;&lt;\/td&gt;\n&lt;\/tr&gt;\n\n&lt;\/table&gt;\n&lt;input type=\"submit\" value=\"Submit\" \/&gt;\n&lt;\/form&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p><strong>9. Create a loginsuccess.jsp<\/strong><\/p>\n\n\n\n<p>After an employee successfully login then this page shows a successful message on screen:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ page language=\"java\" contentType=\"text\/html; charset=ISO-8859-1\"\npageEncoding=\"ISO-8859-1\"%&gt;\n&lt;%@page import=\"test.login.database.*\"%&gt;\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=\"ISO-8859-1\"&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div align=\"center\"&gt;\n&lt;h1&gt;You have logined successfully&lt;\/h1&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<p><strong>First OUTPUT, after entering the details:<\/strong><\/p>\n\n\n\n<p>EMPLOYEE LOGIN FORM<br>User Name: Ram<br>Password: ****<br>Submit<\/p>\n\n\n\n<p><strong>Second OUTPUT:<\/strong><\/p>\n\n\n\n<p>You Have Logged in Successfully.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MVC is a software architectural pattern that is used for developing web applications. It categorizes a given application into the three interconnected parts.&nbsp; a) Model -It means the data. The model did not depend on the Controller or the View. We can say, it is a virtual representation of data used to perform any operation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3651,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[935,933,932,936,934],"class_list":["post-3641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-controller","tag-model","tag-mvc-project","tag-mysql-example","tag-view"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3641"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3641\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3651"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}