28 Developing Web Application Using MVC -2
Pravin Jain
Introduction
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.
Demo Of creating Web application Using MVC part 2.
In this demo we will develop the Controller and the View components for our web application. The following are identified as the Controller components:.
- A LoginServlet 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.
- A WelcomeServlet 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.
- A LogoutServlet 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.
DevelopingtheLoginServletclass
The LoginServlet class has been developed as follows: Source code of LoginServlet class
package org.epgpathshala.dad.servlet;
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;
import org.epgpathshala.dad.model.*;
public class LoginServlet extends HttpServlet {
private Authenticator authenticator = new Authenticator();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String lid = request.getParameter(“lid”);
String paswd = request.getParameter(“passwd”);
String rem = request.getParameter(“rem”); if (!authenticator.authenticate(lid, paswd)) {
MessageBean message = new MessageBean(); message.setMessage(“invalid loginid/password”);
request.setAttribute(“message”, message); getServletContext().getRequestDispatcher(“/WEB-INF/jsp/login.jsp”).forward(request,
response);
return;
}
if (rem != null) {
Cookie cookie = new Cookie(“lid”, lid); cookie.setMaxAge(60*60*24*10); response.addCookie(cookie);
cookie = new Cookie(“paswd”, paswd);
cookie.setMaxAge(60*60*24*10); response.addCookie(cookie);
}
UserInfo info = authenticator.getUserInfo(lid); HttpSession session = request.getSession(false);
if (session != null) { session.invalidate();
}
session = request.getSession();
session.setAttribute(“userInfo”, info); getServletContext().getRequestDispatcher(“/WEB-INF/jsp/home.jsp”).forward(request,
response);
}
}
DevelopingtheWelcomeServletclass
The WelcomeServlet class has been developed as follows: Source code of WelcomeServlet class
package org.epgpathshala.dad.servlet;
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Cookie[] cookies = request.getCookies();
if (cookies == null) {
getServletContext().getRequestDispatcher(“/WEB-INF/jsp/login.jsp”).forward(request, response);
return;
}
String lid = null;
String paswd = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(“lid”)) { lid = cookie.getValue();
}
if (cookie.getName().equals(“paswd”)) {
paswd = cookie.getValue();
}
}
getServletContext().getNamedDispatcher(“LoginServlet?lid=”+lid+”&passwd=”+paswd).forwar d(request, response);
}
}
DevelopingtheLogoutServletclass
The LogoutServlet class has been developed as follows: Source code of LogoutServlet class
package org.epgpathshala.dad.servlet;
import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException;
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException { doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = request.getSession(false);
if (session!= null) { session.invalidate();
}
request.getRequestDispatcher(“/WEB-INF/jsp/login.jsp”).forward(request, response);
}
}
The following are identified as the View components:.
- A login.jsp 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.
- A home.jsp 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..
The code for login.jsp is as follows: Code for login.jsp
<@page import=”org.epgpathshala.dad.model.*” %>
<jsp:useBean id=”message” class=”org.epgpathshala.dad.model.MessageBean” scope=”request”
/>
<html>
<head><title>Login page</title></head>
<body>
<h1>Login Page</h1>
<form action=”Login” method=”POST” >
</form>
</body>
</html>
The code for home.jsp is as follows: Code for home.jsp
<@page import=”org.epgpathshala.dad.model.*” %>
<jsp:useBean id=”userInfo” class=”org.epgpathshala.dad.model.UserInfo” scope=”session” />
<html>
<head><title><h1>Home page</h1></title></head>
<body>
Welcome <h2><jsp:getProperty name=”userInfo” property=”name” /> from <jsp:getProperty name=”userInfo” property=”address” /></h2>
<a href=”Logout”>Logout</a>
</body>
</html>
The deployment descriptor web.xml is as follows: content of web.xml
<webapp>
<display-name>Sample epgpathsala app</display-name>
<welcome-file-list>
<welcome-file>Welcome</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>org.epgpathshala.dad.servlet.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.epgpathshala.dad.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>org.epgpathshala.dad.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/Logout</url-pattern>
</servlet-mapping>
</webapp>
you can view video on Developing Web Application Using MVC -2 |
Suggested Reading:
- Expert One-on-One J2EE Development without EJB by Rod Johnson and Juergen Hoeller, Wrox publication
- Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
- Inside Servlets by Dustin R Callaway, Pearson Education.
- Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.