jsp page指令、include指令、include标签、forword标签、jsp注释方式

目录

三种指令类型

Page指令

列:session与errorPage的运用

include指令

列:静态包含

标签

Include标签

列:实现动态包含发送接收信息

fordword标签

列:jsp实现跳转页面

jsp注释

列:注释练习


指令:发送给jsp编译器的信息(告诉jsp编译器如何处理jsp页面,不会直接生成输出)

语法:<%@ 指令 {属性=value,....}%>

三种指令类型

Page指令

导入页面所依赖的属性并将这些传递给JSP编译器

Include指令

jsp页面翻译时包含文本或代码

Taglib指令

描述编译器所应用的标签库

Page指令

语法:<%@page {attr=”value”}%>

  • page language="java" 表示jsp页面嵌入的脚本是java代码
  • contentType="text/html; charset=UTF-8" 设置响应文件类型和字符集编码
  • pageEncoding="UTF-8" 表示JSP文件本身编码
  • import="java.util.*"  引用类,导入包中的类
  • 默认值是session="true"  是否参与一个http会话 表示页面可以直接使用 session对象

       Session=false,告诉jsp编译器不要创建session对象了.

  • errorPage="error.jsp"   处理错误页面的URL
  • isErrorPage="true"   处理错误页面的信息
  • buffer="16kb"   设置输出缓冲区大小.
  • autoFlush="true"   设置缓冲区是否被自动刷新.

列:session与errorPage的运用

session指令默认为true,表示页面可以使用session对象,可以设置为false

errorPage处理错误页面的URL

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page session="true"%>
<%@ page errorPage="error.jsp" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=new Date()%>


	<%
		session.setAttribute("mysession", "123");
		int i = 100 / 0; //发生异常
	%>
	<%
		session.getAttribute("mysession");
	%>
</body>
</html>

error.jsp 显示错误  

 isErrorPage="true"   处理错误页面的信息

buffer 页面输出的缓冲区是8k

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true" buffer="8kb"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示错误</title>
</head>
<body>
  出错了...
  <%=exception.getMessage() %>
</body>
</html>

include指令

动态包含,先包含进来,然后一起编译,生成一个 . java文件

列:静态包含

includeA.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="includeB.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>includeA.jsp 我包含了</h1>
</body>
</html> 

includeB.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2 style="color: red">includeB.jsp</h2>
</body>
</html>

 

标签

Include标签

分开编译,(就会产生二个.java文件,)然后再动态包含进来,生成两个 . java文件

列:实现动态包含发送接收信息

includeC.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
	%>
	<jsp:include page="includeD.jsp">
		<jsp:param value="aa你好" name="uname" />
	</jsp:include>
	<h1>includec.jsp 我编译了。。。</h1>
</body>
</html>

includeD.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%=request.getParameter("uname")%>
	<h2 style="color: red">includeD.jsp</h2>
</body>
</html>

 

fordword标签

转发,服务器端跳转(又称内部跳转),跳转地址不变,跳出去不会回来

列:jsp实现跳转页面

forwordA.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>
	<%
		System.out.println("forwardA.jsp  begin");
		request.setAttribute("css", "csss");
		response.sendRedirect("forwardB.jsp");
	%>
	<%--    <!--forword代码一次跳转,跳转到另一个页面不会再回来-->
         <jsp:forward page="forwardB.jsp">
             <jsp:param value="111" name="user"/>
             </jsp:forward> --%>

	<%
		System.out.println("forwardA.jsp  end");
	%>
</body>
</html>

forWordB.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>
   <%  
      System.out.println("forwardB.jsp");
   %>
   <%=request.getParameter("user") %>
   <%=request.getAttribute("css")%>
</body>
</html>

结果:

输入http://localhost:8080/0615jsp指令标签与表达式/jsp/forwardB.jsp会跳转到如下界面

问题:你们猜猜值为什么是空的?


jsp注释

注释有如下方式:

HTML页面注释

这里面的注释会被编译(加载页面时,会进行语法判断,取需要的变量默认值)

<!-- 我是注释 -->

jsp页面注释

-这里之后的注释都不会被编译

<%--  jsp注释   --%>

jsp脚本的单行注释

<%
// 书写注释的内容

%>

jsp脚本的多行注释

<%
/* 
书写注释的内容
书写注释的内容
*/
%>

列:注释练习

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值