JSP的概述(熟悉)
JSP的概念
- JSP是Java Server Pages的简称,跟Servlet一样可以动态生成HTML响应, JSP文件命名为xxx.jsp。
- 与Servlet不同,JSP文件以HTML标记为主,然后内嵌Java代码段,用于处理动态内容。
JSP的示例
<%@ page import="java.util.Date" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Hello Time</title></head><body>jj现在的时间是:<%= new Date()%></body></html>
JSP与Servlet的关系

JSP的语法(熟悉)
JSP语法结构
<%! %>
<%程序代码区%>
<%=… …%>
- 说明:可以输出一个变量或一个具体内容,但=后面必须是字符串变量或者可以被转换成字符串的 表达式。
注意:不需要以;结束,只有一行
<%=“hello world”%><%=i+1%>
declare.jsp ```javascript <%— Created by IntelliJ IDEA. User: pingmao Date: 2021/4/23 Time: 下午11:06 To change this template use File | Settings | File Templates. —%> <%@ page contentType=”text/html;charset=UTF-8” language=”java” %>
<%! int ia; // 这是一个全局变量 public void show(){ System.out.println(“这是全局方法!”); } public class MyClass{ { System.out.println(“这是一个全局类哦!”); } } %> <% int ib = 20; // 这是一个局部变量 for (int i = 0; i < 3; i++){ System.out.println(“随便放入Java程序代码吧!”); } %> <%= ia + 1 %> <%= ib %> <%= “我就暂时写到这里吧!” %>
- 案例题目使用for循环输出一个html语言的表格,具体表头如下:| id | name | age | salary || --- | --- | --- | --- || 1 | 1 | 1 | 1 || 2 | 2 | 2 | 2 || ... | | | || 5 | 5 | 5 | 5 |```javascript<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/23Time: 下午11:20To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>实现表格的绘制</title></head><body><table><tr><td>id</td><td>name</td><td>age</td><td>salary</td></tr><%for (int i = 1; i < 6; i++){%><tr><td> <%= i %> </td><td> <%= i %> </td><td> <%= i %> </td><td> <%= i %> </td></tr><%}%></table></body></html>
注释
格式:<!--… …--> HTML文件的注释,浏览器可以查看到<%--… …--%> JSP文件的注释,浏览器看不到<%//… …%> Java语言中的单行注释,浏览器看不到<%/*… …*/%> Java语言中的多行注释,浏览器看不到注释的内容不会被执行
指令和动作
- 指令格式:
<%@指令 属性=“属性值”%>
import 导入相应的包,惟一允许在同一文档中多次出现的属性
contentType 设置Content-Type响应报头,标明即将发送到浏览器的文档类型
pageEncoding 设置页面的编码
language 指定页面使用的语言
session 控制页面是否参与HTTP会话
errorPage 处理当前页面中抛出但未被捕获的任何异常
isErrorPage 当前页是否可以作为其他页面的错误处理页面
page.jsp
<%@ page import="java.util.List" %><%@ page import="java.util.LinkedList" %><%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 上午7:06To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>page指令的使用</title></head><body><%List<String> list = new LinkedList<String>();%></body></html>
taglib指令
taglib指令用来扩展JSP程序的标签元素,引入其他功能的标签库文件。
<!-- prefix属性用于指定库前缀 --><!-- uri属性用于指定库的标识 --><%@taglib uri=“tagLibary” prefix=“prefix”%>
include指令
include指令用于引入另一个JSP程序或HTML文件等,格式如下:
<%@include file=“被包含的文件地址%>
JSP引擎会在JSP文件的转换时期先把file属性设定的文件包含进来,然后开始执行转换及编译的工 作。
jsp:include/jsp:param
jsp:include动作用于引入另一个JSP程序或HTML文件等。
- 执行到include时,被include的文件才会被编译。
如果include的是jsp文件,那它不会被转换成Servlet文件。
<jsp:include page=“URLSpec” flush=“true”/><jsp:include page=“URLSpec” flush=“true”><jsp:param name=“key” value=“value”/></jsp:include>
include指令和include动作的区别
include指令是在JSP程序的转换时期就将file属性所指定的程序内容嵌入再编译执行(静态包含)。
include动作在转换时期是不会被编译的,只有在客户端请求时期被执行到才会被动态的编译载入(动态包含,推荐)。
jsp:forward/jsp:param
forward动作用于在JSP中实现转发,将请求转发到另一个指定的JSP程序或者Servlet中处理。
<jsp:forward page=“urlSpec” flush=“true”/><jsp:forward page=“urlSpec”><!-- 用于指定参数和其对应的值 --><jsp:param name=“key” value=“value”/></jsp:forward>
JSP内置对象(重点)
基本概念
在JSP程序中有9个内置对象由容器为用户进行实例化,程序员可以不用定义就直接使用这些变量。
- 在JSP转换成Servlet后,会自动追加这些变量的定义,使用内置对象可以简化JSP的开发。
对象名称
| 对象变量 | 对象类型 | 作用 | | —- | —- | —- | | out | JSPWriter | 输出流 | | request | HttpServletRequest | 请求信息 | | response | HttpServletResponse | 响应信息 | | session | HttpSession | 会话 | | application | ServletContext | 全局的上下文对象 | | pageContext | PageContext | JSP页面上下文 | | page | Object | JSP页面本身 | | config | ServletConfig | Servlet配置对象 | | exception | Throwable | 捕获网页异常 |
out内置对象
- out内置对象是一个缓冲的输出流,用来给客户端输出信息。
常用方法如下:
| 方法声明 | 功能介绍 |
|---|---|
| void println(String x) | 向客户端输出各种类型数据 |
| void newLine() | 输出一个换行符 |
| void close() | 关闭输出流 |
| int getBufferSize() | 返回缓冲区的大小 |
| int getRemaining() | 返回缓冲区中未使用的字节数 |
| void flush() | 输出缓冲区里的数据 |
| void clearBuffer() | 清除缓冲区里的数据,同时把数据输出到客户端 |
| void clear() | 清除缓冲区里的数据,但不把数据输出到客户端 |
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 上午7:55To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>out内置对象的使用</title></head><body><%out.println("<h1>");out.println("Hello World!");out.println("</h1>");//out.close();int bufferSize = out.getBufferSize();System.out.println("缓冲区的总大小是:" + bufferSize);int remaining = out.getRemaining();System.out.println("缓冲区的剩余字节数为:" + remaining);System.out.println("已经使用的字节数为:" + (bufferSize - remaining));out.clear(); // 清除缓冲区,数据不会输出remaining = out.getRemaining();System.out.println("缓冲区的剩余字节数为:" + remaining);%></body></html>
request内置对象
- request对象封装的是调用JSP页面的请求信息,它是HttpServletRequest接口的一个实例。
- 该对象的属性值只在一个请求中保存。
- 常用方法如下: | 方法声明 | 功能介绍 | | —- | —- | | String getMethod() | 返回客户端向服务器端传送数据的方式 | | String getParameter(String name) | 返回客户端向服务器端传送的参数值 | | String[] getParameterValues( String name) | 获得指定参数的所有值 | | String getRequestURI() | 获得请求地址 | | String getRemoteAddr() | 返回发送请求的客户端或最后一个代理的IP地址 | | int getRemotePort() | 返回发送请求的客户端或最后一个代理的端口号 | | String getServerName() | 获取服务器的名字 | | int getServerPort() | 获取服务器端的端口 | | void setAttribute(String name,Object o) | 在此请求中存储属性。属性在请求之间重置 | | Object getAttribute(String name) | 将指定属性的值作为对象返回,若不存在则返回空值 |
request.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 上午8:05To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>request内置对象的使用</title></head><body><%String serverName = request.getServerName();System.out.println("服务到的服务器名称为:" + serverName);int serverPort = request.getServerPort();System.out.println("获取到的服务器端口号为:" + serverPort);// 通过内置对象设置属性信息,也就是存储数据request.setAttribute("name","guanyu");%><%-- 实现转发效果,也就是服务器跳转 --%><jsp:forward page="requestTarget.jsp"></jsp:forward>></body></html>
requestTarget.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 上午8:08To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>获取request对象中的属性值</title></head><body><%= "获取到的属性值为:" + request.getAttribute("name")%> <%-- guanyu --%></body></html>
response内置对象
- response对象用于给客户端相应输出处理结果,它是HttpServletResponse接口的一个实例。
- 经常用于设置HTTP标题,添加cookie、设置响应内容的类型和状态、发送HTTP重定向和编码URL。
- 常用方法如下:
| 方法声明 | 功能介绍 |
| —- | —- |
| void addCookie(Cookie cookie)
| 添加一个Cookie对象,用于在客户端保存特定的信 息 | | void addHeader(String name, String value)
| 添加HTTP头信息,该Header信息将发送到客户端 | | boolean containsHeader(String name)
| 判断指定名字的HTTP文件头是否存在 | | void sendRedirect(String location)
| 重定向JSP文件 | | void setContentType(String type)
| 设置类型与编码方式 |
reponse.jsp
<%@ page import="java.util.Date" %><%@ page import="java.text.SimpleDateFormat" %><%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:47To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>reponse内置对象的使用</title></head><body><%// 表示每隔1秒刷新一次response.addHeader("refresh", "1");// 获取当前系统时间Date d1 = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format = sdf.format(d1);%><%= "当前时间为:" + format %></body></html>
session内置对象
- session对象表示浏览器和服务器之间的一次会话,一次会话可以包含多次请求,在多次请求之间可以借助session对象存储信息,它是HttpSession类型的一个实例。
- 该对象的属性值在一次会话范围中保存,保存在服务器端,只要不关闭浏览器,默认半个小时内都可以访问。
- 常用方法如下:
| 方法声明 | 功能介绍 |
| —- | —- |
| void setAttribute(String name, Object value)
| 使用指定的名称将对象绑定到此会话 | | Object getAttribute(String name) | 返回在此会话中用指定名称绑定的对象,如果没有对象在该名称下绑定则返回空值 |
session.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:52To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>session内置对象的使用</title></head><body><%session.setAttribute("name","liubei");System.out.println("session内置对象中的数据设置成功");%></body></html>
sessionTarget.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:54To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>获取session内置对象中的数据</title></head><body><%= "获取到的属性值为:" + session.getAttribute("name")%></body></html>
application内置对象
- application对象是一个web程序的全局变量,它是ServletContext类型的一个实例。
- 在整个服务器上保存数据,所有用户共享。
- 常用方法如下:
| 方法声明 | 功能介绍 |
| —- | —- |
| void setAttribute(String name, Object object)
| 将对象绑定到此servlet上下文中的给定属性名 | | Object getAttribute(String name)
| 返回给定名称的servlet容器属性,若没有该名称的属 性返回null |
application.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:56To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>application内置对象的使用</title></head><body><%application.setAttribute("name","zhaoyun");System.out.println("application内置对象中的数据设置成功!");%></body></html>
applicationTarget.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:57To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>application内置对象的获取</title></head><body><%= "获取到的application内置对象的属性为:" + application.getAttribute("name")%> <%-- zhaoyun --%></body></html>
pageContext内置对象
- pageContext对象是PageContext类型的对象,可以使用这个对象来管理其他的隐含对象。
- 只在一个页面中保存数据。
| 方法声明 | 功能介绍 |
| —- | —- |
| void setAttribute(String name, Object value, int scope)
| 使用适当的作用域设置指定的名称和值 | | Object getAttribute(String name, int scope)
| 返回指定作用域中名称关联的对象,若找不到则返回null | | ServletRequest getRequest() | 获取请求对象 | | ServletResponse getResponse() | 获取响应对象 | | HttpSession getSession() | 获取会话对象 | | ServletConfig getServletConfig() | 获取配置对象 | | JspWriter getOut() | 获取输出对象 | | Object getPage() | 获取页面对象 | | Exception getException() | 获取异常对象 |
pageContext.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午2:59To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>pageContext内置对象的使用</title></head><body><%pageContext.setAttribute("name","huangzhong");System.out.println("pageContext内置对象中的数据设置成功!");%><%= "获取到的pageContext内置对象中的属性值为:" + pageContext.getAttribute("name")%></body></html>
pageContextTarget.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午3:01To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>pageContext内置对象属性的获取</title></head><body><%= "获取到的pageContext内置对象中的属性值为:" + pageContext.getAttribute("name")%> <%-- null --%></body></html>
exception内置对象
- exception 对象是Throwable的实例,表示的是JSP的异常信息。
- 如果要使用它,必须将对应页面page指令的isErrorPage属性设置成true。
- 单个页面的处理方式
在web.xml中配置统一的异常处理页面。<%@page errorPage="error.jsp" %>
exception.jsp ```javascript <%— Created by IntelliJ IDEA. User: pingmao Date: 2021/4/24 Time: 下午3:04 To change this template use File | Settings | File Templates. —%> <%@ page contentType=”text/html;charset=UTF-8” language=”java” %> <%—<%@ page errorPage=”error.jsp” %>—%> <% int ia = 10; int ib = 0; System.out.println(ia / ib); // 算数异常 %><error-page><exception-type>java.lang.Throwable</exception-type><location>/error.jsp</location></error-page>
error.jsp```javascript<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午3:10To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page isErrorPage="true" %><html><head><title>异常处理的页面</title></head><body><%if (exception !=null){out.println("异常的错误信息为:" + exception.getMessage());}%></body></html>
除了可以针对单个的报错显示也可以在web.xml中进行配置,这样只要出错就会跳转到error.jsp
JavaBean组件(熟悉)
基本概念
- JavaBean 是使用 Java 语言开发的一个可重用的组件,在 JSP 开发中可以使用 JavaBean 减少重复代码,使整个 JSP 代码的开发更加简洁。
- JavaBean本质上就是Java类,通常要求如下:
属性:全部私有化,通过get和set方法进行访问。
方法:必须是public关键字修饰。
构造器 :必须有无参构造方法。
使用方式
- 使用jsp:useBean的方式创建javaBean实例
保存范围有:page|request|sessin|application,默认为page范围。<jsp:useBean id=“对象名” scope=“保存范围 class=“包名.类名” />
Student.java ```java package com.lagou.demo02;
/**
- @author 悟空
@create 2021/4/24 下午3:57 */ public class Student { private int id; private String name;
public Student() { }
public Student(int id, String name) {
this.id = id;this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
bean.jsp```jsx<%@ page import="com.lagou.demo02.Student" %><%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午3:57To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>JavaBean组件的使用</title></head><body><%-- 表示创建Student类型的对象由student引用变量负责记录 有效范围是当前页面 --%><jsp:useBean id="student" scope="page" class="com.lagou.demo02.Student"/><%-- 表示将student对象中名字为id的属性值设置为1002 --%><jsp:setProperty name="student" property="id" value="1002"/><jsp:setProperty name="student" property="name" value="guanyu"/><%// 创建Student类型的对象并设置成员变量的数值// Student student = new Student();// student.setId(1001);// student.setName("zhangfei");%><%--<%= "获取到的学号是:" + student.getId()%> <%– 1001 1002 –%><%= "获取到的姓名是:" + student.getName()%> <%– zhangfei guanyu –%>--%>学号是:<jsp:getProperty name="student" property="id"/><br/>姓名是:<jsp:getProperty name="student" property="name"/><br/></body></html>
- 使用jsp:setProperty的方式设置javaBean的属性值
beanParam.html ```html <!DOCTYPE html><jsp:setProperty name="对象名" property="属性名" value="属性值" param="参数名"/>
bean.jsp```jsx<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午4:09To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>实现前端页面传入过来参数的接收和设置</title></head><body><jsp:useBean id="student" scope="session" class="com.lagou.demo02.Student"/><jsp:setProperty name="student" property="id" param="id1"/><jsp:setProperty name="student" property="name" param="name1"/></body></html>
使用jsp:getProperty的方式获取javaBean的属性值
<jsp:getProperty name="对象名" property="属性名"/>
beanTarget.jsp
<%--Created by IntelliJ IDEA.User: pingmaoDate: 2021/4/24Time: 下午4:12To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>实现JavaBean组件中对象属性值的打印</title></head><body><jsp:useBean id="student" scope="session" class="com.lagou.demo02.Student"/><%-- 获取名字为student对象中属性为id的数值并打印 --%>经过参数赋值后获取到的学号是:<jsp:getProperty name="student" property="id"/><br/>经过参数赋值后获取到的醒目是:<jsp:getProperty name="student" property="name"/><br/></body></html>
保存范围
JavaBean的保存范围有page、request、session以及application,默认是page范围。
删除方式
<%内置对象.removeAttribute(“JavaBean的名字”);%>
```jsx <%— Created by IntelliJ IDEA. User: pingmao Date: 2021/4/24 Time: 下午7:27 To change this template use File | Settings | File Templates. —%> <%@ page contentType=”text/html;charset=UTF-8” language=”java” %> <% //表示从session对象中删除名字为student的属性 session.removeAttribute(“student”); %> <%= “删除数据成功!”%>

