MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure, MVC is the embodiment of layered ideas, but differs from the three-tier design pattern (which is shared later).
MVC is a framework pattern that makes it mandatory to separate the input, processing, and output of an application. The MVC application is divided into three core components: models, views, controllers. Each of them handles its own task. The most typical MVC is the JSP + servlet + JavaBean pattern.
Example
1. Development environment: MyEclipse 8.0 Server uses its built-in MyEclipse Tomcat 6
2. Create a new Web project named Login. To create a new login.html in its Webroot directory, this page can be seen as the view in MVC, the user interface, and MVC separates it so that the web artist can focus more on the landscaping of the page, and the programmer focuses on the design of the background program. Login.html main code is as follows, very simple mainly made a traditional login page.
<form action= "Loginservlet" method= "POST" >
<div>
Name <input type= "text" name= "name"/><br/>
Password <input type= "text" name= "pas"/><br/>
<input type= "Submit" value= "Login"/>
<input type= "hidden" value= "Login" name= "typeID"/>
</div>
</form>
3. Under the SRC directory new modle. Java, the package name is login. This file can be seen as the Modle (model) in MVC, and the model represents enterprise data and business rules. The model has the most processing tasks in the three parts of MVC. For example, it might use a Widget object such as EJBS and ColdFusion components to process the database. Here we do not involve the database part, simply completes the verification function, when the user enters the name is Admin,password 1234, then considers the login to be successful. The code is as follows:
/**
* 2010-4-19
* Model.java
* Author:xiangzi
*/
Package login;
public class Model {
Private String Name=null;
Private String Pas=null;
public void SetName (String name)
{
This.name=name;
}
Public String GetName () {
return name;
}
public void Setpas (String pas)
{
This.pas=pas;
}
Public String Getpas () {
Return pas;
}
public boolean login ()
{
if ((Name.trim (). Equals ("admin")) && (Pas.trim (). Equals ("1234″)") {
return true;
}
else {
return false;
}
}
}
4. Create a servlet file. Step: Menu/file-new-web-servlet, the package name is login, the name is Loginservlet. The servlet is the C (Controller) inside MVC, where the controller accepts input from the user and invokes the model and view to fulfill the user's needs. So when you click a hyperlink in a Web page and send an HTML form, the controller (for example, the servlet) itself does not output anything and does nothing. It simply receives the request and decides which model widget to call to process the request, and then determines which view is used to display the data returned by the model processing. Here we modify the Dopost method in the Loginservlet.java file, dopost the following code:
Response.setcontenttype ("Text/html;charset=gb2312″");//sets the MIME type of the response.
Java.io.PrintWriter out = Response.getwriter ();
String type=request.getparameter ("typeID");
if (Type.trim (). Equals ("Login")) {
String name=request.getparameter ("name");//Get name in view
String pas=request.getparameter ("pas");//Get password in view
Model Newmodel=new models ();
Newmodel.setname (name);
Newmodel.setpas (PAS);
if (Newmodel.login ())
{
Out.print ("Script lanuage= ' <a href=" http://lib.csdn.net/base/18″class= ' Replace_word ' title= ' JavaScript Knowledge Base " target= ' _blank ' style= ' color: #df3434; Font-weight:bold; ' >JavaScript</a> ' >window.alert (' login successful! ') </script> ");
}
else {
Out.print (' Script lanuage= ' JavaScript ' >window.alert (' Login failed! ') </script> ");
}
}
5. Configure Web.xml files. The main code is as follows:
<servlet>
<description>this is the description i-Java component</description>
<display-name>this is the display name of my Java EE component</display-name>
<servlet-name>loginServlet</servlet-name>
<servlet-class>login.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
6. Enter the address in the browser to see the results. It is easy to see the development of Web application using Jsp+servlet+javabean.