动态页面技术(JSP/EL/JSTL)

JSP技术:

jsp脚本:

1)<%java代码%> ----- 内部的java代码翻译到service方法的内部

2)<%=java变量或表达式> ----- 会被翻译成service方法内部out.print()

3)<%!java代码%> ---- 会被翻译成servlet的成员的内容

jsp注释:  不同的注释可见范围是不同

1)Html注释:<!--注释内容--> ---可见范围 jsp源码、翻译后的servlet、页面 显示html源码

2)java注释://单行注释  /*多行注释*/ --可见范围 jsp源码 翻译后的servlet

3)jsp注释:<%--注释内容--%> ----- 可见范围 jsp源码可见

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--这是HTML注释  -->
    <%
    //这是java当行注释
    /*这是java多行注释*/
    int i=100; 
    System.out.println(i);
    %>
    <%--这是jsp注释 --%>
    <%=i %>
    <%! String str="袁一鸣"; %>
    <%=str %>
</body>
</html>

jsp运行原理:jsp本质就是servlet

jsp指令(3个)

jsp的指令是指导jsp翻译和运行的命运。

1、page指令:

格式:<%@ page 属性名1= "属性值1" 属性名2= "属性值2" ...%>

language:jsp脚本中可以嵌入的语言种类

pageEncoding:当前jsp文件的本身编码---内部可以包含contentType

contentType:response.setContentType(text/html;charset=UTF-8)

session:是否jsp在翻译时自动创建session

import:导入java的包

errorPage:当当前页面出错后跳转到哪个页面

isErrorPage:当前页面是一个处理错误的页面

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="false" errorPage="error.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int y=1/0;
        HttpSession session=request.getSession();
        session.setAttribute("goods", "naiping");
    %>
    <%    
        ArrayList<String> arr=new ArrayList<String>();
    %>
</body>
</html>

2、include指令

格式:<%@ include file="被包含的文件地址"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
这是头部
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
这是尾部
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%@ include file="header.jsp" %><br>
这是内容<br>
<%@ include file="footer.jsp" %>
</body>
</html>

3、taglib指令:

格式:<%@ taglib uri="标签库地址" prefix="前缀"%>

4、jsp 9个内置对象

out

javax.servlet.jsp.JspWriter

用于页面输出

request

javax.servlet.http.HttpServletRequest

得到用户请求信息,

response

javax.servlet.http.HttpServletResponse

服务器向客户端的回应信息

config

javax.servlet.ServletConfig

服务器配置,可以取得初始化参数

session

javax.servlet.http.HttpSession

用来保存用户的信息

application

javax.servlet.ServletContext

所有用户的共享信息

page

java.lang.Object

指当前页面转换后的Servlet类的实例(在普通类中的this)

pageContext

javax.servlet.jsp.PageContext

JSP的页面容器

exception

java.lang.Throwable

表示JSP页面所发生的异常,在错误页中才起作用

 

out对象:

out的类型:JspWriter

out作用就是向客户端输出内容----out.write()

out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲器

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    aaaaaaa
    <%
    out.write("bbbbbbbb");
    response.getWriter().write("ccccccc");
    %>
    <%="dddddddd" %>
</body>
</html>

pageContext对象:

jsp页面的上下文对象,作用如下:

page对象与pageContext对象不是一回事

1)pageContext是一个域对象

    setAttribute(String name,Object obj)

    getAttribute(String name)

    removeAttrbute(String name)

 

pageContext可以向指定的其他域中存取数据

    setAttribute(String name,Object obj,int scope)

    getAttribute(String name,int scope)

    removeAttrbute(String name,int scope)

    findAttribute(String name)

        ---依次从pageContext域,request域,session域,application域中获取属性,在某个域中获取后将不在向后寻找

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        pageContext.setAttribute("goods", "奶瓶");
        //向request域中设置值
        pageContext.setAttribute("goods", "脑袋", pageContext.REQUEST_SCOPE);
        //向session域中设置值
        pageContext.setAttribute("goods", "方便面", pageContext.SESSION_SCOPE);
        //向application域中设置值
        pageContext.setAttribute("goods", "快乐水", pageContext.APPLICATION_SCOPE);
    %>
    <%
        System.out.println((String)pageContext.findAttribute("goods"));
    %>
    <%
        /* //从request域中取值
        System.out.println(pageContext.getAttribute("goods",pageContext.REQUEST_SCOPE));
        //从session域中取值
        System.out.println(pageContext.getAttribute("goods",pageContext.SESSION_SCOPE));
        //从application域中取值
        System.out.println(pageContext.getAttribute("goods",pageContext.APPLICATION_SCOPE)); */
    %>
</body>
</html>

pageContext对象可以获得其他8大隐式对象。

jsp标签(动作)

1、页面包含(动态包含):<jsp:include page="被包含的页面"/>

2、请求转发:<jsp:forward page="要转发的资源" />

 

 EL技术:

EL 表达式概述

EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写

EL从域中取出数据(EL最重要的作用)

jsp脚本:<%=request.getAttribute(name)%>

EL表达式替代上面的脚本:${requestScope.name}

EL最主要的作用是获得四大域中的数据,格式${EL表达式}

EL获得pageContext域中的值:${pageScope.key};

EL获得request域中的值:${requestScope.key};

EL获得session域中的值:${sessionScope.key};

EL获得application域中的值:${applicationScope.key};

EL从四个域中获得某个值${key};

---同样是依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找

 

JSTL技术:

使用jsp的taglib指令导入核心标签库<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

 <c:if test="">标签

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@    taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- <c:if test="${1==1 }">
正确
</c:if>
<c:if test="${1!=1 }">
错误
</c:if> --%>
<!--普通for-->
<c:forEach begin="0" end="10" var="i">
${i }
</c:forEach>
</body>
</html>

<c:forEach>标签

<%@page import="com.oracle.domain.User"%>
<%@page import="java.nio.channels.SeekableByteChannel"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@    taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        //创建List<User>
        User user1=new User();
        user1.setAge(8);
        user1.setName("袁一鸣");
        User user2=new User();
        user2.setAge(10);
        user2.setName("熊大");
        ArrayList<User> list=new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        request.setAttribute("UserList", list);
        //创建Map<String,User>
        Map<String,User> map=new HashMap<String,User>();
        map.put("1", user1);
        map.put("2", user2);
        application.setAttribute("UserMap", map);
        //创建List<String>
        ArrayList<String> arr=new ArrayList<String>();
        arr.add("a");
        arr.add("b");
        session.setAttribute("str", arr);
    %>
    
    
    <c:forEach items="${str }" var="s">
        ${s }
    </c:forEach>
    <!-- for(User user:UserList){
        System.out.println(user.getName()+"..."+user.getAge());
    } -->
    <c:forEach items="${UserList }" var="user">
        ${user.name }...${user.age }
    </c:forEach>
    <c:forEach items="${UserMap }" var="entry">
        ${entry.key }...${entry.value.name }...${entry.value.age }
    </c:forEach>
</body>
</html>

posted on 2019-07-15 14:28  boss-H  阅读(203)  评论(0编辑  收藏  举报

导航