? 简介
? Web 开发基础

–B/S结构程序
–请求响应机制
–HTML
–Servlet
–JSP
–Web 服务器

? 编程思想
–分层开发思想
–面向接口编程思想

? 设计模式
–DAO设计模式
–MVC设计模式
----------------------------Start---------------------------------------
? 简介
–因为Struts是Servlet+JSP的MVC解决方案,所以在学习Struts之前有必要了解如下Web开发的基础知识,使用Servlet+JSP如何编程,以及编程思想和设计模式中Web开发中应用
? 这对我们在学习Struts时是很有帮助的
? 我们要比较Servlet+JSP程序的MVC模式实现和Struts MVC的异同
? Servlet+JSP程序的弊端
? Struts的优势
? 程序开发的思想
? 设计模式的应用
? Web 开发基础
– B/S结构程序

? B/S结果程序是指,基于Browser(浏览器)/Server(服务器)的应用程序
? 相对C/S结构程序而言,B/S结构程序有如下优点:
– 基于网络
– 共享性好
– 客户端零维护
– 请求响应机制
? B/S程序的主要特征是请求(request)响应(response)机制
? 通常使用的协议是Http协议,该协议是无状态的
– HTML
? HTML的全称是Hyper Text Markup Language(超文本标记语言)
?基本结构是:


, <base>, <link>,  <meta> <br></HEAD> <br><BODY> <br>HTML 文件的正文写在这里... ... <br></BODY> <br></HTML> <br> <font color="#0000ff"><strong>– Servlet</strong></font> <br>? Servlet 是为动态创建Web工程而提供的编程接口 <br> <font color="#008000"><strong>MyServlet.java</strong></font> </div> <div> <a href="http://img.e-com-net.com/image/info3/2a2fcdc61f9d4e62a8df51b199baaf4a.jpg" target="_blank"><img title="p_w_picpath" style="border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-top-: ;border:1px solid black;" height="346" alt="Struts框架核心技术小小班_第1张图片" src="http://img.e-com-net.com/image/info3/2a2fcdc61f9d4e62a8df51b199baaf4a.jpg" width="554"></a> </div> <div> <font color="#008000"><strong>web.xml</strong></font> </div> <div> 设置servlet的申明和映射 </div> <div> <a href="http://img.e-com-net.com/image/info3/daeb0e6c89774b0b859ddcac564940ac.jpg" target="_blank"><img title="p_w_picpath" style="border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-top-: ;border:1px solid black;" height="352" alt="Struts框架核心技术小小班_第2张图片" src="http://img.e-com-net.com/image/info3/daeb0e6c89774b0b859ddcac564940ac.jpg" width="554"></a> </div> <div> 测试: </div> <div> <a href="http://img.e-com-net.com/image/info3/c9cfd26d0d9841049ed266f212c14b28.jpg" target="_blank"><img title="p_w_picpath" style="border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-top-: ;border:1px solid black;" height="343" alt="Struts框架核心技术小小班_第3张图片" src="http://img.e-com-net.com/image/info3/c9cfd26d0d9841049ed266f212c14b28.jpg" width="554"></a> </div> <div> <font color="#0000ff"><strong>– JSP</strong></font> <br>? 为了解决Servlet的开发效率问题而设计的开发语言 <br>? 底层执行还是Servlet </div> <div> <a href="http://img.e-com-net.com/image/info3/fff6435dc8064a7bab6e396252b70ff5.jpg" target="_blank"><img title="p_w_picpath" style="border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-top-: ;border:1px solid black;" height="343" alt="Struts框架核心技术小小班_第4张图片" src="http://img.e-com-net.com/image/info3/fff6435dc8064a7bab6e396252b70ff5.jpg" width="554"></a> <a href="http://img.e-com-net.com/image/info3/bce909cfd8bb4315a5586d28a4c4aadf.jpg" target="_blank"><img title="p_w_picpath" style="border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px; border-top-: ;border:1px solid black;" height="313" alt="Struts框架核心技术小小班_第5张图片" src="http://img.e-com-net.com/image/info3/bce909cfd8bb4315a5586d28a4c4aadf.jpg" width="554"></a> <font color="#008000"><strong>MyJsp_jsp.java</strong></font> </div> <div> <div style="border-right: #cccccc 1px solid; padding-right: 4px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 10pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; color: #000000; word-break: break-all; line-height: 16px; padding-top: 4px; border-bottom: #cccccc 1px solid; font-family: verdana,宋体; background-color: #eeeeee"> <font color="#0000ff">package</font> org.apache.jsp;     <br> <br> <font color="#0000ff">import</font> javax.servlet.*;     <br> <font color="#0000ff">import</font> javax.servlet.http.*;     <br> <font color="#0000ff">import</font> javax.servlet.jsp.*;     <br> <font color="#0000ff">import</font> java.util.*;     <br> <br> <font color="#0000ff">public</font> <font color="#0000ff">final</font> <font color="#0000ff">class</font> MyJsp_jsp <font color="#0000ff">extends</font> org.apache.jasper.runtime.HttpJspBase     <br>         <font color="#0000ff">implements</font> org.apache.jasper.runtime.JspSourceDependent {     <br> <br>     <font color="#0000ff">private</font> <font color="#0000ff">static</font> <font color="#0000ff">final</font> JspFactory _jspxFactory = JspFactory.getDefaultFactory();     <br> <br>     <font color="#0000ff">private</font> <font color="#0000ff">static</font> java.util.List _jspx_dependants;     <br> <br>     <font color="#0000ff">private</font> javax.el.ExpressionFactory _el_expressionfactory;     <br>     <font color="#0000ff">private</font> org.apache.AnnotationProcessor _jsp_annotationprocessor;     <br> <br>     <font color="#0000ff">public</font> Object getDependants() {     <br>         <font color="#0000ff">return</font> _jspx_dependants;     <br>    }     <br> <br>     <font color="#0000ff">public</font> <font color="#0000ff">void</font> _jspInit() {     <br>        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();     <br>        _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor. <font color="#0000ff">class</font>.getName());     <br>    }     <br> <br>     <font color="#0000ff">public</font> <font color="#0000ff">void</font> _jspDestroy() {     <br>    }     <br> <br>     <font color="#0000ff">public</font> <font color="#0000ff">void</font> _jspService(HttpServletRequest request, HttpServletResponse response)     <br>                 <font color="#0000ff">throws</font> java.io.IOException, ServletException {     <br> <br>        PageContext pageContext = <font color="#0000ff">null</font>;     <br>        HttpSession session = <font color="#0000ff">null</font>;     <br>        ServletContext application = <font color="#0000ff">null</font>;     <br>        ServletConfig config = <font color="#0000ff">null</font>;     <br>        JspWriter out = <font color="#0000ff">null</font>;     <br>        Object page = <font color="#0000ff">this</font>;     <br>        JspWriter _jspx_out = <font color="#0000ff">null</font>;     <br>        PageContext _jspx_page_context = <font color="#0000ff">null</font>;     <br> <br>         <font color="#0000ff">try</font> {     <br>            response.setContentType( <font color="#800000">"text/html;charset=ISO-8859-1"</font>);     <br>            pageContext = _jspxFactory.getPageContext( <font color="#0000ff">this</font>, request, response,     <br>                                     <font color="#0000ff">null</font>, <font color="#0000ff">true</font>, 8192, <font color="#0000ff">true</font>);     <br>            _jspx_page_context = pageContext;     <br>            application = pageContext.getServletContext();     <br>            config = pageContext.getServletConfig();     <br>            session = pageContext.getSession();     <br>            out = pageContext.getOut();     <br>            _jspx_out = out;     <br> <br>            out.write('\r');     <br>            out.write('\n');     <br> <br>String path = request.getContextPath();     <br>String basePath = request.getScheme()+ <font color="#800000">":<font color="#008000">//"+request.getServerName()+":"+request.getServerPort()+path+"/";    </font><br><br>            out.write(<font color="#800000">"\r\n"</font>);    <br>            out.write(<font color="#800000">"\r\n"</font>);    <br>            out.write(<font color="#800000">"<!DOCTYPE HTML PUBLIC \"-<font color="#008000">//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");    </font><br>            out.write(<font color="#800000">"<html>\r\n"</font>);    <br>            out.write(<font color="#800000">"    <head>\r\n"</font>);    <br>            out.write(<font color="#800000">"        <base href=\""</font>);    <br>            out.print(basePath);    <br>            out.write(<font color="#800000">"\">\r\n"</font>);    <br>            out.write(<font color="#800000">"        \r\n"</font>);    <br>            out.write(<font color="#800000">"        <title>My JSP 'MyJsp.jsp' starting page\r\n");    
            out.write("        \r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\t        \r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\t\r\n");    
            out.write("\r\n");    
            out.write("    \r\n");    
            out.write("    \r\n");    
            out.write("    \r\n");    
            out.write("        This is my JSP page.
\r\n"
);    
            out.write("    \r\n");    
            out.write("\r\n");    
        } catch (Throwable t) {    
            if (!(t instanceof SkipPageException)){    
                out = _jspx_out;    
                if (out != null && out.getBufferSize() != 0)    
                    try { out.clearBuffer(); } catch (java.io.IOException e) {}    
                if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);    
            }    
        } finally {    
            _jspxFactory.releasePageContext(_jspx_page_context);    
        }    
    }    
}


– Web 服务器
? Web工程的运行容器,如Tomcat
Struts框架核心技术小小班_第6张图片
Struts框架核心技术小小班_第7张图片
? 编程思想
–分层开发思想
? 软件的层次结构可以分为四层:
–表现层
–控制层
–业务逻辑层
–数据逻辑层(持久层)
–面向接口编程思想
? 在编程中将业务逻辑抽象出接口,以供上次调用
? 依赖抽象(接口),而非具体(接口实现)的编程思想,又称之为控制反转(Inversion of Control)
 
? 设计模式
–DAO设计模式
? DAO设计模式
–DAO的全称是:Data Access Object,数据访问对象。
–使用DAO设计模式,来封装数据库持久层的所有操作(CRUD),使低级的数据逻辑和高级的业务逻辑分离,达到解耦合的目的。
? 一个典型的DAO实现有如下的组件:
–一个 DAO 接口
–一个实现了 DAO 接口的具体类
–一个 DAO 工厂类
–数据传输对象(有时称为值对象)
?以维护一个客户信息为例,具体组件如下所示:
–CustomerDao 接口
–Customer 值对象(VO)
–CustomerDaoImpl(接口的具体实现类)
–CustomerFactory(工厂类,实例化用)
–MVC设计模式
? MVC的全称是:Model(模型) View(视图) Controller( 控制器)
? Model
–对应业务逻辑层、数据逻辑层
–由接口及其实现类充当
? View
–对应表现层
–由HTML页面、JSP页面、Tag(标签)等充当
? Controller
–对应控制层
–由Servlet 或Struts中的Action等充当
 
? 编程思想和设计模式的具体应用
–图示
自定义MVC
Struts框架核心技术小小班_第8张图片
 
?实例
–以添加客户和查询客户列表为例来说明上述内容
–程序运行结果
Struts框架核心技术小小班_第9张图片
数据库设置  Struts框架核心技术小小班_第10张图片
CustomerDao.java
  Struts框架核心技术小小班_第11张图片
CustomerDaoImpl.java
package com.redking.dao.impl;    

import java.sql.Connection;    
import java.sql.PreparedStatement;    
import java.sql.ResultSet;    
import java.sql.SQLException;    
import java.sql.Statement;    
import java.util.ArrayList;    
import java.util.List;    

import com.redking.dao.CustomerDao;    
import com.redking.util.ConnectionUtil;    
import com.redking.util.SQLConstants;    
import com.redking.vo.Customer;    

public class CustomerDaoImpl implements CustomerDao,SQLConstants{    

         public List listCustomer() {    
                ConnectionUtil cu = new ConnectionUtil();    
                Connection conn = cu.getConnection();    
                 //conn.setAutoCommit(false);设置默认不自动提交    
                 //Statement stmt;静态添加    
                 //PrepareStatement pstmt;动态添加    
                List list = new ArrayList();    
                 try {    
                        Statement stmt = conn.createStatement();    
                        ResultSet rs = stmt.executeQuery(QUERY_CUSTOMER_SQL);    
                         while(rs.next()){    
                                 int id = rs.getInt(1);    
                                String name = rs.getString(2);    
                                String email = rs.getString(3);    
                                Customer c = new Customer();    
                                c.setId(id);    
                                c.setName(name);    
                                c.setEmail(email);    
                                list.add(c);    
                        }    
                         return list;    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                } finally{    
                         try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
                 return null;    
        }    

         public void save(Customer c) {    
                ConnectionUtil cu = new ConnectionUtil();    
                Connection conn = cu.getConnection();    
                 //conn.setAutoCommit(false);设置默认不自动提交    
                 //Statement stmt;静态添加    
                 //PrepareStatement pstmt;动态添加    
                PreparedStatement pstmt = null;    
                 try {    
                        pstmt = conn.prepareStatement(ADD_CUSTOMER_SQL);    
                        pstmt.setString(1, c.getName());    
                        pstmt.setString(2, c.getEmail());    
                        pstmt.executeUpdate();    
                         //conn.commit();设置强制提交    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                } finally{    
                         try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
        }    

}

CustomerServlet.java
package com.redking.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    
import java.util.List;    

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

import com.redking.dao.CustomerDao;    
import com.redking.dao.impl.CustomerDaoImpl;    
import com.redking.vo.Customer;    

public class CustomerServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public CustomerServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet.
    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet.
    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet.
    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                 /*    
                //响应用户请求    
                String name = request.getParameter("name");    
                String email = request.getParameter("email");    
                //调用后台逻辑-dao    
                CustomerDao dao = new CustomerDaoImpl();    
                Customer c = new Customer();    
                c.setName(name);    
                c.setEmail(email);    
                dao.save(c);    
                //跳转其他页面    
                //RequestDispatcher    
                request.getRequestDispatcher("/pages/Customer.jsp").forward(request,response);    
                */
    
                String methodName = request.getParameter( "methodName");    
                 if(methodName!= null&&methodName.equals( "save")){    
                        save(request,response);    
                } else{    
                        list(request,response);    
                }    
        }    
         public void save(HttpServletRequest request, HttpServletResponse response)    
         throws ServletException, IOException {    

                 //响应用户请求    
                String name = request.getParameter( "name");    
                String email = request.getParameter( "email");    
                 //调用后台逻辑-dao    
                CustomerDao dao = new CustomerDaoImpl();    
                Customer c = new Customer();    
                c.setName(name);    
                c.setEmail(email);    
                dao.save(c);    
                 //跳转其他页面    
                 //RequestDispatcher    
                 //request.getRequestDispatcher("/pages/Customer.jsp").forward(request,response);    
                list(request,response);    
        }    
         public void list(HttpServletRequest request, HttpServletResponse response)    
         throws ServletException, IOException {    

                 //响应用户请求    
                String name = request.getParameter( "name");    
                String email = request.getParameter( "email");    
                 //调用后台逻辑-dao    
                CustomerDao dao = new CustomerDaoImpl();    
                List list = dao.listCustomer();    
                request.setAttribute( "CustomerList", list);    
                 //跳转其他页面    
                 //RequestDispatcher    
                request.getRequestDispatcher( "/pages/Customer.jsp").forward(request,response);    
        }    

         /**    
         * Initialization of the servlet.
    
         *    
         * @throws ServletException if an error occurs    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    

}
ConnectionUtilTest.java
Struts框架核心技术小小班_第12张图片
CustomerDaoImplTest.java
Struts框架核心技术小小班_第13张图片
ConnectionUtil.java
Struts框架核心技术小小班_第14张图片
  SQLConstants.java
Struts框架核心技术小小班_第15张图片
Customer.java
Struts框架核心技术小小班_第16张图片
DBConfig.properties
Struts框架核心技术小小班_第17张图片
Customer.jsp
Struts框架核心技术小小班_第18张图片 
<%@ page language= "java" import= "java.util.*,com.redking.vo.*" pageEncoding= "gbk"%>    
<%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix="redking" %>    
<%    
String path = request.getContextPath();    
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
%>    

"-//W3C//DTD HTML 4.01 Transitional//EN">    
    
        
        "<%=basePath%>"
>    
        My JSP 'Customer.jsp' starting page    
        "pragma"
content="no-cache">    
        "cache-control" content="no-cache">    
        "expires" content="0">        
        "keywords" content="keyword1,keyword2,keyword3">    
        "description" content="This is my page">    
            

        
        
        
"f1" id="f1" action="<%=path %>/servlet/CustomerServlet?methodName=save" method="post">    
            "0">    
                    
                        
                        
                    
                    
                        
                        
                    
                    
                        
                    
            
Name:"text" name="name" >
Email:"text" name="email" >
"2" align="center">"submit" value="保存">
    
        
    
        
    
            
            
        <%--    
        <%    
                List list = (List) request.getAttribute("CustomerList");    
                //遍列LIST    
                for(int i=0;i                        Customer c = (Customer) list.get(i);    
                        out.println("
");    
                        out.println("");    
                        out.println("");    
                        out.println("");    
                        out.println("");    
                }    
         %>    
            --%>    
            <%--使用标准标签 --%>    
            "c" items="${CustomerList}">    
                    
    
                                
                                
                                
                        
                
        
IDNameEmail
");    
                        out.println(c.getId());    
                        out.println("
");    
                        out.println(c.getName());    
                        out.println("
");    
                        out.println(c.getEmail());    
                        out.println("
    
                                    ${c.id }    
                            
    
                                    ${c.name }    
                            
    
                                    ${c.email }    
                            
    
        


测试:
Struts框架核心技术小小班_第19张图片
Struts框架核心技术小小班_第20张图片 Struts框架核心技术小小班_第21张图片
Struts框架核心技术小小班_第22张图片 
----------------------------------END-------------------------------------