Loading

javaweb 8、JSP

什么是JSP

Java ServicePages:java服务器端页面,也和Servlet一样,用于动态web技术

最大特点

  • 写JSP就像在写HTML
  • 区别:
    • HTML只给用户提供静态的数据
    • JSP页面中科院嵌入java代码,为用户提供动态数据

JSP原理

思路:JSP到底怎么执行的

  • 代码层面没有任何问题
  • 服务器内部工作
    • tomcat中有一个work目录
    • IDEA中使用Tomcat的会在IDEA中生成一个work目录

浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet

JSP最终也会被转化成为一个Java类

  • JSP本质上就是一个Servlet
// 初始化
    public void _jspInit() {
        
    }
// 销毁
    public void _jspDestroy() {
        
    }

// JSPService
    public void _jspService(HttpServletRequest request HttpServletResponse response) {
        
    }

  1. 判断请求

  2. 内置一些对象

final jakarta.servlet.jsp.PageContext pageContext; // 页面上下文
final jakarta.servlet.ServletContext application; // session
final jakarta.servlet.ServletConfig config; // applicationContext
jakarta.servlet.jsp.JspWriter out = null; // out
final java.lang.Object page = this; // page 当前页
HttpServletRequest request // 请求
HttpServletResponse response // 响应

3.输出页面前增加的代码

response.setContentType("text/html; charset=UTF-8"); //设置响应的页面类型
pageContext = _jspxFactory.getPageContext(this, request, response,
  		null, false, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
out = pageContext.getOut();
_jspx_out = out;
  1. 以上的这些对象我们可以在JSP页面中直接使用

在JSP页面中
只要是java代码就会原封不动的输出
如果是HTML代码,就会被转换为

out.write("<html>\r\n");

这样的格式输出到前端

JSP基础语法

建一个maven项目

加入maven依赖第三方库

pom.xml

<dependencies>
    <!-- Servlet 依赖-->
    <dependency>
        <groupId>jakarta.servlet</groupId>
        <artifactId>jakarta.servlet-api</artifactId>
        <version>5.0.0</version>
    </dependency>
    <!-- JSP依赖-->
    <dependency>
        <groupId>jakarta.servlet.jsp</groupId>
        <artifactId>jakarta.servlet.jsp-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- JSTL表达式的依赖-->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>jakarta.servlet.jsp.jstl</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!-- standard 标签库-->
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>

IDEA配置tomcat

启动tomcat

http://localhost:8080/

  • 任何语言都有自己的语法,java中有,JSP作为java技术的一种应用,它拥有一些自己扩充的语法(了解、知道即可)
  • java所有语法都支持

JSP表达式

<%--JSP表达式

作用: 用来将程序的输出,输出到客户端
<%= %>
--%>
<%= new java.util.Date()%>

JSP脚本片段

<%-- jsp 脚本片段--%>
<%
int sum = 0;
for (int i = 0; i <=100; i++) {
  sum+=i;
}
out.println("<h1>Sum="+sum+"</h1>");
%>

JSP脚本片段再实现

<%
int x = 10;
out.println(x);
%>
<p>这是一个JSP文档</p>
<%
int y = 2;
out.println(y);
%>

<%--在代码嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>Hello world <%=i%></h1>
<%
}
%>

JSP声明

<%!
static {
  System.out.println("Loading Servlet!");
}

private int globalVar = 0;

public void jjsp(){
  System.out.println("进入了方法jjsp!");
}
%>

JSP声明:会被编译到JSP生成java的类中,其他的,就会被生成到_jspService方法!

在JSP中,嵌入java代码即可!

<%%>
<%=%>
<%!%>

<%--注释--%>

JSP的注释,不会在客户端显示,HTML就会!

JSP指令

<%@page args...%>
<%@include file=""%>

<%--@include会将两个页面合二为一--%>
<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%>

<hr>

<%--jsp标签
jsp: include:拼接页面,本质还是三个
--%>
<jsp:include page="common/header.jsp"/>
<h1>网页主体</h1>
<jsp:include page="common/footer.jsp"/>

指定报错页

web目录下创建error目录
创建文件

500.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>自定义500错误页面</h1>

<img src="https://img.zcool.cn/community/01c1b35bebe8bca8012092522efdb9.jpg@1280w_1l_2o_100sh.jpg" alt="500">

</body>
</html>

创建文件 jsp2.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%--定制错误页面--%>

<%@ page errorPage="error/500.jsp" %>

<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    int x = 1/0;
%>

</body>
</html>

启动tomcat
访问:http://localhost:8080/jsp2.jsp

web.xml配置报错页

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>
    
</web-app>

在error目录下创建404.jsp文件
404.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<img src="https://img.zcool.cn/community/01ab2b55c56cf36ac7255808357300.jpg" alt="404">

</body>
</html>

启动浏览器,访问:http://localhost:8080/jsp2.jsp3456

嵌入页面

web目录下创建common文件夹
header.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是header</h1>

footer.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>

web下创建jsp3.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <%--@include会将两个页面合二为一--%>
    <%@include file="common/header.jsp"%>
    <h1>网页主体</h1>
    <%@include file="common/footer.jsp"%>

    <hr>

    <%--jsp标签
    jsp: include:拼接页面,本质还是三个
    --%>
    <jsp:include page="common/header.jsp"/>
    <h1>网页主体</h1>
    <jsp:include page="common/footer.jsp"/>

</body>
</html>

启动tomcat,访问:http://localhost:8080/jsp3.jsp

9大内置对象

  • PageContext 存东西
  • Request 存东西
  • Response
  • Session 存东西
  • Application [Servlet application] 存东西
  • config [Servlet config]
  • out
  • page
  • exception

pageContextDemo01.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--内置对象--%>
<%
    pageContext.setAttribute("name1","冰渣渣1号"); // 保存数据只在一个页面中有效
    request.setAttribute("name2","冰渣渣2号"); // 保存的数据只在一次请求中有效,请求转发会携带这个数据
    session.setAttribute("name3","冰渣渣3号"); // 保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
    application.setAttribute("name4","冰渣渣4号"); // 保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>

<%
    //通过pageContext取出,我们通过寻找的方法来
    // 从底层到高层(作用域)page>request>session>application
    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>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>

</body>
</html>

request: 客户端向服务器发送请求,产生的数据,用户看完就没用了,比如:新闻,用户看完就没用了

session: 客户向服务器发送请求,产生的数据,用户用完一会还有用,比如:购物车

application:客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还能使用,比如:聊天数

JSP标签、JSTL标签、EL表达式

<!-- JSTL表达式的依赖-->
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jakarta.servlet.jsp.jstl</artifactId>
    <version>2.0.0</version>
</dependency>
<!-- standard 标签库-->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

EL表达式:${}

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象
  • 调用java方法

JSP标签

<%--jsp:include--%>

<%--
https://locahost:8080/jsptag.jsp?name=binzaza&age=19
--%>

<jsp:forward page="jsptag2.jsp">
    <jsp:param name="name" value="binzaza"></jsp:param>
    <jsp:param name="age" value="19"></jsp:param>
</jsp:forward>

JSTL表达式

JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和java代码一样!

核心标签

<%@ 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>

<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}"/>

</body>
</html>
posted @ 2022-12-28 14:38  Binzichen  阅读(31)  评论(0编辑  收藏  举报