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

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

一、JSP技术

1.jsp脚本和注释

jsp脚本:

1)<%java代码%> ----- 内部的java代码翻译到service方法的内部
2)<%=java变量或表达式> ----- 会被翻译成service方法内部out.print()
3)<%!java代码%> ---- 会被翻译成servlet的成员的内容
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

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

1)Html注释:<!--注释内容-->—可见范围 jsp源码、翻译后的servlet、页面 显示html源码
2)java注释://单行注释 /多行注释/ --可见范围 jsp源码 翻译后的servlet
3)jsp注释:<%–注释内容–%> ----- 可见范围 jsp源码可见
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

2.jsp运行原理-----jsp本质就是servlet(面试)

jsp在第一次被访问时会被Web容器翻译成servlet,在执行
过程:
第一次访问---->helloServlet.jsp---->helloServlet_jsp.java---->编译运行
PS:被翻译后的servlet在Tomcat的work目录中可以找到

3.jsp指令(3个)

jsp的指令是指导jsp翻译和运行的命令,jsp包括三大指令:

1)page指令 — 属性最多的指令(实际开发中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:当前页面是一个处理错误的页面
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
当不进行设置web应用的全局的错误页面时候,会出现这样的错误:
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
设置web应用的全局的错误页面-就近调用自己的
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

2)include指令

页面包含(静态包含)指令,可以将一个jsp页面包含到另一个jsp页面中
格式:<%@ include file=“被包含的文件地址”%>
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

3)taglib指令

在jsp页面中引入标签库(jstl标签库、struts2标签库)
格式:<%@ taglib uri=“标签库地址” prefix=“前缀”%>

4.jsp内置/隐式对象(9个)----- 笔试

jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,我们在jsp 脚本中可以直接使用这9个对象
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
打开任意一个.jsp文件
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

(1)out对象

out的类型:JspWriter
out作用就是想客户端输出内容----out.write()
out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲 器
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)

(2)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域:当前jsp页面范围
request域:一次请求
session域:一次会话
application域:整个web应用

2)可以获得其他8大隐式对象
例如: pageContext.getRequest()
pageContext.getSession()

<%@ 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向request域存数据
		request.setAttribute("name", "zhangsan");
		pageContext.setAttribute("name", "sunba");
		pageContext.setAttribute("name", "lisi",PageContext.REQUEST_SCOPE);
		pageContext.setAttribute("name", "wangwu", PageContext.SESSION_SCOPE);
		pageContext.setAttribute("name", "tianqi", PageContext.APPLICATION_SCOPE);
	%>
	<!-- 这两个一样 -->
	<%=request.getAttribute("name") %>
	<%=pageContext.getAttribute("name", PageContext.REQUEST_SCOPE) %>
	
	<!-- findAttribute会从小到大搜索域的范围中的name -->
	<!-- page域<request域<session域<application域 -->
	<%=pageContext.findAttribute("name") %>
	
	<%
		/* 可以获得其他8大隐式对象 */
		pageContext.getRequest();
		pageContext.getOut();
	%>
</body>
</html>

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

5.jsp标签(动作)

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

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

静态包含与动态包含的区别?
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
静态包含

<%@ 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>
	<h1>this is include1 page</h1>
	<%@ include file="/include2.jsp" %>
</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>
	<h1>this is include2 page</h1>
</body>
</html>

WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
WEB17/WEB18—动态页面技术(JSP/EL/JSTL)
动态包含

<%@ 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>
	<h1>this is include3 page</h1>
	<!-- 包含include4 -->
	<jsp:include page="/include4.jsp"></jsp:include>
</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>
	<h1>this is include4 page</h1>
</body>
</html>

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

2)请求转发:<jsp:forward page=“要转发的资源” />

转发:地址不发生改变

<%@ 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>
	<h1>this is forward1 page</h1>
	<jsp:forward page="/forward2.jsp"></jsp:forward>
</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>
	<h1>this is forward2 page</h1>
</body>
</html>

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

案例:完成商品的列表的展示

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

package cn.itcast.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.itcast.domain.Product;
import cn.itcast.utils.DataSourceUtils;

public class ProductListServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//准备所有商品数据--List<Product>
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from product";
		List<Product> productList = null;
		try {
			productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		//商品的集合准备好
		//将数据存到request域,转发给product_List.jsp进行显示
		request.setAttribute("productList", productList);
		request.getRequestDispatcher("/product_list.jsp").forward(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

product_list.jsp

<%
			//获得集合List<Product>
			List<Product> productList = (List<Product>)request.getAttribute("productList");
			if(productList != null){
				for(Product product : productList){
					out.write("<div class='col-md-2' style='height:230px'>");
					out.write("<a href='product_info.htm'> <img src='" + product.getPimage() + "' width='170' height='170' style='display: inline-block;'></a>");
					out.write("<p><a href='product_info.html' style='color: green'>" + product.getPname() + "</a></p>");
					out.write("<p><font color='#FF0000'>商城价:&yen;" + product.getShop_price() + "</font></p>");
					out.write("</div>");					
				}
			}
		%>

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