一,MVC将代码分为三个部分,分别为视图(jsp),模型(javaBean),控制部分(servlet);

  视图基本为 jsp 文件,主要内容为界面的html代码,负责显示界面;

  模型为 javaBean ,负责与数据库交互;

  控制部分为 servlet 充当,负责处理业务逻辑与页面切换。

二,MVC包括 Model1 和 Model2 两个模型;

  1. Model1 模型程序流程如下图:

  

  Model1 中界面显示部分与界面跳转,业务逻辑都由 jsp 负责,导致 jsp 中充满大量java脚本代码,

  代码重复率高,可用性低,程序功能的微小的修改往往引起大量的修改,优点是容易掌控。

  2. Model2 模型程序流程如下图:

  

  Model2 模型中分层更加明显,jsp 负责页面显示,javaBean负责数据库操作与业务逻辑,servlet

  则是核心的调度部分,负责调用 javaBean 来处理业务逻辑,同时负责界面调度切换。优点是程序

  执行更加清晰,方便后期功能的添加与修改。

三,基于MVC模型的用户登陆实例:

  1.界面部分(login.jsp登陆界面,login_success.jsp登陆成功界面,login_failure.jsp登录失败界面)

  login.jsp:

<%@ 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="../loginConf" method="post">
<center>
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="uname" /></td>
</tr>
<tr>
<td>密&nbsp;&nbsp;码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交" /> <input
type="reset" value="重置" /></td>
</tr>
</table>
</center>
</form>
</body>
</html>

  效果图:

  login_success.jsp:

<%@ 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>
<%
if(session.getAttribute("login")!=null&&session.getAttribute("login").equals("true"))
{
%>
<center>
<h2>登陆成功</h2>
</center> <%
}
else
{
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
%>
</body>
</html>

  效果图:

  login_failure.jsp

<%@ 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>
<center>
<h2>登录失败</h2>
</center>
</body>
</html>

  效果图:

  2.servlet(controller)

  loginConf.java

package com.javaweb.mvc;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* Servlet implementation class loginConf
*/
@WebServlet("/loginConf")
public class loginConf extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public loginConf() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码格式
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
//获取参数
String uname = request.getParameter("uname");
String password = request.getParameter("password");
//获取session
HttpSession session = request.getSession();
//业务逻辑判断
loginCheck lc = new loginCheck();
if(lc.isLogin(uname, password))
{
session.setAttribute("login", "true");
request.getRequestDispatcher("MVC/login_success.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("MVC/login_failure.jsp").forward(request, response);
}
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

  3.javaBean(业务逻辑Model)

package com.javaweb.mvc;

public class loginCheck
{
public boolean isLogin(String uname,String password)
{
if(uname.equals("wei")&&password.equals("123"))
{
return true;
}
else
{
return false;
}
}
}

最新文章

  1. mysql metadata lock(三)
  2. MAC电脑操作快捷键
  3. Experience all that SharePoint 15 has to offer. Start now or Remind me later.
  4. BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声
  5. XBox360自制系统的更新(Update)
  6. SGU 194 Reactor Cooling
  7. jQuery获取当前对象标签名称
  8. 开心的金明&lt;0-1背包&gt;
  9. 转载 感受K2.Net 2003工作流解决方案
  10. 微信小程序 服务器端生成用户登陆环节的 3rd_session
  11. JavaScript中innerHTML与innerText,createTextNode的区别
  12. 使用idea的springboot项目出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
  13. springboot使用lettuce连接池
  14. (七):处理MFC
  15. 有关于Integer的一些小问题
  16. Delphi XE3通过ADOConnection 连接 MySQL 5.5.27 数据库
  17. echarts中国地图坐标弹框
  18. [Python] io 模块之 open() 方法
  19. Python中如何获取类属性的列表
  20. php的精确计算

热门文章

  1. git安装教程
  2. 剑指OFFER之从上往下打印二叉树(九度OJ1523)
  3. The Woman in Red Is Seen as a Threat by Other Wom
  4. Android设计模式(1)----单例模式
  5. Tomcat启用HTTPS(生成证书、配置Tomcatserver)
  6. 数据库升级ora-04063 DBMS_REGISTRY has error
  7. 04---XML编程整理
  8. java_jdbc_可变参数_MetaData
  9. Android(java)学习笔记118:类继承的注意事项
  10. div+css(ul li)实现图片上文字下列表布局