参考书籍:《J2EE开源编程精要15讲》

MVC(Model View Controller),Model(模型)表示业务逻辑层,View(视图)代表表述层,Controller(控制)表示控制层。

在Java Web应用程序中,

  View部分一般用JSP和HTML构建。客户在View部分提交请求,在业务逻辑层处理后,把结果返回给View部分显示

  Controller部分一般用Servlet组成,把用户的请求发给适当的业务逻辑组件处理;处理后又返回Controller,把结果转发给适当的View组件

  Model部分包括业务逻辑层和数据库访问层,一般由JavaBean或EJB(Enterprise JavaBean,企业级JavaBean)构建。数据访问层也叫数据持久层,与数据库打交道,常用JDBC API或Hibernate构建。

示例:

View部分:

  index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>登陆界面</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22
23   <body>
24     <form method="post" action="LoginServlet">
25         用户名:<input type="text" name="username" size="15"><br><br>
26         密&nbsp&nbsp码:<input type="password" name="password" size="15"><br><br>
27         <input type="submit" name="submit" value="登陆"><br>
28     </form>
29   </body>
30 </html>

View Code

  main.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>主页面</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26     <h1>
27         <%=session.getAttribute("username") %>,你成功登陆,现已进入主界面!
28     </h1>
29   </body>
30 </html>

View Code

  register.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>注册页面</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26     <h1>
27         <%=session.getAttribute("username") %>,你未能成功登陆。
28         <br>现已进入注册页面,请注册你的信息!
29     </h1>
30   </body>
31 </html>

View Code

Controller部分:

  LoginServlet.java

 1 /**
 2  * 控制组件,在web.xml中将<servlet-mapping>标签内的<url-pattern>设置为"/LoginServlet",
 3  * 所以能接受来自index.jsp中action="LoginServlet"的表单的HTTP POST请求
 4  */
 5 package login;
 6
 7 import java.io.IOException;
 8 import java.io.PrintWriter;
 9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15
16 public class LoginServlet extends HttpServlet {
17
18     /**
19      * Constructor of the object.
20      */
21     public LoginServlet() {
22         super();
23     }
24
25     /**
26      * Destruction of the servlet. <br>
27      */
28     public void destroy() {
29         super.destroy(); // Just puts "destroy" string in log
30         // Put your code here
31     }
32
33     /**
34      * The doGet method of the servlet. <br>
35      *
36      * This method is called when a form has its tag value method equals to get.
37      *
38      * @param request the request send by the client to the server
39      * @param response the response send by the server to the client
40      * @throws ServletException if an error occurred
41      * @throws IOException if an error occurred
42      */
43     public void doGet(HttpServletRequest request, HttpServletResponse response)
44             throws ServletException, IOException {
45         doPost(request,response);
46
47     }
48
49     /**
50      * The doPost method of the servlet. <br>
51      *
52      * This method is called when a form has its tag value method equals to post.
53      *
54      * @param request the request send by the client to the server
55      * @param response the response send by the server to the client
56      * @throws ServletException if an error occurred
57      * @throws IOException if an error occurred
58      */
59     public void doPost(HttpServletRequest request, HttpServletResponse response)
60             throws ServletException, IOException {
61
62         //获取请求中的用户名和密码
63         String username=request.getParameter("username");
64         String password=request.getParameter("password");
65
66         //生成一个Session对象,在main.jsp和register.jsp中可以根据Session对象获取用户名
67         HttpSession session=request.getSession(true);
68         session.removeAttribute("username");
69         session.setAttribute("username", username);
70
71         //调用业务逻辑组件,检查该用户是否已注册
72         LoginHandler login=new LoginHandler();
73         boolean mark=login.checkLogin(username,password);
74
75         //根据查询结果跳转
76         if(mark)
77             response.sendRedirect("main.jsp");
78         else
79             response.sendRedirect("register.jsp");
80     }
81
82     /**
83      * Initialization of the servlet. <br>
84      *
85      * @throws ServletException if an error occurs
86      */
87     public void init() throws ServletException {
88         // Put your code here
89     }
90
91 }

View Code

Model部分

  业务逻辑组件,LoginHandler.java

 1 /**
 2  * 业务逻辑组件,从数据访问组件DBPool中获得数据库链接,检查用户记录是否存在
 3  */
 4 package login;
 5
 6 import java.sql.*;
 7
 8 public class LoginHandler {
 9     public LoginHandler(){}
10
11     Connection conn;
12     PreparedStatement ps;
13     ResultSet rs;
14
15     public boolean checkLogin(String username,String password){
16         DBPool con=new DBPool();
17         try{
18             conn=con.getConnection();
19             String sql="select * from UserInfo where username=? and password=?";
20             ps=conn.prepareStatement(sql);
21             ps.setString(1, username);
22             ps.setString(2, password);
23             rs=ps.executeQuery();
24             if(rs.next()){
25                 conn.close();
26                 return true;
27             }
28             conn.close();
29         }catch(SQLException e){
30             e.printStackTrace();
31         }
32         return false;
33     }
34 }

View Code

  数据库访问组件,DBPool.java (MySQL数据库)

 1 /**
 2  * 数据访问组件,数据库类型为MySQL,
 3  * 登陆数据库用户名:"root",
 4  * 密码:"root",
 5  * 数据库:"user",
 6  * 表:"UserInfo"
 7  */
 8 package login;
 9
10 import java.sql.*;
11
12 public class DBPool {
13     public Connection getConnection() throws SQLException{
14         Connection con=null;
15
16         try{
17             Class.forName("com.mysql.jdbc.Driver");
18             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8", "root", "root");
19         }catch(ClassNotFoundException e){
20             e.printStackTrace();
21         }
22         return con;
23     }
24 }

View Code

配置文件web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>Login_Proj</display-name>
 4   <servlet>
 5     <description>This is the description of my J2EE component</description>
 6     <display-name>This is the display name of my J2EE component</display-name>
 7     <servlet-name>LoginServlet</servlet-name>
 8     <servlet-class>login.LoginServlet</servlet-class>
 9   </servlet>
10
11   <servlet-mapping>
12     <servlet-name>LoginServlet</servlet-name>
13     <url-pattern>/LoginServlet</url-pattern>
14   </servlet-mapping>
15   <welcome-file-list>
16     <welcome-file>index.html</welcome-file>
17     <welcome-file>index.htm</welcome-file>
18     <welcome-file>index.jsp</welcome-file>
19     <welcome-file>default.html</welcome-file>
20     <welcome-file>default.htm</welcome-file>
21     <welcome-file>default.jsp</welcome-file>
22   </welcome-file-list>
23 </web-app>

View Code

应用实例 简单登陆系统:http://pan.baidu.com/s/1eQdAGqI

PS:使用环境Win7 64bit+MyEclipse Professional 2014+phpMyAdmin-2.10.3

转载于:https://www.cnblogs.com/cqq626/p/4319961.html

Java Web编程的主要组件技术——MVC设计模式相关推荐

  1. Java Web编程的主要组件技术——Hibernate入门

    参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...

  2. java web编程技术解题与实验指导_javaweb编程技术实验指导书

    javaweb编程技术实验指导书 <Java Web编程技术> 实 验 指 导 书 沈泽刚 编写2010 年 3 月目 录 实验一 简单的 Servlet 与 JSP .1 实验二 HTT ...

  3. java web编程技术上机实验_JavaWeb編程技术实验指导书.doc

    JavaWeb編程技术实验指导书 <Java Web编程技术> 实 验 指 导 书 沈泽刚 编写 2010年3月 目 录 实验一 简单的Servlet与JSP1 实验二 HTTP请求对象3 ...

  4. Java Web编程技术

    为什么80%的码农都做不了架构师?>>>    Java Web编程技术 该文档重点讲解如何自定义标签的开发,包括简单标签的开发.TLD文件.几种常见类型标签的开发. 对其内容详细阅 ...

  5. java web 编程技术 pdf_Java WEB编程技术.pdf

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbspJava Java WEB编程技术.pdf289页 本文档一共被 ...

  6. 【Java Web编程 十一】深入理解Servlet监听器

    上篇Blog详细介绍了Servlet的过滤器,了解到过滤器类似一个AOP的概念,这篇Blog就来学习下Servlet的监听器,了解下Java Web的第三大组件的用处是什么,又是怎么工作的. 监听器基 ...

  7. Java Web 编程入门知识

    Java SE 的内容基本都讲完了. 但是 Java一般用于网络编程, 就是所谓的web编程. Java SE讲的基本上都是本地程序的内容. 而Java web编程需要在两个程序中传输数据, 以后就是 ...

  8. 用php web编程作业,代做CSE2ISD作业、代做Web,php程序作业、代写Java/web编程作业、代写C/C++/Java留学生作业...

    代做CSE2ISD作业.代做Web,php程序作业.代写Java/web编程作业.代写C/C++/Java留学生作业 日期:2018-10-08 10:00 CSE2ISD – Information ...

  9. 学习(Java Web)编程技术要点及方向; 完成项目的要决

    本文亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理. 要Jar, 不要War:以小为主,以简为宝,集堆而成. 去繁取简 Spring Boot,明日之春. 集堆综合技术如 jHipster ...

最新文章

  1. POJ3666:Making the Grade——题解
  2. Java:写2个线程,其中一个线程打印1-52,另一个线程打印A-Z,打印顺序应该是12A34B56C...5152Z。
  3. ML之FE:数据随机抽样之利用pandas的sample函数对超大样本的数据集进行随机采样,并另存为csv文件
  4. JavaScript---设计模式之观察者模式
  5. 第九节:基于MVC5+AutoFac+EF+Log4Net的基础结构搭建
  6. c++中求前n项和(这代码不得不让我佩服)
  7. mysql中使用正则表达式,mysql中使用正则表达式查询
  8. python 学堂在线_最新网课答案2020学堂在线Python 交互式程序设计导论
  9. BZOJ3437 小P的牧场
  10. 3.1 Tensorflow基础知识
  11. 如何按照页面载入进度制作进度条??
  12. linux系统安装五笔输入法,Linux下安装五笔输入法
  13. JVM---类加载与字节码技术
  14. 数据结构与算法分析(C++语言版)_张琨版 第一章课后习题答案
  15. 洛谷P1359租用游艇
  16. VS2019+OpenCV4.5.1 环境配置+一劳永逸法(附下载地址)
  17. 读大学,大学四年培养的思维与工作能力,(读研,读博)
  18. 交通期刊JCR(2020)
  19. android studio 倒计时,Android studio 学习3:实现倒计时、画板
  20. 图表插件 - chart.js (柱状图) 学习总结

热门文章

  1. linux 用户行为审计update1
  2. mysql display size_mysql integer size 大小
  3. 高速学会Mac上托管代码到github(具体解释)
  4. linux增加自定义path和manpath
  5. ArcGIS API for Silverlight 点沿着线流动
  6. 08Spring_Spring和junit测试集成
  7. vsftp安装与配置(通过虚拟用户登录)
  8. 长期使用中型Access数据库的一点经验
  9. 永远记住9月26号这一天!
  10. MFCC特征参数提取(一)(基于MATLAB和Python实现)