JSP简介

前言

   

知识点
1.JSP是什么
  java  server   page,java服务器端页面技术。其主要作用在服务器端动态生成页面, 其组成java代码和html,
2.JSP的组成
   html:包括css/js
   java
       java代码段
              <%java代码%>
       表达式
             <%=%>
3.隐含对象
   对象不需要创建,可以直接调用。   out  、 request   、response;
4.指令
   servelt引擎在将.jsp文件转换成servelt源文件时,所做的一些额外的处理。
   语法:
   <%@指令名  属性%>
   常用的一些指令
   <%@page  import="java.util.*"%>
   <%@page  pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
   pageEncoding: jsp文件在保存的时候需要按照什么样的编码格式来保存。
   contentType:等同于response 的setContentType()
5.JSP的执行
  jsp引擎 (servelt引擎 )会将jsp文件转换为一个servelt源文件,再去  调用其service方法
6.转换
  html:放到service中,使用write输出
  java:   放到service中,照搬
 表达式:放到service中,使用print输出
7.转发
   a.同一个应用中,一个组件完成部分请求,将未完成的请求转交给另外一个组件继续处理。   在这些组件之间HttpServeltRequest对象共享。
   b.转发步骤:
    //绑定数据到request对象中
    request.setAttribute(String name, Object obj);
    Object  request.getAtrribute(String name);
    如果name没有找到就返回 null
    //创建转发器
    RequestDispatcher   rd=request.getRequestDispatcher("emplist.jsp");
    rd.forward(request, response);
    也可以这样写
     request.getRequestDispatcher("emplist.jsp").forward(request, response);
   
    c.转发和重定向的区别
      1).重定向是这件事已经完成,转发是这件事完成了部分。
      2).重定向的地址是任意的,转发是在同一程序内部组件之间。
      3).重定向地址栏中的地址会发生改变,而转发不会改变。
     
8.路径问题
   (1)链接   (2)表单提交    (3)重定向    (4)转发
   相对路径:不以“/”开头的路径
   绝对路径:以“/”开头的路径。
   以(1)、(2)、(3)的情况,绝对路径从应用名开始,(4)绝对路径从应用名之后开始
               
            
              
  

正文

 

JSP


 Java Server Page; JavaEE 规范


servlet的不足


 servlet不善于做页面显示; 每次修改程序修改配置都必须重新部署重启服务器


Jsp的特点


 善于做页面显示; 修改程序修改配置不需要重新部署重启服务器; 是一个特殊的servlet; 在HTML标签中嵌套java代码; 处理业务逻辑,程序的可读性不好


 
定义书写Jsp程序


 文件的后缀名 .jsp

 

  在jsp中书写java代码


 1.脚本


  脚本格式 <% java代码   %>


  a.普通脚本


   <%    %>

   特点:

书写java代码
       不能在普通脚本中定义方法
       不能在普通脚本中定义成员变量,定义的变量是局部变量
    java语句一定要以分号结束
    普通脚本中的java语句会定义在转换后的 servlet中的service方法中


  b.定义脚本


   <%!   %>
   特点:

定义方法,成员变量,不能书写普通java语句
   定义脚本中的java语句定义在转换后的servlet中的类体中。
   
     c.输出脚本


      <%= %>
      特点:不能定义方法,不能定义变量,java语句不能含有分号
      输出脚本会将java语句定义在转换后的 servlet的service方法中的out.print(java语句);
      
  注意:

脚本之间不能嵌套
   脚本中不能嵌套HTML标签
   HTML中必须合理嵌套脚本
   


 Jsp中常用的指令


  1.page指令


   设置当前jsp页面的一些基本属性
  <%@page
   *language="java" 设置jsp页面融合的编程语言
   *pageEncoding="编码方式"  设置request请求的编码方式
    request.setCharacterEncoding();
   *contentType="文件类型和编码方式" 设置当前输出内容的文件类型和编码方式
    response.setContentType()
   *import="类的全限定名" 引入依赖的类所对应的包
   
   session="true/false" 设置当前页面是否支持session
   extends=""   设置当前jsp的父类
   buffer = "数字"  设置当前jsp缓存容量
   isELIgnored = "true/false"  表示当前页面是否支持EL表达式
   isErrorPage = "true/false"  表示当前页面是否是错误信息展示页面
   errorPage = "目标页面"   设置错误信息展示页面
   info = ""    当前jsp的描述信息
   autoFlush = "true/false"  当前页面是否自动清理缓存
   isThreadSafe = "true/false" 设置当前页面是否线程安全   
  %>


  
  2.taglib指令


  <%@taglib
   prefix=""    设置引入标签库的标签前缀
   uri=""    设置被引入的标签库的路径
  %>


  
  3.include指令


   <%@include file="" 设置被包涵的页面的路径 %>   


  
 Jsp中的内置对象


  request   请求对象 封装请求信息


  response  响应对象

封装响应信息

1.设置HTTP标头的方法

 void addCookie(Cookie cookie)
          Adds the specified cookie to the response.
 void addDateHeader(java.lang.String name, long date)
          Adds a response header with the given name and date-value.
 void addHeader(java.lang.String name, java.lang.String value)
          Adds a response header with the given name and value.
 void addIntHeader(java.lang.String name, int value)
          Adds a response header with the given name and integer value.
 boolean containsHeader(java.lang.String name)
          Returns a boolean indicating whether the named response header has already been set.
 void setDateHeader(java.lang.String name, long date)
          Sets a response header with the given name and date-value.
 void setHeader(java.lang.String name, java.lang.String value)
          Sets a response header with the given name and value. 例如设置浏览器无缓冲:setHeader("pragma"."no-cache");或者setHeader("cache-control"."no-cache");

注:

60秒重新加载本页面

setIntHeader("refresh",60);

3秒后浏览器加载新的页面

setHeader("referesh","3;URL=http://www.baidu.com");


 void setIntHeader(java.lang.String name, int value)
          Sets a response header with the given name and integer value.
 java.lang.String encodeURL(java.lang.String url)
          Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged.

2.重定向解析

 void sendRedirect(java.lang.String location)
          Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.
  java.lang.String encodeRedirectURL(java.lang.String url)
          Encodes the specified URL for use in the sendRedirect method or, if encoding is not needed, returns the URL unchanged.

3.设定数据内容的类型和长度

 void setContentType(java.lang.String type)
          Sets the content type of the response being sent to the client, if the response has not been committed yet.这是实现ServletResponse接口的方法。

 

4.设定状态吗的方法

 void sendError(int sc)
          Sends an error response to the client using the specified status code and clears the buffer.
 void sendError(int sc, java.lang.String msg)
          Sends an error response to the client using the specified status and clears the buffer.
 void setStatus(int sc)
          Sets the status code for this response.

注:状态码常数

static int SC_ACCEPTED
          Status code (202) indicating that a request was accepted for processing, but was not completed.
static int SC_BAD_GATEWAY
          Status code (502) indicating that the HTTP server received an invalid response from a server it consulted when acting as a proxy or gateway.
static int SC_BAD_REQUEST
          Status code (400) indicating the request sent by the client was syntactically incorrect.
static int SC_CONFLICT
          Status code (409) indicating that the request could not be completed due to a conflict with the current state of the resource.
static int SC_CONTINUE
          Status code (100) indicating the client can continue.
static int SC_CREATED
          Status code (201) indicating the request succeeded and created a new resource on the server.
static int SC_EXPECTATION_FAILED
          Status code (417) indicating that the server could not meet the expectation given in the Expect request header.
static int SC_FORBIDDEN
          Status code (403) indicating the server understood the request but refused to fulfill it.
static int SC_FOUND
          Status code (302) indicating that the resource reside temporarily under a different URI.
static int SC_GATEWAY_TIMEOUT
          Status code (504) indicating that the server did not receive a timely response from the upstream server while acting as a gateway or proxy.
static int SC_GONE
          Status code (410) indicating that the resource is no longer available at the server and no forwarding address is known.
static int SC_HTTP_VERSION_NOT_SUPPORTED
          Status code (505) indicating that the server does not support or refuses to support the HTTP protocol version that was used in the request message.
static int SC_INTERNAL_SERVER_ERROR
          Status code (500) indicating an error inside the HTTP server which prevented it from fulfilling the request.
static int SC_LENGTH_REQUIRED
          Status code (411) indicating that the request cannot be handled without a defined Content-Length.
static int SC_METHOD_NOT_ALLOWED
          Status code (405) indicating that the method specified in the Request-Line is not allowed for the resource identified by the Request-URI.
static int SC_MOVED_PERMANENTLY
          Status code (301) indicating that the resource has permanently moved to a new location, and that future references should use a new URI with their requests.
static int SC_MOVED_TEMPORARILY
          Status code (302) indicating that the resource has temporarily moved to another location, but that future references should still use the original URI to access the resource.
static int SC_MULTIPLE_CHOICES
          Status code (300) indicating that the requested resource corresponds to any one of a set of representations, each with its own specific location.
static int SC_NO_CONTENT
          Status code (204) indicating that the request succeeded but that there was no new information to return.
static int SC_NON_AUTHORITATIVE_INFORMATION
          Status code (203) indicating that the meta information presented by the client did not originate from the server.
static int SC_NOT_ACCEPTABLE
          Status code (406) indicating that the resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
static int SC_NOT_FOUND
          Status code (404) indicating that the requested resource is not available.
static int SC_NOT_IMPLEMENTED
          Status code (501) indicating the HTTP server does not support the functionality needed to fulfill the request.
static int SC_NOT_MODIFIED
          Status code (304) indicating that a conditional GET operation found that the resource was available and not modified.
static int SC_OK
          Status code (200) indicating the request succeeded normally.
static int SC_PARTIAL_CONTENT
          Status code (206) indicating that the server has fulfilled the partial GET request for the resource.
static int SC_PAYMENT_REQUIRED
          Status code (402) reserved for future use.
static int SC_PRECONDITION_FAILED
          Status code (412) indicating that the precondition given in one or more of the request-header fields evaluated to false when it was tested on the server.
static int SC_PROXY_AUTHENTICATION_REQUIRED
          Status code (407) indicating that the client MUST first authenticate itself with the proxy.
static int SC_REQUEST_ENTITY_TOO_LARGE
          Status code (413) indicating that the server is refusing to process the request because the request entity is larger than the server is willing or able to process.
static int SC_REQUEST_TIMEOUT
          Status code (408) indicating that the client did not produce a request within the time that the server was prepared to wait.
static int SC_REQUEST_URI_TOO_LONG
          Status code (414) indicating that the server is refusing to service the request because the Request-URI is longer than the server is willing to interpret.
static int SC_REQUESTED_RANGE_NOT_SATISFIABLE
          Status code (416) indicating that the server cannot serve the requested byte range.
static int SC_RESET_CONTENT
          Status code (205) indicating that the agent SHOULD reset the document view which caused the request to be sent.
static int SC_SEE_OTHER
          Status code (303) indicating that the response to the request can be found under a different URI.
static int SC_SERVICE_UNAVAILABLE
          Status code (503) indicating that the HTTP server is temporarily overloaded, and unable to handle the request.
static int SC_SWITCHING_PROTOCOLS
          Status code (101) indicating the server is switching protocols according to Upgrade header.
static int SC_TEMPORARY_REDIRECT
          Status code (307) indicating that the requested resource resides temporarily under a different URI.
static int SC_UNAUTHORIZED
          Status code (401) indicating that the request requires HTTP authentication.
static int SC_UNSUPPORTED_MEDIA_TYPE
          Status code (415) indicating that the server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.
static int SC_USE_PROXY
          Status code (305) indicating that the requested resource MUST be accessed through the proxy given by the Location field.


  page   当前页面对象

如同java中的this


  pageContext  当前页面的上下文环境对象


  session   会话对象

可用于实现购物车功能


  application  当前应用的配置环境对象

可用于实现网站的计数器功能

application对象被所有的客户共享。


  config   当前页面的配置对象

手动配置一个jsp页面

使用Servlet标签的子标签<jsp-file>来指定jsp页面文件和Servlet-mapping标签将该jsp页面映射到指定的URL。


  exception  异常对象

封装异常处理信息;只有在%@page isErrorPage="true"%的页面才可以使用exception对象。


  out    输出流对象

HTML的标准输出;out对象对应的类为javax.servlet.jsp
Class JspWriter

 

Class JspWriter的类结构

java.lang.Object
  java.io.Writer
      javax.servlet.jsp.JspWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable
Direct Known Subclasses:
BodyContent

 

Class JspWriter的常用API

out 对象的主要方法分为两类

1.向浏览器输出数据的常用方法如下

abstract  void close()
          Close the stream, flushing it first 关闭输出流
abstract  void print(java.lang.String s)
          Print a string.
abstract  void println()
          Terminate the current line by writing the line separator string.
abstract  void println(boolean x)
          Print a boolean value and then terminate the line.

2.对缓冲区进行操作的常用方法如下

abstract  void clear()
          Clear the contents of the buffer.
abstract  void clearBuffer()
          Clears the current contents of the buffer.
 int getBufferSize()
          This method returns the size of the buffer used by the JspWriter.
abstract  int getRemaining()
          This method returns the number of unused bytes in the buffer.

abstract  void flush()
          Flush the stream.
boolean isAutoFlush()
          This method indicates whether the JspWriter is autoFlushing

  
  四大作用域


   能够存储一些临时数据
   
   pageContext    当前页面有效
   request   同一个请求
   session   同一个会话
   application  同一个web应用
  
   setAttribute("key",);
   getAttribute("key");

   
 JSP常用动作  


   <jsp:名称 ></jsp:名称>


   1.在某个作用域范围创建对象


   <jsp:useBean id="uid" class="com.zhongx.jsp.User" scope="page"></jsp:useBean>


    id属性:表示创建的对应对象的引用,不能重复
    class属性:表示需要创建的对象的类路径
    scope属性:生成对象的作用域范围;     page    当前页面范围;     request 当前请求范围;     session 当前会话范围;     application 当前应用范围


   2.设置对象的属性


   <jsp:setProperty property="name" name="uid" value="hello"/>
     name属性:设置需要取值的对象
     property属性 :设置需要设值的属性名 
     value属性 :属性的取值


   3.获得对象属性值


   <jsp:getProperty property="name" name="uid"/> 
     name属性:设置需要取值的对象
     property属性 :设置需要设值的属性名 
     
   4.页面跳转


   类似servlet中的forward转发
   <jsp:forward page="testAction2.jsp"></jsp:forward>
       page属性: 目标页面的地址
   forward之后不能写代码
   
   5.设置参数


    作为跳转的子标签,在两个页面转发过程中设置参数
    <jsp:param value="test" name="key"/>
     name属性:参数的名称
     vlaue属性:参数的取值
    获得传递的参数
    request.getParameter("key");
    <jsp:forward page="testAction2.jsp">
     <jsp:param value="test" name="key"/>
    </jsp:forward>
    
   6.包涵页面


    动态包涵
    <jsp:include page="testIncludeAction.jsp"></jsp:include>
     page属性:设置被包涵的页面
    通过JspRuntimeLibrary.include方法将被包涵的页面动态的包涵
   
      
    动态包涵与静态包涵
     动态包涵是通过JSP的include动作来实现,
     他在转换之后的servlet代码中通过JspRuntimeLibrary.include方法实现
     在work目录中会将包涵的页面与被包涵的页面都生成对应的转换文件和字节码文件
     
     静态包涵是通过JSP的include指令来实现
     在转换后的servlet中直接将被包涵的页面的源码包涵进来,然后一同编译执行输出
     在work目录中被包涵的页面不会有单独的转换servlet和字节码文件
    
    传递参数
    <jsp:include page="testIncludeAction.jsp">
     <jsp:param value="test" name="test"/>
    </jsp:include>
    获得参数
    request.getParameter("test");


    
  EL(Expression Language,表达式语言)


   帮助程序员更加灵活方便快捷的获得对应的值
   语法:${}
    
   <h1>EL</h1>
   <h2>简单计算</h2>
   <h2>1+1=${1+1 }</h2>
   <h2>"1"+1=${"1"+1 }</h2>
   <h2>4>3=${4>3 }</h2>
   <h2>true&&false=${true&&false }</h2>
   <h2>1>3?"好大":"好小"=${1>3?"好大":"好小" }</h2>
   <h2>从作用域获得对象</h2>
   <h2>page:${pageScope.user1 }</h2>
   <h2>request:${requestScope.user2 }</h2>
   <h2>session:${sessionScope.user3 }</h2>
   <h2>application:${applicationScope.user4 }</h2>
   <!-- 直接获得会从pageContext---request---session---application 按照从小到大范围查找 -->
   <h2>直接获得 :${user1 }</h2>
   <h2>直接使用pageContext内置对象调用API</h2>
   <h2>url:${pageContext.request.serverPort}</h2>
   <h2>接收页面参数</h2>
   <h2>参数name=${param.name }</h2>
   <h2>参数name是否为空${empty param.name }</h2>
   <h2>cookie=${cookie.JSESSIONID.value}</h2> 

   
  JSTL(Java standard tag  library,java标准标签库)


   标签:通过标签这种形式来实现一组java类所表现的功能


   1.引入标签库的实现类以及配置文件(.tld)


    JSTL1.1.jar Standard.jar
    JSTL1.2


   2.在JSP页面引入标签库


    向作用域中设置键值对
    <c:set var="name" value="hello" scope="application"></c:set>
    <c:set var="tag" scope="session">
     <h1>hello</h1>
     <h1>hello</h1>
     <h2>hello</h2>
     <h1>hello</h1>
     <h3>hello</h3>
    
    </c:set>


    移除键值对
    <c:remove var="name"/>


    输出值
    <c:out value="${name}"></c:out>
    设置url
    <c:url value="${url }"></c:url>
    页面重定向
    <c:redirect url="http://localhost:8899/helloJSP129/testEL.jsp"></c:redirect>
    分支流程控制
    <c:if test="${5>param.num}">
     <h1>你真好</h1>
    </c:if>
    
    类似java中的switch/case
    <c:choose>
      <c:when test="${param.age<18}">
       <h1>你未成年</h1>
      </c:when>
      <c:when test="${param.age>=18 && param.age<60}">
       <h1>小青年</h1>
      </c:when>
      <c:when test="${param.age>=60 && param.age<100}">
       <h1>你该享受生活了</h1>
      </c:when>
      <c:otherwise>
       <h1>你成仙了</h1>
      </c:otherwise>
    </c:choose>


     
   自定义标签


    标签功能---->实现类
      ----->配置文件  .tld
     //标签库的属性和描述信息
     标签库的描述
      <description>JSTL 1.1 core library</description>
       标签库显示的名称
      <display-name>JSTL core</display-name>
       标签库的版本
      <tlib-version>1.1</tlib-version>
       标签库的别名
      <short-name>c</short-name>
       表示当前标签库的引用路径
       *不能重复
      <uri>http://java.sun.com/jsp/jstl/core</uri>
     
     验证
      <validator>
        <description>
            Provides core validation features for JSTL tags.
        </description>
        <validator-class>
            org.apache.taglibs.standard.tlv.JstlCoreTLV
        </validator-class>
      </validator>
     
     //配置标签
      <tag>
       对当前标签的描述
        <description>
            Catches any Throwable that occurs in its body and optionally
            exposes it.
        </description>
             标签名称,不能重复
        <name>catch</name>
              表示标签对应的功能实现类
        <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>
              表示当前标签体的约束
              JSP   表示标签体中可以是jsp中的任何内容
              scriptless    表示标签体中不能包涵脚本
              tagdependent  当前标签体是否依赖标签本身
              empty   表示没有标签体
        <body-content>JSP</body-content>
              在当前标签中配置属性
        <attribute>
         属性的描述信息
            <description>
      Name of the exported scoped variable for the
      exception thrown from a nested action. The type of the
      scoped variable is the type of the exception thrown.
            </description>
                          属性名称
            <name>var</name>
             配置属性的特性(是否必须出现)
            <required>false</required>
                           表示是否支持EL动态取值
                         (Runtime Expression Value)
            <rtexprvalue>false</rtexprvalue>
        </attribute>
      </tag>
     
      1.书写功能实现类


       implements BodyTag       
       
      

     
      方法:


       doStartTag()
       doEndTag()
       
      常数:


       SKIP_BODY  忽略标签体
       SKIP_PAGE  忽略结束标签之后的其他标签
       EVAL_BODY_INCLUDE 显示标签体内容
       EVAL_BODY_BUFFERED  缓存标签体内容
       EVAL_BODY_AGAIN  重复执行标签体
       EVAL_PAGE   标签结束后其他标签继续执行
    
      2.配置配置文件
              定义tld文件参照myTag.tld

3.自定义分页标签的代码示例

1)page.tld配置文件

 

2)com.zhongxin.backstage.bodyTag.TestPage  分页标签的处理类

 

3)在页面使用自定义标签

4)属性的参数的实体类Page

在tld配置文件中对应的属性:

<attribute>

  <description>p</description>
  <name>p</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>

 

 


  
  MVC


   M---Model  模型层
   V---View   视图层
   C---Controller 控制层
   
   模型层:JavaBean实现    封装数据对象,处理业务逻辑实现
   视图层:Jsp实现    获取数据,将数据显示
   控制层:Servlet实现    接收视图层参数,调用业务逻辑,将返回结果传递给视图层
   
   
   

总结

jsp


 (1)jsp是什么?
 java  server   page,java服务器端页面技术。其主要作用在服务器端动态生成页面,其组成java代码和html.
 (2)jsp的组成?
  A. html:包括css/js 
  B.java代码
   java代码段 <%java代码%>
            表达式 <%=%>
   jsp声明:<%!   %> 
  C.jsp隐含对象
   对象不需要创建,可以直接调用。   out、   request、   response、   session、   application、   config: ServletConfig实例 、  pageContext:(  PageContext实例    jsp引擎会为每一个jsp实例分配唯一的一个PageContext实例;    可以放置数据:     PageContext.setAttribute(String,Object)     Object PageContext.getAttribute(String) ;    访问其它八个隐含对象)
   page    表示jsp实例本身
   exception    jsp在运行过程当中,产生的错误会封装到该对象上。
  D、指令
             servelt引擎在将.jsp文件转换成servelt源文件时,所做的一些额外的处理。
             语法:
              <%@指令名  属性%>
              常用的一些指令
               <%@page  import="java.util.*"%>
               <%@page  pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
                pageEncoding: jsp文件在保存的时候需要按照什么样的编码格式来保存。
                contentType:等同于response 的setContentType()
   errorPage:指定错误处理页面
   isErrorPage:当前页面是否为错误处理页面,缺省值是false
   session:true(缺省)/false,当前页面是否支持session。
   isELIgnored:true(缺省)/false,是否忽略el表达式。
   <%@include file=""%>
   <%@taglib prefix="" uri=""%>:引入标签。
  E、活动元素
   在jsp已经运行时,让jsp引擎做一些额外的处理。
   <jsp:include/>:一个jsp在运行过程当中,去调用   另外一个jsp。
  F、注释:
   <!--
    注释内容
   -->
   注释内容如果是java代码,会执行,不会
   在浏览器端输出。
   
   <%--
    注释内容
   --%>
   注释内容如果是java代码,不会执行,不会
   在浏览器端输出。
 (3)jsp如何执行的?
  jsp引擎会将jsp文件转换为一个servelt源文件,再去   调用其service方法
  
 (4)如何转换成servlet源文件


  A、html --->放到service()方法里,使用out.write()输出。
  B、java代码-->放到service()方法里,照搬。
  C、jsp表达式--->放到serivce()方法里,使用print()输出。
  
servlet线程安全问题?


    当请求到达服务后,服务求就会创建一个新的线程来处理请求,有可能出现  多个请求访问同一个servlet实例,如果操作servelt属性,就有可能出现    线程安全问题。
         1.加锁
              加上synchronized的代码块。
         2.让servlet实现SingleThreadModel接口
            SingleThreadModel 是一个标识接口,实现SingleThreadModel接口的作用, 服务器会给每一个请求都创建一个servlet的实例。(不建议使用)
         3.尽量避免操作修改servlet的属性。    

 

   mvc


(1)什么是mvc?
 model view controller
 是一种软件架构思想,将一个软件的组成部分划分成 三种不同类型的模块,分别是模型、视图和控制器。
 模型:

  封装了业务逻辑:
   业务逻辑:包括业务逻辑本身的处理,还有  为保证业务逻辑能够正确处理所需要的事务    、安全、日志、审核等等基础服务。   封装:业务逻辑实现以及对外提供的接口。
 视图:

  实现了表示逻辑:将模型中的数据以合适的方式  展现出来,另外,也提供合适的界面,用于输入。
 控制器:
  视图将请求交给控制器,控制器依据请求的不同,  调用合适的模型来处理;模型处理之后的结果给  控制器,控制器依据结果,选择合适的视图展现  给用户。
  这样做的目的,是使得模型可以方便地  多个视图复用。 
(2)在web应用当中,如何使用mvc?
  model:   使用java类来实现,或者是ejb,spring等容器  管理的javabean来实现(容器会提供部分  基础服务)。
  view:   使用jsp(html,css,js)
  controller:   使用servlet(或者filter)
(3)mvc的优势与缺点:
  优点:
   A.使得模型可以方便地 多个视图复用
   B.模型方便测试(比如 servlet中包含的业务逻辑,   要测试的话,需要部署,启动服务器,通过浏览器   发送请求。将业务逻辑写在java类,只需要测试java类。  )
   C.方便分工协作。
  缺点:
   mvc是一种思想,在做具体产品或者项目时,   需要设计(开发的复杂度增加)。   开发的周期变长,代码的维护量增加了。
  总结:
   如果产品或者项目需要代码的良好的可维护性、   可测试性、方便分工协作(一般是比较大型的项目)。   反之,没有必要用。

 

注释

html <!---->

css //单行  /**/ 多行注释

js //单行  /**/ 多行注释

jsp <%----%>隐含注释,即开发人员专用的注释

java //单行  /**/ 多行注释  /** */ 文档注释

JSP简介,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。