web review day06: jsp & el & jstl

review:

Session: a call

Session Role: save the browser and server interaction process data generated.

Session cookie: browser

由服务器创建

保存在浏览器上

存放的数据不安全

存放的大小受限(4kb)

存放文本格式的数据

cookie属于缓存的一种

    cookie中不能存放特殊的字符

API:

    Cookie c = new Cookie(String,String);

    c.setMaxAge(3600秒);

    response.addCookie(c); // 本质上就是给set-cookie头设置信息

    默认绑定路径: 访问路径的上一层

    c.setPath("/项目名称/aa");

    携带cookie:

        规则: 访问路径字符串包含原则

    Cookie []cookies = request.getCookies();

    cookie.getName();

    cookie.getValue();

Session session: server

执行流程:

    请求没有携带jsessionid:

        request.getSession(); // 创建空间并返回jsessionid

    jsessionid丢失: 请求没有携带jsessionid

    携带jsessionid:

        找到了session空间,直接使用

        没有找到session空间,创建一个新的session,并返回jsessionid

API:

    request.getSession();

        //1.创建session空间,并返回jsessionid(session的唯一标识)

        //2.根据jsessionid找到对应的session空间,进行使用

session的声明周期:

    创建:

        请求不携带jsessionid就会创建session对象

        请求携带jsessionid,但未找到对应的session空间

    销毁:

        服务器非正常关闭

        session超时

        手动销毁

            session.invalidate();

servlet in three scopes:

request: 请求对象

    创建: 请求来的时候

    销毁: 响应信息生成的时候

    作用域范围: 当前请求

    api:

        setAttribute(String,Object); // 设置同名属性即是修改

        getAttribute(String);

        removeAttribute(String);

session: 会话对象

    创建:

        请求不携带jsessionid就会创建session对象

        请求携带jsessionid,但未找到对应的session空间

    销毁:

        服务器非正常关闭

        session超时

        手动销毁

            session.invalidate();

        作用域范围: 当前会话

        api:

            setAttribute(String,Object); // 设置同名属性即是修改

            getAttribute(String);

            removeAttribute(String);

servletContext: 上下文对象

    创建: 服务器启动时

    销毁: 服务器关闭时, 或项目从服务器上移除

    作用范围: 当前项目

    api:

            setAttribute(String,Object); // 设置同名属性即是修改

            getAttribute(String);

            removeAttribute(String);

jsp&el&jstl

JSP (java server page)

jsp Overview:

JSP全名为Java Server Pages,中文名称java服务器页面,本质上就是一个Servlet,是服务器上的一种动态的网页技术.

简单理解

    在html页面中嵌套java代码

高大上理解

    将页面的展示 和 内容的生成相分离

effect:

展示动态的信息

jsp extension

*.jsp

jsp part

html + java + jsp特有内容(三大指令,四大作用域,九大内置对象)

// 需求: 访问servlet返回一个表格
// 由于通过servlet返回html信息相当痛苦,我们使用jsp代替servlet完成展示.

jsp execution process

第一次访问jsp(index.jsp)页面时,服务器接收请求,由jspservlet来处理该请求.

1.jspservlet会去查找对应的jsp文件

2.找到之后,服务器会将jsp文件转换成java文件(index_jsp.java)

3.服务器编译java文件,生成class文件

4.服务器运行class文件,生成动态的内容,并返回给浏览器.

==理解:== 

    当浏览器访问 *.jsp 时,统一由JspServlet处理这类请求,

    找到对应的jsp页面,转成java类(就是Servlet)

    然后让转完后的servlet执行(_jspService),返回的信息就是jsp页面中的html代码

jsp unique content

jsp scripts

How to write java code in jsp page

编写java程序片段

    <% ...... %>

输出信息表达式

    <%= ... %>

声明成员变量

    <%! ... %>

jsp comments

    <%-- 注释内容 --%>

    注释使用时: ctrl+shift+ / 

Extended:
JSP-specific content (three instructions, the four scopes, nine built-in object)
three instructions:
Page
taglib: the introduction of a third-party tag library
include: the other page contains
four scopes:
pageContext (PageContext): the current page
request (the HttpServletRequest)
the session (the HttpSession)
file application (the ServletContext)
nine built-in objects:
JSPServlet when jsp pages into java, 9 defined to achieve the object, the aim to facilitate jsp pages, can be used directly built jsp pages Object
Request
Response
the session
file application
the pageContext
Page
config
OUT
Exception
+ HTML Ajax:
on the servlet is essentially a jsp
jsp consisting of:
HTML Java + + jsp specific content (the three instructions, the four scopes, nine built-in object)
Java:
<% Java % fragment>
<content% =% outputted>
<%!% declare a global variable>
Get request session scope and servletContext jsp pages of data on the
<% = the request.getAttribute STR String ( "AA"); ....%>
<% =% STR>
the EL effect expression: instead of the output <% the request.getAttribute = ( "AA")%>
$ { "AA"}
el and jstl: simplified java code jsp pages, jsp pages enhanced functionality
el expression

Full name: Expression Language later (expression language) jsp2.0 built-el

effect:

主要用来代替 <%= %>

format:

${表达式}

Common features

Scope of data acquisition

Acquiring specified data domain:
<% @ Page contentType = "text / HTML; charset = UTF-. 8" Language = "Java"%>
<HTML>
<head>
<title> the Title </ title>
</ head>
<body>
<H3> simple data acquired domain information </ H3>
<%
// store data information to the domain
pageContext.setAttribute ( "pkey", "pvalue "); // current page scope
request.setAttribute ( "rKey", "rvalue");
session.setAttribute ( "SKey", "sValue");
application.setAttribute ( "AKEY", 47); // where servletContext

    request.setAttribute("aa.bb","哈哈哈");
%>
<%-- pageScope: 代表的就是pageContext对象 --%>
获取pageContext域中的数据信息: <br>
原始: <%=pageContext.getAttribute("pkey")%> <br>
EL表达式: ${pageScope.pkey} <br>
<hr>
获取request域中的数据信息: <br>
原始: <%=request.getAttribute("rkey")%> <br>
EL表达式: ${requestScope.rkey} <br>
<hr>
获取session域中的数据信息: <br>
原始: <%=session.getAttribute("skey")%> <br>
EL表达式: ${sessionScope.skey} <br>
<hr>
获取servletContext域中的数据信息: <br>
原始: <%=application.getAttribute("akey")%> <br>
EL表达式: ${applicationScope.akey} <br>
<hr>
获取request域中的数据信息: <br>
原始: <%=request.getAttribute("aa.bb")%> <br>
EL表达式: ${requestScope["aa.bb"]} <br>
<hr>
获取不到信息时: <br>
原始: <%=request.getAttribute("aaaaaa")%> <br>
EL表达式: ${requestScope.aaaaaa} <br>

</ body>
</ HTML>
obtain simple data
pageScope: pageContext
requestScope: Request
sessionScope: the session
applicationScope: servletContext
== == easy to find

${域中属性的名称}

    注释: 在所有作用域中依次从小到大查找指定的属性名所对应的值

         若找到立即返回并结束查询

             若找不到返回 ""

    pageContext  request  session  application

Precautions

    属性名中一旦使用了"." "+" "-"等特殊符号的时候${属性名}就取不到了,要使用{域Scope["属性名"]}

    例如:${requestScope["aa.bb"]}
<%
    request.setAttribute("aa.bb","abc");
%>
<hr>
老方式: <%=request.getAttribute("aa.bb")%> <br>
el方式: ${aa.bb}  ---  ${requestScope["aa.bb"]}
    获取复杂数据

Acquiring data in the array

格式

    ${数组属性名称[index]}

例子:

    request.setAttribute("arr", new String[]{"aa","bb","cc"});

获取的时候

    ${arr[1]}

Get Data list of

格式

    ${list属性名[index]}

The map data acquisition

格式

    ${map属性名称.键名}

Javabean in obtaining data

格式

    ${javabean在域中的属性名称.属性名称}

JSTL:

apache出品的标签库语言

effect:

增强jsp页面的功能, 在jsp页面上少写java代码

classification

core:核心(目前还在使用)

fmt:格式化(国际化)

xml:和xml相关的(过时了)    json

sql:和sql相关的(过时了)

fn:对字符串处理的函数库(用的很少了)

Steps for usage:

1.导入jar包

2.在页面上引入标签库

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

If the use of the label

格式:

<!--当el表达式返回true时,标签体中的数据会被输出-->
<c:if test="el表达式">...</c:if>
<!--扩展(了解)
将el表达式的结果起个名称为flag 默认会放入page域中
page pageContext
request request
session session
application application
-->
<c:if test="el表达式" var="flag" scope="">...</c:if>
forEach标签的使用
<!-- 基本使用 -->
<c:forEach begin="从哪开始" end="到哪结束" step="步长" var="给变量起个名称">${i }</c:forEach>

<!-- 例子 -->
<c:forEach begin="1" end="20" step="1" var="i">
${i}
</c:forEach>

<!-- 高级使用 -->
<!-- 在页面上遍历数组,单列集合和map -->
<!-- 格式: -->
<c:forEach items="从域中通过el获取集合" var="n">
${n}
</c:forEach>
<!--属性:varStatus
用来记录循环状态的,有几个常用的属性:
index:
count:
first:
last:
-->
javaEE开发的模式(思想 理解)

MVC模式 -- 属于所有的web开发

发展历史

model1

        技术架构: jsp + javaBean

            jsp:负责处理业务逻辑  页面展示

            javaBean: 实体封装  负责处理简单的业务逻辑  

        优缺点

            优点

                开发效率高

            缺点

                随着业务的复杂性,导致jsp中的业务越来越多,不方便维护

model2

        技术架构:jsp+servlet+javaBean

            jsp:页面展示

            servlet:负责处理业务逻辑

            javaBean:负责处理业务逻辑  实体封装

        优缺点

            优点

                逻辑清晰 方便维护

            缺点

                小型项目开发没有 model1 效率高

M:model 模型

    封装数据 ---- javaBean

    主要封装数据 业务处理

V:view 视图

    页面展示 ---- jsp

    作用:页面展示

C:controller 控制器

    控制执行流程---- servlet

    作用:与客户端交互

javaEE的三层架构

    通过分包实现

        web(action | controller ): 接收请求和前台交互

        service: 处理业务逻辑

        dao: 操作数据库

        domain: 实体

        utils:

        jsp: 展示信息

Guess you like

Origin blog.51cto.com/13859849/2400319