servlet 的MVC形式

servlet 的MVC模式

MVC,即模型/视图/控制器,用来将一个WEB程序的输入,处理和输出进

行分开处理,便于管理,修改和方便分工。
处理过程:首先控制器接收页面发送的请求,并且对页面的请求数据进

行处理,然后将数据保存在模型里,再由模型请求显示数据到页面上。

 

实例:
一个简单的用户登录模块:
说明:login.html中将表单通过
 

action="LoginAction.netjava"提交到

Control这个servlet进行处理,然后Control将数据发送给LoginAction

这个类进行验证,验证正确则进入主页面,否则返回登录页面。

显示层:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 

charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginAction.netjava" method="post">
	<table align="center">
		<tr>
			<td>用户名:</td>
			<td><input type="text" name="username" 

/></td>
		</tr>
		<tr>
			<td>密码:</td>
			<td><input type="password" 

name="password" /></td>
		</tr>
		<tr>
		<td><a href="#">注册</a></td>
		<td><input type="submit" value="登陆" /></td>
		</tr>
	</table>
</form>
</body>
</html>

 

控制层:用来处理上面动作action="LoginAction.netjava",即处理

以.netjava结尾的请求,然后将数据发送给模型层进行验证

 

 

 

 

package cn.hpw.mvc;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Control
 */
public class Control extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Control() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void service(HttpServletRequest request, 

HttpServletResponse response)
    		throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	String path=request.getServletPath();//得到请求路径
    	String ActionName=path.substring(1, path.lastIndexOf

("."));//得到请求中命令动作名
    	System.out.println("动作名:"+ActionName);
    	String ActionClass=this.getInitParameter(ActionName);//

根据动作名得到实现这个Action的类名
    	System.out.println("对应的类名:"+ActionClass);
    	//创建命令对象
    	Action action=ActionFactory.getIns().getAction

(ActionClass);
    	//处理对象
    	String url=action.execute(request, response);
    	System.out.println("---------------url是:"+url);
    	if(url!=null){
    		getServletContext().getRequestDispatcher

(url).forward(request, response);//跳转到该页面
    	}
    }
}

 

模型层:
登录动作的处理:通过数据库来验证密码和账号是否正确,如果正确则

返回/index.php这个页面,否则跳回到/login.html,重新登录。

package cn.hpw.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction implements Action{

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String username=request.getParameter

("username");
		String password=request.getParameter

("password");
		if(username=="hpw"&&password=="123"){//这个本应

该是从数据库里取得然后匹配的
			System.out.println("执行了!++++++++++

++++++");
			return "/index.php";
		}else{
			System.out.println("您输入的账号或密码

不正确!");
			return "/login.html";
		}
		
		
	}

}

 

感觉控制器实际上是起一个中转的作用,而MVC是保证将模型和视图进行分离。