1. JSP简介
1.1 简 介
JavaServer Pages:Java 服务器页面,类似 Servlet ,用于动态 Web 技术。
特点:写 JSP 就像写 HTML 。
与 HTML 的区别:HTML 只给用户提供静态的数据;JSP 页面中可以嵌入 JAVA 代码,为用户提供动态数据。
1.2 原 理
浏览器向服务器发送请求,不管访问什么资源,本质都是在访问 Servlet ,因为 JSP 最终会被转换成一个 Java 类,所以 JSP 的本质是一个 Servlet 。
源码剖析:
- 判断请求
- 内置一些对象
final javax.servlet.jsp.PageContext pageContext; //页面上下文
javax.servlet.http.HttpSession session = null; //session
final javax.servlet.ServletContext application; //applicationContext
final javax.servlet.ServletConfig config; //config
javax.servlet.jsp.JspWriter out = null; //out
final java.lang.Object page = this; //page,当前页
HttpServletRequest request; //请求
HttpServletResponse response; //响应
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
- 输出页面前增加一些代码
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this,resquest,response,null,true,8192,true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
-
第2步中的内置对象 ,可以在JSP页面中直接使用
-
原理示意图
-
JSP页面中:
只要是 JAVA 代码就会保持原样输出;如果是 HTML 代码,就会被转换为out.write("<html>\r\n");
的格式输出到前端
2. JSP基础语法和指令
新建的 web 项目中 pom.xml 文件需要引入的依赖
<!--添加Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--添加JSP依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--添加JSTL表达式的依赖-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--添加standard标签库-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
2.1 基础语法
jsp表达式
// 格式
<%= 变量或者表达式%>
//示例
<%= new java.util.Date()%>
作用:用来将程序的输出,输出到客户端
jsp脚本片段
//格式
<% 内容 %>
//示例
<%
int data = 0;
for (int i = 0; i <= 100; i++) {
data += i;
}
out.println("<h1>data =" + data + "</h1>");
%>
脚本片段的再实现
<%--在代码中嵌入HTML元素--%>
<%
for (int j = 0; j < 5; j++) {
%>
<h1> Hello World <%=j%> </h1>
<%
}
%>
jsp声明
//格式
<%! 内容%>
//示例
<%!
static {
System.out.println("loading Servlet!");
}
private int globalNum = 0;
public void initMethod() {
System.out.println("进入自定义初始化方法!");
}
%>
jsp 声明会被编译到 jsp 生成的 java 类中,其它的则会被生成到 _jspService 方法中
2.2 指令
page指令:<%@ page errorPage= xxxxx %>
自定义错误页面
- 方式一
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page errorPage="error/500.jsp" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<% int i = 1 / 0; %>
</body>
</html>
- 方式二
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
</web-app>
include指令:<%@ page errorPage= xxxxx %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--@include会将两个页面合二为一--%>
<%@ include file="common/header.jsp" %>
<h3>输出内容</h3>
<%@ include file="common/footer.jsp" %>
<hr>
<%--jsp:include:拼接页面,本质还是三个页面--%>
<jsp:include page="/common/header.jsp"/>
<h3>输出内容</h3>
<jsp:include page="/common/footer.jsp"/>
</body>
</html>
3. JSP内置对象及作用域
3.1 九大内置对象
- PageContext:用于存东西
- Request:用于存东西
- Response
- Session:用于存东西(相当于ServletContext)
- Application:用于存东西(相当于ServletConfig)
- config
- out
- page
- exception
3.2 作用域
- pageContext.setAttribute(“userName01”,“yuhuofei01”); ——保存的数据只在一个页面中有效
- request.setAttribute(“userName02”,“yuhuofei02”); ——保存的数据只在一次请求中有效,请求转发会携带这个数据
- session.setAttribute(“userName03”,“yuhuofei03”); ——保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
- application.setAttribute(“userName04”,“yuhuofei04”); ——保存的数据只在服务器中有效,从打开服务器到关闭服务器
作用域从低到高:pageContext——>request——>session——>application
4. JSP 标签、JSTL 标签、EL 表达式
4.1 EL表达式——${}
- 获取数据
- 执行运算
- 获取 web 开发的常用对象
- 调用 java 方法
4.2 JSP 标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--jsp:include--%>
<%--jsp:forward--%>
<%--jsp:param--%>
<%--http:localhost:8080/index.jsp?name=yuhuofei&16--%>
<jsp:forward page="/index.jsp">
<jsp:param name="name" value="yuhuofei"/>
<jsp:param name="age" value="16"/>
</jsp:forward>
</body>
</html>
4.3 JSTL 标签
JSTL 标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以提供用户使用,标签的功能和 Java 代码一样!可以详细参考菜鸟教程关于 JSTL 的简介
- 核心标签(掌握部分)
- 格式化标签
- SQL标签
- XML标签
使用步骤:
- 在每个 JSP 文件中的头部包含 taglib 标签,如 <%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
- 使用其中的方法
- 在 tomcat 引入 jstl 的包,否则会报错:jstl 解析错误
<body>
<h4>if测试</h4>
<hr>
<form action="index.jsp" method="get">
<input type="text" name="userName" value="${param.userName}">
<input type="submit" value="登录">
</form>
<c:if test="${param.userName=='admin'}" var="isAdmin">
<c:out value="管理员登录成功!"/>
</c:if>
<c:out value="${isAdmin}"/>
</body>
5. JavaBean
所谓的 JavaBean 指的是有以下特定写法的实体类
- 必须要有一个无参构造方法
- 属性必须私有化
- 必须有对应的 get/set 方法
一般用来和数据库的字段做映射,即ORM(对象关系映射)
- 表——>类
- 字段——>属性
- 行记录——>对象
通过操作 JavaBean 实现对数据库字段的增删改查