jsp语法
jsp头部文件声明
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
language 属性 值只能是 java。 表示翻译的得到的是 java 语言的
-
contentType 属性 设置响应头 contentType 的内容
-
pageEncoding 属性 设置当前 jsp 页面的编码 import 属性 给当前 jsp 页面导入需要使用的类包
-
autoFlush 属性 设置是否自动刷新 out 的缓冲区,默认为 true
-
buffer 属性 设置 out 的缓冲区大小。默认为 8KB
-
errorPage 属性 设置当前 jsp 发生错误后,需要跳转到哪个页面去显示错误信息 isErrorPage 属性 设置当前 jsp 页面是否是错误页面。是的话,就可以使用 exception 异常对象
-
session 属性 设置当前 jsp 页面是否获取 session 对象,默认为 true
-
extends 属性 给服务器厂商预留的 jsp 默认翻译的 servlet 继承于什么
jsp中的脚本
- 声明脚本
<%!
java代码
%>
<%!
//定义全局变量
public int a = 19;
private String string = "hello";
private static HashMap<String, String> temp;
//定义static代码块
static {
temp = new HashMap<String, String>();
temp.put("key1", "value1");
System.out.println("这是静态代码块。");
}
//定义方法
public void sayHello(){
System.out.println("这是sayHello方法。");
}
//定义内部类
class AA{
public int a =20;
private String string = "stringValue";
public AA(){}
}
%>
- 表达式脚本
<%= 表达式 %>
<%-- 表达式脚本 --%>
<%= 19 %>
<%= 20.1 %>
<%= "hello" %>
<%= temp %>
- 代码脚本
<% java代码 %>
<%-- 代码脚本1 --%>
<%
int a = 0;
if (a == 0) {
System.out.println("a == 0");
} else {
System.out.println("a != 0");
}
%>
<%-- 代码脚本2 --%>
<%
int b = 0;
if (b == 0) {
%>
<%
System.out.println("b == 0");
} else {
%>
<%
System.out.println("b != 0");
}
%>
<%-- 代码脚本3 --%>
<%
for (int i = 0; i < 10; i++){
%> <%= "for循环:i=" + i %><br/>
<%
}
%>
jsp中的注释
<%
// 单行 java 注释
/**
* 多行 java 注释
*/
%>
<%-- jsp注释 --%>
<!-- html注释 -->
jsp中的九大内置对象
request 对象 请求对象,可以获取请求信息
response 对象 响应对象。可以设置响应信息
pageContext 对象 当前页面上下文对象。可以在当前上下文保存属性信息
session 对象 会话对象。可以获取会话信息。
exception 对象 异常对象只有在 jsp 页面的 page 指令中设置 isErrorPage="true" 的时候才会存在
application 对象 ServletContext 对象实例,可以获取整个工程的一些信息。
config 对象 ServletConfig 对象实例,可以获取 Servlet 的配置信息
out 对象 输出流。
page 对象 表示当前 Servlet 对象实例(无用,用它不如使用 this 对象)。
jsp中的四大域对象
- 经常用来保存数据信息。
pageContext 可以保存数据在同一个 jsp 页面中使用
request 可以保存数据在同一个 request 对象中使用。经常用于在转发的时候传递数据
session 可以保存在一个会话中使用
application(ServletContext) 就是 ServletContext 对象
-
新建测试代码 context1.jsp, context2.jsp
context1.jsp
<%--
Created by IntelliJ IDEA.
User: Mr.Chen
Date: 2021/5/23
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
这是 context1 页面<br/>
<%
//设置 page 域的数据
pageContext.setAttribute("key", "pageContext-value");
//设置 request 域的数据
request.setAttribute("key", "request-value");
//设置 session 域的数据
session.setAttribute("key", "session-value");
//设置 application 域的数据
application.setAttribute("key", "application-value");
%>
<%-- 测试当前页面作用域 --%>
<%=pageContext.getAttribute("key") %><br/>
<%=request.getAttribute("key") %><br/>
<%=session.getAttribute("key") %><br/>
<%=application.getAttribute("key") %><br/>
<%
// 测试 request 作用域
//request.getRequestDispatcher("/context2.jsp").forward(request, response);
%>
</body>
</html>
context2.jsp
<%--
Created by IntelliJ IDEA.
User: Mr.Chen
Date: 2021/5/23
Time: 11:03
To change this template use File | Settings | File Templates.
--%>
<%@ 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>
这是 context2 页面 <br/>
<%=pageContext.getAttribute("key") %><br/>
<%=request.getAttribute("key") %><br/>
<%=session.getAttribute("key") %><br/>
<%=application.getAttribute("key") %><br/>
</body>
</html>
测试
测试pageContext作用域步骤:
1. 直接访问context1.jsp
测试request作用域步骤:
1. 取消context1.jsp 第36行注释(测试request)
2. 直接访问context2.jsp(没有数据)
3. 直接访问context1.jsp(有数据)
测试session作用域步骤:
1. 访问context1.jsp
2. 关闭浏览器,不要关闭服务器
3. 打开浏览器,访问context2.jsp
测试application作用域步骤:
1. 访问context1.jsp,然后关闭浏览器
2. 重启服务器
3. 直接访问context2.jsp
jsp中的out输出流和response.getWriter()输出流
<%--
Created by IntelliJ IDEA.
User: Mr.Chen
Date: 2021/5/23
Time: 20:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>output</title>
</head>
<body>
<%
// out 输出
out.write("这是 out 的第一次输出<br/>");
// out flush 之后。会把输出的内容写入 writer 的缓冲区中
out.flush();
// 最后一次的输出,由于没有手动 flush,会在整个页面输出到客户端的时候,自动写入到 writer缓冲区
out.write("这是 out 的第二次输出<br/>");
// writer 的输出
response.getWriter().write("这是 writer 的第一次输出<br/>");
response.getWriter().write("这是 writer 的第二次输出<br/>");
%>
</body>
</html>
-
浏览器输出
-
图解
jsp常用标签
-
静态包含(把包含的页面内容原封装不动的输出到包含的位置)
<%@include file=" "%>
-
动态包含(把包含的 jsp 页面单独翻译成 servlet 文件,然后在执行到时候再调用翻译的 servlet 程序。并把 计算的结果返回。 动态包含是在执行的时候,才会加载。所以叫动态包含)
<jsp:include page=" "></jsp:include>
-
页面转发(转发功能相当于 request.getRequestDispatcher("/xxxx.jsp").forward(request, response); 的功能)
<jsp:forward page=" "></jsp:forward>
-
动态包含(把包含的 jsp 页面单独翻译成 servlet 文件,然后在执行到时候再调用翻译的 servlet 程序。并把 计算的结果返回。 动态包含是在执行的时候,才会加载。所以叫动态包含)
<jsp:include page=" "></jsp:include>
-
页面转发(转发功能相当于 request.getRequestDispatcher("/xxxx.jsp").forward(request, response); 的功能)
<jsp:forward page=" "></jsp:forward>