用JSP+Servlet+JavaBean实现MVC设计模式的流程



用JSP+Servlet+JavaBean实现MVC设计模式的流程

前端时间学习了 MVC 模式,现在来谈谈自己对MVC的认识:

 

MVC是三个单词的缩写:M,Model(模型);V,View( 视图 ),C,Control(控制)。

MVC模式的目的就是实现Web系统的职能分工,

Model层:实现系统的业务逻辑,即javaBean部分

View层:负责与用户交互,即在界面上展示数据对象给用户,即html,jsp

Control层:Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作,当然就是Servlet的职责了

 

下面我们用MVC设计模式来实现 简单的用户登录过程

1、控制器Servlet的实现

系统中只有一个servlet即ControlServlet,所有页面发起的以” *.do “的请求,都被web.xml配置给ControlServlet进行处理,在ControlServlet中根据‘ * ’的字符串(即解析用户请求的路径),调用ActionFactory生成的制定Action对象,在将处理后的URL转发给用户。

 

Java代码
1.package cn.netjava.servlet;
2.
3.import java.io.IOException;
4.import javax.servlet.ServletException;
5.import javax.servlet.http.HttpServlet;
6.import javax.servlet.http.HttpServletRequest;
7.import javax.servlet.http.HttpServletResponse;
8.
9.import cn.netjava.action.Action;
10.import cn.netjava.action.ActionFactory;
11.
12./**
13. * Servlet implementation class ControlServlet
14. */
15.public class ControlServlet extends HttpServlet {
16. private static final long serialVersionUID = 1L;
17.
18. protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19. //得到当前Servlet的请求路径
20. String pathName =request.getServletPath();
21. // request.getContextPath();得到项目名字
22. System.out.println(“pathName:”+pathName);
23. //得到请求的Action名字
24. int index = pathName.indexOf(“.”);
25. String ActionName = pathName.substring(1, index);
26. System.out.println(ActionName);
27. //获取运行时参数
28. String ActionClassName = this.getInitParameter(ActionName);
29. //得到Action对象
30. Action action = ActionFactory.getActionFactory().getAction(ActionClassName);
31. //执行Action的execute得到要返回的URL路径
32. String url = action.execute(request, response);
33. if(url==null){
34. request.getRequestDispatcher(“error.jsp”).forward(request, response);
35. }else{
36. request.getRequestDispatcher(url).forward(request, response);
37. }
38. }
39.
40.}
package cn.netjava.servlet;

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

import cn.netjava.action.Action;
import cn.netjava.action.ActionFactory;

/**
* Servlet implementation class ControlServlet
*/
public class ControlServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到当前Servlet的请求路径
String pathName =request.getServletPath();
// request.getContextPath();得到项目名字
System.out.println(“pathName:”+pathName);
//得到请求的Action名字
int index = pathName.indexOf(“.”);
String ActionName = pathName.substring(1, index);
System.out.println(ActionName);
//获取运行时参数
String ActionClassName = this.getInitParameter(ActionName);
//得到Action对象
Action action = ActionFactory.getActionFactory().getAction(ActionClassName);
//执行Action的execute得到要返回的URL路径
String url = action.execute(request, response);
if(url==null){
request.getRequestDispatcher(“error.jsp”).forward(request, response);
}else{
request.getRequestDispatcher(url).forward(request, response);
}
}

}

 

2、Action对象工厂类实现:

 

ActionFactory是一个单实例类(整个系统只需要使用其一个对象),它只提供一个Action对象,通过getAction(String ActionClassName) 的方法调用创建一个Action对象。这个方法在Control中被调用。代码如下:

 

Java代码
1.package cn.netjava.action;
2./**
3. * 根据Action名字,创建Action对象
4. * @author Administrator
5. *
6. */
7.public class ActionFactory {
8.
9. //单例模式:不需要创建对象
10. private ActionFactory(){
11. }
12. //单实例访问方法,得到ActionFactory对象
13. public static ActionFactory getActionFactory(){
14. if(af == null){
15. af = new ActionFactory();
16. }
17. return af;
18. }
19. /**
20. * 根据具体的Action类名字创建Action对象
21. * @param ActionClassName :具体的Action类全名
22. * @return:Action类型对象
23. */
24. public Action getAction(String ActionClassName){
25. Action action = null;
26. try{
27. action = (Action) Class.forName(ActionClassName).newInstance();
28. }catch(Exception e){
29. e.printStackTrace();
30. }
31. return action;
32. }
33.
34. private static ActionFactory af;
35.
36.
37.}
package cn.netjava.action;
/**
* 根据Action名字,创建Action对象
* @author Administrator
*
*/
public class ActionFactory {


//单例模式:不需要创建对象
private ActionFactory(){
}
//单实例访问方法,得到ActionFactory对象
public static ActionFactory getActionFactory(){
if(af == null){
af = new ActionFactory();
}
return af;
}
/**
* 根据具体的Action类名字创建Action对象
* @param ActionClassName :具体的Action类全名
* @return:Action类型对象
*/
public Action getAction(String ActionClassName){
Action action = null;
try{
action = (Action) Class.forName(ActionClassName).newInstance();
}catch(Exception e){
e.printStackTrace();
}
return action;
}

private static ActionFactory af;

}

3、Action接口类定义:

所有的事件处理(即Action)类都必须实现这个接口

Java代码
1.package cn.netjava.action;
2.
3.public interface Action {
4. /**
5. * 所有的具体Action实现这个接口
6. * @param request 请求对象
7. * @param response 应答对象
8. * @return :结果页面
9. */
10. public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response);
11.
12.}
package cn.netjava.action;

public interface Action {
/**
* 所有的具体Action实现这个接口
* @param request 请求对象
* @param response 应答对象
* @return :结果页面
*/
public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response);

}
4、web.xml中配置请求发送给控制器Servlet

 

最后,我们只需要在wex.xml中对MVC结构的配置:

视图页面中的请求都是以<动作名字>.do结尾,当这个请求到达web服务器后,会被服务器转向给控制器处理,控制器在根据解析出的动作名,调用对应的Action对象,处理结果,并输出结果页面,所以web.xml中必须有如下配置:

Java代码
1.<servlet>
2. <servlet-name>controlServlet</servlet-name>
3. <servlet-class>cn.netjava.servlet.ControlServlet</servlet-class>
4. <init-param>
5. <param-name>loginAction</param-name>
6. <param-value>cn.netjava.action.LoginAction</param-value>
7. </init-param>
8. </servlet>
9. <servlet-mapping>
10. <servlet-name>controlServlet</servlet-name>
11. <url-pattern>*.do</url-pattern>
12. </servlet-mapping>
<servlet>
<servlet-name>controlServlet</servlet-name>
<servlet-class>cn.netjava.servlet.ControlServlet</servlet-class>
<init-param>
<param-name>loginAction</param-name>
<param-value>cn.netjava.action.LoginAction</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>controlServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> 4、具体的Action类实现(即对登录动作进行处理的类)

 

Java代码
1.package cn.netjava.action;
2.
3.import javax.servlet.http.HttpServletRequest;
4.import javax.servlet.http.HttpServletResponse;
5.
6.public class LoginAction implements Action {
7.
8. public String execute(HttpServletRequest request,
9. HttpServletResponse response) {
10. // 得到用户名和密码
11. String userName = request.getParameter(“userName”);
12. String userPwd = request.getParameter(“userPwd”);
13. if (userName.equals(“netjava”) && userPwd.equals(“netjava”)) {
14. request.setAttribute(“userName”, userName);
15. return “main.jsp”;
16. } else {
17. return “login.jsp”;
18. }
19. }
20.
21.}
package cn.netjava.action;

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

public class LoginAction implements Action {

public String execute(HttpServletRequest request,
HttpServletResponse response) {
// 得到用户名和密码
String userName = request.getParameter(“userName”);
String userPwd = request.getParameter(“userPwd”);
if (userName.equals(“netjava”) && userPwd.equals(“netjava”)) {
request.setAttribute(“userName”, userName);
return “main.jsp”;
} else {
return “login.jsp”;
}
}

}
如果登录成功,跳转到 main.jsp页面,否则,返回login,jsp页面

 

以下是main.jsp和login.jsp页面:

main.jsp

Html代码
1.<%@ page language=”java” contentType=”text/html; charset=utf-8″
2. pageEncoding=”utf-8″%>
3.<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
4.<html>
5.<head>
6.<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
7.<title>Insert title here</title>
8.</head>
9.<body>
10.<h1 style=”color:red”><%=request.getAttribute(“userName”) %>登录成功</h1>
11.</body>
12.</html>
<%@ page language=”java” contentType=”text/html; charset=utf-8″
pageEncoding=”utf-8″%>
<!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>
<h1 style=”color:red”><%=request.getAttribute(“userName”) %>登录成功</h1>
</body>
</html> login.jsp

Html代码
1.<%@ page language=”java” contentType=”text/html; charset=utf-8″
2. pageEncoding=”utf-8″%>
3.<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
4.<html>
5.<head>
6.<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
7.<title>用户登录</title>
8.</head>
9.<body>
10.<form action=”loginAction.do” method=”post”>
11. 用户名:<input type=”text” name=”userName” id=”userName”><br>
12. 密码:<input type=”password” name=”userPwd” id=”userPwd”><br>
13. <input type=”submit” value=”登录”/>
14.</form>
15.</body>
16.</html>
<%@ page language=”java” contentType=”text/html; charset=utf-8″
pageEncoding=”utf-8″%>
<!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>用户登录</title>
</head>
<body>
<form action=”loginAction.do” method=”post”>
用户名:<input type=”text” name=”userName” id=”userName”><br>
密码:<input type=”password” name=”userPwd” id=”userPwd”><br>
<input type=”submit” value=”登录”/>
</form>
</body>
</html>
最后做个总结吧:以前我们与服务器进行交互,可能jsp页面和servlet中都将html和java代码参杂在一起,这会导致系统的系统维护困难、分工不清;例如在加有jsp代码段的网页中,程序员与美工之间的配合就非常困难!MVC结构的系统会从根本上强制我们将web系统中的数据对象、业务逻辑、用户界面三者分离,使得程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中精力于表现形式上。