27 Developing Web Application Using MVC -1
Pravin Jain
Introduction
In the last module, we have learnt about MVC architecture for development of Web applications.. This development of web application using MVC has been divided into two modules. This is the first module for developing web application using MVC. In this module we develop the required Model classes.
Demo Of creating Web application Using MVC part 1.
In this demo we will develop the Model class for our web application. The following classes have been identified for development in this module..
- A UserInfo class, which is a simple Java bean class for storing information about a User.
- A Authenticator class, which will be used for authenticating. The class with the logic for authenticating the credentials
- A MessageBean class, which is a simple Java Bean class for storing a message String.
Developing the UserInfo class
The User class is a Simple Java Bean class. Here it is used for storing information about a user. It has only three properties, name, email and address. The source code for UserInfo is as follows:
Source code of UserInfoha class
package org.epgpathshala.dad.model;
public class UserInfo { private String name; private String email; private String address; public String getName() {
return this.name;
}
public void setName(String n) {
this.name = n;
}
public String getEmail() { return this.email;
}
public void setEmail(String e) {
this.email = e;
}
public String getAddress() { return this.address;
}
public void setAddress(String a) {
this.address = a;
}
}
Developing the Authenticator class
The Authenticator class is a simple class, it used for authenticating a user. It has a method called authenticate which takes loginid and password and returns a boolean value. In this demo a simple Authenticator has been developed, which authenticates for a fixed loginid and password. It also has a method for getting the UserInfo object for a given loginid. The code for Authenticator class is as follows:
Source for Authenticator class
package org.epgpathshala.dad.model;
public class Authenticator {
public boolean authenticate(String lid, String pswd) {
return (lid.equals(“guest”)) && (pswd.equals(“guest”));
}
public UserInfo getUserInfo(String lid) { if (!lid.equals(“guest”)) {
return null;
}
UserInfo info = new UserInfo();
info.setName(“Guest”); info.setEmail(“guest@epgpathsla.org”); info.setAddress(“Ahmedabad”);
return info;
}
}
Developing the MessageBean class
The MessageBean class is a simple Java Bean class, used for storing a message as a String. It has a single property called message. The code for MessageBean is as follows:
Source code of MessageBean class
package org.epgpathshala.dad.model;
public class MessageBean { private String message = “”; public String getMessage() {
return this.message;
}
public void setMessage(String msg) { this.message = msg;
}
}
you can view video on Developing Web Application Using MVC -1 |
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.