一、什么是jsp
Java Server Pages:Java服务器端页面,也和servlet一样,用于动态Web技术!
最大特点:
- 写jsp就像在写HTML
- 区别:
- html只给用户提供静态数据
- jsp页面中可以嵌入Java代码,为用户提供动态数据
二、JSP原理
思路:jsp到底怎么执行
- 代码层面没有问题
- 服务器内部工作
tomcat中由一个work目录;
IDEA中使用tomcat的会在IDEA中生产一个work目录,默认路径:C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2022.1\tomcat\5cc99375-a568-45d2-a1c6-14392b8563a1\work
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet
JSP最后也会被转化成java类
JSP本质上就是servlet
//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//JSPServlet
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
1、判断请求
2、内置一些对象
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 //响应
3、输出页面前增加的代码
response.setContentType("text/html"); //设置响应的页面类型
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
4、以上的这些对象,可以在jsp页面中直接使用
在JSP页面中:
- 只要是java代码会原封不动的输出
- 如果是html代码,就会被转换为:
out.write(“< html>\r\n”);
三、JSP基础语法
任何语言都有自己的语法,jsp作为Java技术的一种应用,它拥有一些自己的扩充语法(了解即可),java语法都支持
JSP表达式
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%-- JSP表达式
作用:用来将程序输出到客户端
<%= 变量或者表达式%>
--%>
<%= new java.util.Date()%>
</body>
</html>
jsp脚本片段
<%
int sum=0;
for (int i = 0; i < 100; i++) {
sum+=i;
}
out.print("<h1>Sum="+sum+"</h1>");
%>
脚本片段的再实现
<%
int x= 10;
out.println(x);
%>
<p>这是一个jsp文档</p>
<%
int y= 20;
out.println(y);
%>
<hr>
<%-- 在代码中嵌入html元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>hello,word <%=i%></h1>
<%
}
%>
JSP声明
<%!
static {
System.out.println("loading servlet");
}
private int globalVar = 0;
public void jjl(){
System.out.println("进入方法jjl");
}
%>
JSP声明:会被编译到JSP生产到java的类中!其他的,就会被生成到jspservice方法中!
在jsp中嵌入java代码即可
四、JSP指令
1、<%@page ······ %>
1、定制错误页面
-
通过jsp指定跳转的错误页面
-
通过web.xml指定跳转的错误页面
<?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>
2、<%@include file=" "%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--引入公用头部页面和尾部页面--%>
<%--方法1:会将两个页面合二为一,这种方式公用页面和主页面不能有相同的变量名或者方法名--%>
<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%>
<hr>
<%-- JSP标签 --%>
<%--方法2:拼接页面(常用),这种方式公用页面和主页面互不影响--%>
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<jsp:include page="/common/footer.jsp"/>
</body>
</html>
五、JSP9大内置对象
- PageContext (存东西)
- Request (存东西)
- Response
- Session (存东西)
- Application 【SerlvetContext】(存东西)
- config【SerlvetConfig】
- out
- page
- exception
1、四种存东西方式的区别
- pageContext.setAttribute(“name1”,“jjl1”);//保存的数据只一个页面中有效
- request.setAttribute(“name2”,“jjl2”);//保存的数据,只在一次请求中有效,请求转发会携带这些数据
- request:用于客户端向服务器发送请求,产生的数据,用户看完就没用了,比如:新闻,用户看完没用的!
- session.setAttribute(“name3”,“jjl3”);//保存的时间,在一次会话中有效,从浏览器打开到浏览器关闭
- session:用于客户端向服务器发送请求,产生的数据,用户看完一会还有用,比如购物车;
- application.setAttribute(“name4”,“jjl4”);//保存的数据,在服务器中有效,只启动服务器到关闭服务器
- application:用于客户端向服务器发送请求,产生的数据,用户看完了其他用户还用,如:购物车
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--内置对象--%>
<%
pageContext.setAttribute("name1","jjl1");//保存的数据只一个页面中有效
pageContext.setAttribute("hello","hello1",PageContext.SESSION_SCOPE);//设置作用域等价于session.setAttribute的作用域
request.setAttribute("name2","jjl2");//保存的数据,只在一次请求中有效,请求转发会携带这些数据
session.setAttribute("name3","jjl3");//保存的时间,在一次会话中有效,从浏览器打开到浏览器关闭
application.setAttribute("name4","jjl4");//保存的数据,在服务器中有效,只启动服务器到关闭服务器
%>
<%--脚本片段中的代码,会被原封不动的生成到JSP.java中
要求:这里面的代码,必须保证Java语法的正确性--%>
<%
//通过pageContext取出,通过寻找方式
//从底层到高层
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
String name5 = (String) pageContext.findAttribute("name5");
%>
<%--使用el表达式输出 ${}--%>
<h1>取出的值为:</h1>
<h1>${name1}</h1>
<h1>${name2}</h1>
<h1>${name3}</h1>
<h1>${name4}</h1>
<%--使用el表达,会自动过滤掉不存在的内容--%>
<h1>${name5}</h1>
</body>
</html>
六、JSP标签、JSTL标签、EL表达式
1、EL表达式 ${}:
<!-- JSTL表达式的依赖 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
EL表达式 ${}:
- 获取数据
- 执行运算
- 获取web开发的常用对象
- 调用java方法
2、JSP标签
<%-- 转发时携带参数--%>
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="jjl"></jsp:param>
<jsp:param name="age" value="12"></jsp:param>
</jsp:forward>
3、JSTL表达式
JSTL标签库的使用就是为了弥补HTML标签的不足,它自定义许多标签,可以供我们使用,标签的功能和Java代码一样
- 核心标签(掌握部分即可)
- 格式化标签
- SQL标签
- XML 标签
JSTL标签使用步骤:
- 引入对应的taglib
- 使用其中的方法
- 在Tomcat中也需要导入jstl-api-1.2.jar和standard-1.1.2.jar
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,才能使用JSTL标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>if测试</h4>
<hr>
<form action="coreif.jsp" method="get">
<%--
EL表达式获取表单中的数据
${param.参数名}
--%>
<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}"></c:out>
</body>
</html>
条件判断
<body>
<%--定义一个变量score,值为85--%>
<c:set var="score" value="62"/>
<c:choose>
<c:when test="${score>=90}">
你的成绩优秀
</c:when>
<c:when test="${score>=80}">
你的成绩一般
</c:when>
<c:when test="${score>=60}">
你的成绩良好
</c:when>
<c:when test="${score<60}">
你的成绩不及格
</c:when>
</c:choose>
</body>
遍历
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"张三");
people.add(1,"张si");
people.add(2,"张5");
people.add(3,"张六");
people.add(4,"张six");
request.setAttribute("list",people);
%>
<%--
var,每次遍历出来的变量
items,要遍历的对象
--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/><br>
</c:forEach>
<hr>
<%--
begin="1" 从1开始
end="3" 到3结束
step="2" 步长2
--%>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
<c:out value="${people}"/><br>
</c:forEach>
</body>
</html>
七、JavaBean
实体类
javaBean有特定的写法
- 必须有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
- 一般用来做数据库的字段映射
- ORM:对象关系映射
- 一个表对应一个类
- 字段对应属性
- 行记录对应对象
实体类
package com.jjl.pojo;
//实体类,一般都是和数据库中的表结构一一对应
public class People {
private int id;
private String name;
private int age;
private String address;
public People() {
}
public People(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "people{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
用JSP操作实体类
<%@ page import="com.jjl.pojo.People" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
// People people = new People();
// people.setAddress();
%>
<%--下面这行等于java中的<People people = new People();>--%>
<jsp:useBean id="people" class="com.jjl.pojo.People" scope="page"/>
<%--传递值--%>
<jsp:setProperty name="people" property="address" value="成都"/>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="age" value="30"/>
<jsp:setProperty name="people" property="name" value="张三"/>
<%--获取值--%>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
id:<jsp:getProperty name="people" property="id"/>
地址:<jsp:getProperty name="people" property="address"/>
</body>
</html>