JSP and EL expressions

JSP

1. What is JSP?

  Java Server Pages, a dynamic web programming SUN technology companies.

2. Why should JSP?

  Servlet output page cumbersome

  Servlet generated pages do not preview tool  

  JSP: JSP = HTML + java Code + JSP tag itself

3, JSP dynamic resources or static resources?

  Dynamic resource is   put in the position, like static on the preparation of and access to resources. Essentially a Servlet.

4, JSP how to run?

  A jsp the first time visit, will be the container (server) in the jsp engine (jsp container compiled software modules)

      Compiled into a Servlet jsp and jsp name _jsp.java name _jsp.class and placed under the work directory Tomcat.

5, JSP and Servlet different 

  1. Write jsp file is written as a text file. Write servlet is written in java program.

  2. After the write jsp, jsp directly copied to the container deployment directory on it.

  3. After the servlet written to be configured through the web.xml file. jsp direct access on the line.

  4.jsp and static resources together on it. ----- jsp more like static in the use of resources.

6, JSP three script tag

  <!%%> This tag can be used to declare variables, methods , classes; variables and methods are the member variable and methods; class is an internal class member

       Generally do not have the label

  <%> Prepared in this tab java code, it will appear in the _jspService ()

  <% =%> Wrote in this tab java code will appear in the out () output code in the tag to the page must not be a semicolon

7, JSP comments in

 

  <! - -> general comment, it will appear as comments in the code html

 

      <% - -%> JSP comment does not appear in html

8, operation principle

  In conf tomcat in web.xml can be seen in nature or Servlet

  Principle diagram:

9, three major label instructions

  1、<%@ page %> 

Language

jsp in language that can be embedded so far can only fill in java

Import

For the leader packet

pageEncoding

Set the current encoding jsp page

contentType

Use encoding when you set your browser to view this page

Session

The default is true in the current jsp Can I use the session (If true, then this would be able to use the jsp session object)

isELIgnored

Can you use the current page EL expression

Buffer

The default buffer size is 8kb

autoFlush

Whether to automatically refresh

errorPage

Specified error page

isErrorPage

Set to the wrong page (set to the wrong page, there is a processing dysfunction)

  General configuration error page in web.xml

<error-page>
     <error-code>500</error-code>
     <location>/error.jsp</location>
</error-page>
<error-page>

<exception-type>java.lang.NullPointerException</exception-type>
    <location>/error.jsp</location>
</error-page>

  2、<%@ include file=""  %>  在当前页面包含其他页面

    这样的包含是静态包含,也叫做编译期包含。两个jsp将被编译成一个class文件。

    静态包含时,生成的html的结构被破坏

    动态包含:<jsp:include page="include1.jsp"></jsp:include>

 

  重点:动态包含和静态包含的区别    

    静态包含:是先将整个页面合并然后再生成相应java文件再在tomcat上运行。
    动态包含:先编译所有的页面,在使用的时候 jsp:include标签动态的加载。

    静态加载包含一些HTML静态的页面,没有动态的操作,编译较快,使用方便,功能欠缺。
    动态加载速度会稍稍慢一些,但是所有的页面都是独立的,功能齐全。

  3、<%@ taglib prefix="前缀" uri="标签库的路径" %>   引入标签库,使用的最多

10、九大内置对象

  (1)out输出流对象  out流的执行是 先把 缓冲区中的内容 输出到 response.getWriter()的缓冲区中,再由

          response.getWriter() 这个流统一输出。

<%="aaa"%>
<% out.write("bbb");%>
<%response.getWriter().write("ccc");%>
输出结果为:ccc  aaa  bbb

  (2)pageContext  页面对象,可以获得其他八个对象,也有作用域(范围为当前页面)

  (3)request  请求对象

  (4)response  响应对象

  (5)session  会话对象

  (6)application  项目对象,用法和ServletContext一样

  (7)config  配置对象,用法和ServletConfig一样

  (8)Page  页面对象=this

  (9)exceptino  异常对象,只有jsp的isErrorPage=true时,才会出现这个对象

11、四个作用域 

  pageContext:页面作用域
  request:请求作用域
  Session:会话作用域
  application:项目作用域

  1、在当前页中设置属性值,所有的作用域都可以获取到

  2、请求转发 时,pageContext作用域失效,其他的作用域可以获取值

  3、再次访问,进行第二次请求时, pageContext和request都失效

  4、关闭浏览器,重新打开再次访问,pageContext、request、session 都失效了

  5、关闭服务器,全部失效  

 EL表达式

1、EL是Expression Language的缩写,它是一种简单的数据访问语言。目的是为了使JSP写起来更加简单。

2、EL表达式的作用

  1.获取作用域中的值

  2.做运算 (数学运算  逻辑运算 关系运算...)

  3.EL 提供常用的操作web对象  (11 个)

  4.访问java的方法

3、获取作用域中的值  

  例如:${name} 按照 page,request,session,application(ServletContext)的顺序,查找作用域 key是name的 value值 

   也可以指定查找的范围

            ${pageScope.key}

            ${requestScope.key}

            ${sessionScope.key}

            ${applicationScope.key}   

    getAttribute(key);  当key不存在时返回 null

    ${key}              当key不存在时返回空字符串"" 

User user = new User(1,"zhaosi",20);
request.setAttribute("user", user);
/*
${user.id}
${user.name}
${user.age}   
user----> 保存在request中时的key值
id,name ,age 对象的属性名
要求: 属性必须有get方法 EL表达式才能访问到
*/
<%
         List<String> list = new ArrayList<String>();
         pageContext.setAttribute("list", list);
 %> 
${empty list }  判断list中是否有元素
         
 //数组  List  Map
         
 <%
          String[] ss = {"aaa","bbb","ccc"};
          pageContext.setAttribute("ss", ss);
          List<String>  list = new ArrayList<String>();
          list.add("xxx");
          list.add("yyy");
          list.add("zzz");
          request.setAttribute("list",list);

          List<User> list2 = new ArrayList<User>();
          User u1 = new User(1,"qwe",20);
          User u2 = new User(2,"asd",20);
          User u3 = new User(3,"zzz",20);
          list2.add(u1);
          list2.add(u2);
          list2.add(u3);
          request.setAttribute("list2",list2);
          
       
          Map<String,String> map = new HashMap<String,String>();
          map.put("aaa-a", "111");
          map.put("bbb", "222");
          map.put("ccc", "333");
          
          pageContext.setAttribute("map", map);
%>
        ${pageScope.ss[1] }<br>
        ${requestScope.list[2] }<br>
        ${list2[0].name}<br>
        ${map.bbb } <br>
        ${map["aaa-a"] }

  注:EL不能遍历集合结构

4、做运算 注意:两个值必须在一个大括号里,不能分开写

  ${10+2 } ${10 / 2}  ${5==5 }  ${5<3 }
  ${list2[0].name=="aaa" }
  ${list2[0].age==20 }
  ${list2[0].age == list2[1].age }

  运算符:

操作

描述

示例

结果

==eq

是否相等

${5==5}

True

!=ne

是否不等

${5!=5}

false

<lt

是否小于

${5<7}

True

>gt

是否大于

${5>7}

false

<=le

是否小于等于

${5 le 5}

true

>=ge

是否大于等于

${5 ge 6}

false

  同样也有逻辑运算符

5、EL提供常用的操作WEB对象

pageScope

requestScope

sessionScope

applicationScope

param         相当于  request.getParameter()

paramValues   相当于  request.getParameterValues()

header        相当于   request.getHeader();  一个key对应一个value

headerValues  相当于   request.getHeaders() 一个key对应多个value                             

initParam     获取初始化参数(全局初始化参数)

cookie        获取Cookie的

pageContext  获取其他八个内置对象   访问这个八个对象下的get...方法

  注:当属性中出现 - 时:会被认为是减号,应该这样写  ${header[“User-Agent”]}

 

Guess you like

Origin www.cnblogs.com/xfdhh/p/11409078.html