MVC is one of the most common model for web applications. All it says is that your code must be separated into 3 logical entities: the Model, View and Controller.
We will see this with an example. For now, here is a high level idea:
Model: This is the class in which data is stored
View: This is the JSP that gets displayed to the user
Controller: This is the class which accepts user requests, does some logic (which may involve loading/querying the model) and then chooses which View to display.
Now for the example.
In our previous post, we used a form to send some data to the application.
The HelloWorld.java servlet responded to the request, handled the data (firstName/lastName) and returned a response.
It did all 3 jobs - it acted as the Model, View and Controller.
Let us now separate these functions out.
We will start by writing a class which will handle the data. This is our 'Model'.
Create a class User.java in org.confucius, like this:
package org.confucius;
import java.util.regex.Pattern;
public class User {
private String firstName = null;
private String lastName = null;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Boolean validate(){
if (firstName != null
&& lastName != null
&& firstName.length() != 0
&& lastName.length() != 0
&& Pattern.matches("[a-zA-Z]+", firstName)
&& Pattern.matches("[a-zA-Z]+", lastName)){
return true;
}
else{
return false;
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
This class can store the firstName and lastName and validate them.
Our Model is now ready.
Next, we will create two JSP views - one for displaying a Confirmation and the other for displaying an Error.
In your /jsp folder, create a Confirm.jsp like this:
<html>
<head>
</head>
<body>
Confirmation: Registration accepted.
</body>
</html>
In your /jsp folder, create a Error.jsp like this:
<html>
<head>
</head>
<body>
Error! You entered blank or names with numbers in them.
</body>
</html>
Our Views are now ready.
Let us now update our HelloWorld.java to become the Controller.
It will look like this:
package org.confucius;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User(request.getParameter("firstName"), request.getParameter("lastName"));
String view = null;
if (user.validate()){
view = "/jsp/Confirm.jsp";
}
else{
view = "/jsp/Error.jsp";
}
getServletConfig().getServletContext().getRequestDispatcher(view).forward(request,response);
}
}
You can see that our Controller only manages the request. It lets the Model handle the data, and the Views display the response.
If you build and deploy HelloWorld.war, then point your browser to:
http://localhost:8080/HelloWorld/jsp/HelloWorld.jsp
you will be able to play with the app.
Congratulations! You just wrote your first MVC style web application.
No comments:
Post a Comment