{"id":156,"date":"2018-07-11T10:15:05","date_gmt":"2018-07-11T10:15:05","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=156"},"modified":"2019-05-15T07:31:29","modified_gmt":"2019-05-15T07:31:29","slug":"developing-web-application-using-mvc-2","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/developing-web-application-using-mvc-2\/","title":{"rendered":"Developing Web Application Using MVC -2"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/V10HhmghYKA\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\n&nbsp;\r\n\r\n<strong>Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the last module, we have developed the Model classes for a web application. In this module, we develop the Controller and the View components. We also need to develop a deployment descriptor for the web application.<\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong><span style=\"text-align: justify; font-size: 1em;\">Demo Of creating Web application Using MVC part 2.<\/span><\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In this demo we will develop the Controller and the View components for our web application. The following are identified as the Controller components:.<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify;\">A <strong>L<\/strong><strong>o<\/strong><strong>ginServlet <\/strong>class. This is the servlet class, to which the loginid and password are submitted by the client. It uses the Authenticator for checking the credentials and accordingly forwards to either the home page or back to the login page with a message.<\/li>\r\n \t<li style=\"text-align: justify;\">A <strong>WelcomeServlet <\/strong>class, This is the servlet class, which will be used as the default for the application, by including its url pattern in the welcome file list. This servlet will check for cookies and if it find the appropriate cookies, it would forward to LoginServlet for authentication using the value obtained from the cookies, otherwise it simply forwards to the login page.<\/li>\r\n \t<li style=\"text-align: justify;\">A <strong>L<\/strong><strong>o<\/strong><strong>g<\/strong><strong>o<\/strong><strong>utServlet <\/strong>class. This is the servlet class which is used when a clients wants to logout. It simply invalidates the session and forwards to login page. The link for logout has been provided in the home page.<\/li>\r\n<\/ul>\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\n<strong>DevelopingtheLoginServletclass<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The LoginServlet class has been developed as follows: Source code of LoginServlet class<\/p>\r\n&nbsp;\r\n\r\npackage org.epgpathshala.dad.servlet;\r\n\r\nimport javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;\r\n\r\nimport org.epgpathshala.dad.model.*;\r\n\r\npublic class LoginServlet extends HttpServlet {\r\n\r\nprivate Authenticator authenticator = new Authenticator();\r\n\r\npublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\r\ndoPost(request, response);\r\n\r\n}\r\n\r\npublic void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\r\nString lid = request.getParameter(\"lid\");\r\n\r\nString paswd = request.getParameter(\"passwd\");\r\n\r\nString rem = request.getParameter(\"rem\"); if (!authenticator.authenticate(lid, paswd)) {\r\n\r\nMessageBean message = new MessageBean(); message.setMessage(\"invalid loginid\/password\");\r\n\r\nrequest.setAttribute(\"message\", message); getServletContext().getRequestDispatcher(\"\/WEB-INF\/jsp\/login.jsp\").forward(request,\r\n\r\nresponse);\r\n\r\nreturn;\r\n\r\n}\r\n\r\nif (rem != null) {\r\n\r\nCookie cookie = new Cookie(\"lid\", lid); cookie.setMaxAge(60*60*24*10); response.addCookie(cookie);\r\n\r\ncookie = new Cookie(\"paswd\", paswd);\r\n\r\ncookie.setMaxAge(60*60*24*10); response.addCookie(cookie);\r\n\r\n}\r\n\r\nUserInfo info = authenticator.getUserInfo(lid); HttpSession session = request.getSession(false);\r\n\r\nif (session != null) { session.invalidate();\r\n\r\n}\r\n\r\nsession = request.getSession();\r\n\r\nsession.setAttribute(\"userInfo\", info); getServletContext().getRequestDispatcher(\"\/WEB-INF\/jsp\/home.jsp\").forward(request,\r\n\r\nresponse);\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; font-size: 1em;\">DevelopingtheWelcomeServletclass<\/strong><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">The WelcomeServlet class has been developed as follows: Source code of WelcomeServlet class\u00a0<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.servlet;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public class WelcomeServlet extends HttpServlet {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void doGet(HttpServletRequest request, HttpServletResponse response) throws<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException { doPost(request, response);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void doPost(HttpServletRequest request, HttpServletResponse response) throws<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Cookie[] cookies = request.getCookies();<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">if (cookies == null) {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">getServletContext().getRequestDispatcher(\"\/WEB-INF\/jsp\/login.jsp\").forward(request,\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">response);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">return;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">String lid = null;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">String paswd = null;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">for (Cookie cookie : cookies) {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">if (cookie.getName().equals(\"lid\")) { lid = cookie.getValue();<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">if (cookie.getName().equals(\"paswd\")) {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">paswd = cookie.getValue();<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">getServletContext().getNamedDispatcher(\"LoginServlet?lid=\"+lid+\"&amp;passwd=\"+paswd).forwar d(request, response);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; font-size: 1em;\">DevelopingtheLogoutServletclass<\/strong><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">The LogoutServlet class has been developed as follows: Source code of LogoutServlet class<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.servlet;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public class LogoutServlet extends HttpServlet {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void doGet(HttpServletRequest request, HttpServletResponse response) throws<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException { doPost(request, response);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">public void doPost(HttpServletRequest request, HttpServletResponse response) throws<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException {<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">HttpSession session = request.getSession(false);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">if (session!= null) { session.invalidate();<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">request.getRequestDispatcher(\"\/WEB-INF\/jsp\/login.jsp\").forward(request, response);<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">}<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">The following are identified as the View components:.<\/span>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<ul>\r\n \t<li style=\"text-align: justify;\">A <strong>l<\/strong><strong>o<\/strong><strong>gin.jsp <\/strong>JSP. This is the JSP, which is used for accepting a loginid and a password from the user. It also has a checkbox for rembember me, and it has a provision for displaying a message from a MessageBean in the request scope.<\/li>\r\n \t<li style=\"text-align: justify;\">A <strong>home.jsp <\/strong>JSP, This is the JSP, which is used for displaying a welcome message to the user. It fetches information about the user from the UserInfo bean object available in the session scope. It has a link for logout..<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nThe code for login.jsp is as follows: Code for login.jsp\r\n\r\n&lt;@page import=\"org.epgpathshala.dad.model.*\" %&gt;\r\n\r\n&lt;jsp:useBean id=\"message\" class=\"org.epgpathshala.dad.model.MessageBean\" scope=\"request\"\r\n\r\n\/&gt;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;&lt;title&gt;Login page&lt;\/title&gt;&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;Login Page&lt;\/h1&gt;\r\n\r\n&lt;form action=\"Login\" method=\"POST\" &gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">The code for home.jsp is as follows: Code for home.jsp<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;@page import=\"org.epgpathshala.dad.model.*\" %&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;jsp:useBean id=\"userInfo\" class=\"org.epgpathshala.dad.model.UserInfo\" scope=\"session\" \/&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;html&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;head&gt;&lt;title&gt;&lt;h1&gt;Home page&lt;\/h1&gt;&lt;\/title&gt;&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Welcome &lt;h2&gt;&lt;jsp:getProperty name=\"userInfo\" property=\"name\" \/&gt; from &lt;jsp:getProperty name=\"userInfo\" property=\"address\" \/&gt;&lt;\/h2&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;a href=\"Logout\"&gt;Logout&lt;\/a&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">&lt;\/html&gt;<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">The deployment descriptor web.xml is as follows: content of web.xml<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;webapp&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;display-name&gt;Sample epgpathsala app&lt;\/display-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;welcome-file-list&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;welcome-file&gt;Welcome&lt;\/welcome-file&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/welcome-file-list&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;WelcomeServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.WelcomeServlet&lt;\/servlet-class&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;WelcomeServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Welcome&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LoginServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.LoginServlet&lt;\/servlet-class&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LoginServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Login&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LogoutServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.LogoutServlet&lt;\/servlet-class&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LogoutServlet&lt;\/servlet-name&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Logout&lt;\/url-pattern&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span>\r\n\r\n<span style=\"text-align: justify; font-size: 1em;\">&lt;\/webapp&gt;<\/span>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Developing Web Application Using MVC -2<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/V10HhmghYKA\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<p class=\"hanging-indent\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\r\n\r\n<\/div>\r\n<ol>\r\n \t<li>Expert One-on-One J2EE Development without EJB by Rod Johnson and Juergen Hoeller, Wrox publication<\/li>\r\n \t<li style=\"text-align: justify;\">Core Servlets and Java Server Pages Volume 2 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Inside Servlets by Dustin R Callaway, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\r\n<\/ol>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/V10HhmghYKA\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div style=\"text-align: justify;\">\n<p>&nbsp;<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the last module, we have developed the Model classes for a web application. In this module, we develop the Controller and the View components. We also need to develop a deployment descriptor for the web application.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong><span style=\"text-align: justify; font-size: 1em;\">Demo Of creating Web application Using MVC part 2.<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In this demo we will develop the Controller and the View components for our web application. The following are identified as the Controller components:.<\/p>\n<ul>\n<li style=\"text-align: justify;\">A <strong>L<\/strong><strong>o<\/strong><strong>ginServlet <\/strong>class. This is the servlet class, to which the loginid and password are submitted by the client. It uses the Authenticator for checking the credentials and accordingly forwards to either the home page or back to the login page with a message.<\/li>\n<li style=\"text-align: justify;\">A <strong>WelcomeServlet <\/strong>class, This is the servlet class, which will be used as the default for the application, by including its url pattern in the welcome file list. This servlet will check for cookies and if it find the appropriate cookies, it would forward to LoginServlet for authentication using the value obtained from the cookies, otherwise it simply forwards to the login page.<\/li>\n<li style=\"text-align: justify;\">A <strong>L<\/strong><strong>o<\/strong><strong>g<\/strong><strong>o<\/strong><strong>utServlet <\/strong>class. This is the servlet class which is used when a clients wants to logout. It simply invalidates the session and forwards to login page. The link for logout has been provided in the home page.<\/li>\n<\/ul>\n<\/div>\n<div style=\"text-align: justify;\">\n<p><strong>DevelopingtheLoginServletclass<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The LoginServlet class has been developed as follows: Source code of LoginServlet class<\/p>\n<p>&nbsp;<\/p>\n<p>package org.epgpathshala.dad.servlet;<\/p>\n<p>import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;<\/p>\n<p>import org.epgpathshala.dad.model.*;<\/p>\n<p>public class LoginServlet extends HttpServlet {<\/p>\n<p>private Authenticator authenticator = new Authenticator();<\/p>\n<p>public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<\/p>\n<p>doPost(request, response);<\/p>\n<p>}<\/p>\n<p>public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<\/p>\n<p>String lid = request.getParameter(&#8220;lid&#8221;);<\/p>\n<p>String paswd = request.getParameter(&#8220;passwd&#8221;);<\/p>\n<p>String rem = request.getParameter(&#8220;rem&#8221;); if (!authenticator.authenticate(lid, paswd)) {<\/p>\n<p>MessageBean message = new MessageBean(); message.setMessage(&#8220;invalid loginid\/password&#8221;);<\/p>\n<p>request.setAttribute(&#8220;message&#8221;, message); getServletContext().getRequestDispatcher(&#8220;\/WEB-INF\/jsp\/login.jsp&#8221;).forward(request,<\/p>\n<p>response);<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>if (rem != null) {<\/p>\n<p>Cookie cookie = new Cookie(&#8220;lid&#8221;, lid); cookie.setMaxAge(60*60*24*10); response.addCookie(cookie);<\/p>\n<p>cookie = new Cookie(&#8220;paswd&#8221;, paswd);<\/p>\n<p>cookie.setMaxAge(60*60*24*10); response.addCookie(cookie);<\/p>\n<p>}<\/p>\n<p>UserInfo info = authenticator.getUserInfo(lid); HttpSession session = request.getSession(false);<\/p>\n<p>if (session != null) { session.invalidate();<\/p>\n<p>}<\/p>\n<p>session = request.getSession();<\/p>\n<p>session.setAttribute(&#8220;userInfo&#8221;, info); getServletContext().getRequestDispatcher(&#8220;\/WEB-INF\/jsp\/home.jsp&#8221;).forward(request,<\/p>\n<p>response);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; font-size: 1em;\">DevelopingtheWelcomeServletclass<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">The WelcomeServlet class has been developed as follows: Source code of WelcomeServlet class\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.servlet;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public class WelcomeServlet extends HttpServlet {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void doGet(HttpServletRequest request, HttpServletResponse response) throws<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException { doPost(request, response);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void doPost(HttpServletRequest request, HttpServletResponse response) throws<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Cookie[] cookies = request.getCookies();<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">if (cookies == null) {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">getServletContext().getRequestDispatcher(&#8220;\/WEB-INF\/jsp\/login.jsp&#8221;).forward(request,\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">response);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">return;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">String lid = null;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">String paswd = null;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">for (Cookie cookie : cookies) {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">if (cookie.getName().equals(&#8220;lid&#8221;)) { lid = cookie.getValue();<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">if (cookie.getName().equals(&#8220;paswd&#8221;)) {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">paswd = cookie.getValue();<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">getServletContext().getNamedDispatcher(&#8220;LoginServlet?lid=&#8221;+lid+&#8221;&amp;passwd=&#8221;+paswd).forwar d(request, response);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; font-size: 1em;\">DevelopingtheLogoutServletclass<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">The LogoutServlet class has been developed as follows: Source code of LogoutServlet class<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">package org.epgpathshala.dad.servlet;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public class LogoutServlet extends HttpServlet {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void doGet(HttpServletRequest request, HttpServletResponse response) throws<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException { doPost(request, response);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">public void doPost(HttpServletRequest request, HttpServletResponse response) throws<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ServletException, IOException {<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">HttpSession session = request.getSession(false);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">if (session!= null) { session.invalidate();<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">request.getRequestDispatcher(&#8220;\/WEB-INF\/jsp\/login.jsp&#8221;).forward(request, response);<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">The following are identified as the View components:.<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<ul>\n<li style=\"text-align: justify;\">A <strong>l<\/strong><strong>o<\/strong><strong>gin.jsp <\/strong>JSP. This is the JSP, which is used for accepting a loginid and a password from the user. It also has a checkbox for rembember me, and it has a provision for displaying a message from a MessageBean in the request scope.<\/li>\n<li style=\"text-align: justify;\">A <strong>home.jsp <\/strong>JSP, This is the JSP, which is used for displaying a welcome message to the user. It fetches information about the user from the UserInfo bean object available in the session scope. It has a link for logout..<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The code for login.jsp is as follows: Code for login.jsp<\/p>\n<p>&lt;@page import=&#8221;org.epgpathshala.dad.model.*&#8221; %&gt;<\/p>\n<p>&lt;jsp:useBean id=&#8221;message&#8221; class=&#8221;org.epgpathshala.dad.model.MessageBean&#8221; scope=&#8221;request&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;&lt;title&gt;Login page&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;h1&gt;Login Page&lt;\/h1&gt;<\/p>\n<p>&lt;form action=&#8221;Login&#8221; method=&#8221;POST&#8221; &gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">The code for home.jsp is as follows: Code for home.jsp<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;@page import=&#8221;org.epgpathshala.dad.model.*&#8221; %&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;jsp:useBean id=&#8221;userInfo&#8221; class=&#8221;org.epgpathshala.dad.model.UserInfo&#8221; scope=&#8221;session&#8221; \/&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;html&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;head&gt;&lt;title&gt;&lt;h1&gt;Home page&lt;\/h1&gt;&lt;\/title&gt;&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Welcome &lt;h2&gt;&lt;jsp:getProperty name=&#8221;userInfo&#8221; property=&#8221;name&#8221; \/&gt; from &lt;jsp:getProperty name=&#8221;userInfo&#8221; property=&#8221;address&#8221; \/&gt;&lt;\/h2&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;a href=&#8221;Logout&#8221;&gt;Logout&lt;\/a&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">&lt;\/html&gt;<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">The deployment descriptor web.xml is as follows: content of web.xml<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;webapp&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;display-name&gt;Sample epgpathsala app&lt;\/display-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;welcome-file-list&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;welcome-file&gt;Welcome&lt;\/welcome-file&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/welcome-file-list&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;WelcomeServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.WelcomeServlet&lt;\/servlet-class&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;WelcomeServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Welcome&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LoginServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.LoginServlet&lt;\/servlet-class&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LoginServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Login&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LogoutServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-class&gt;org.epgpathshala.dad.servlet.LogoutServlet&lt;\/servlet-class&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;servlet-name&gt;LogoutServlet&lt;\/servlet-name&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;url-pattern&gt;\/Logout&lt;\/url-pattern&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/servlet-mapping&gt;<\/span><\/p>\n<p><span style=\"text-align: justify; font-size: 1em;\">&lt;\/webapp&gt;<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Developing Web Application Using MVC -2<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/V10HhmghYKA\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"hanging-indent\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\n<\/div>\n<ol>\n<li>Expert One-on-One J2EE Development without EJB by Rod Johnson and Juergen Hoeller, Wrox publication<\/li>\n<li style=\"text-align: justify;\">Core Servlets and Java Server Pages Volume 2 by Marty Hall &amp; Larry Brown, Second Edition, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Inside Servlets by Dustin R Callaway, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":28,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-156","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/156\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/156\/revisions\/417"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/156\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=156"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=156"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}