ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

JavaEE学习笔记 2024-1-15 --JSP

2026/8/5 20:40:56 拓冰建站 浏览量
JavaEE学习笔记 2024-1-15 --JSP

« 上一篇

个人整理非商业用途,欢迎探讨与指正!!


文章目录

    • 6.JSP
      • 6.1JSP的基本使用
      • 6.2JSP的使用规则
      • 6.3JSP内置对象
      • 6.4错误页面的配置
        • 6.4.1每个页面单独设置
        • 6.4.2统一的错误页面设置
      • 6.5域对象


6.JSP

动态页面
Java Server Pages 基本Servlet的java服务页面(动态页面)
JSP之上可以编写java和html代码

6.1JSP的基本使用

<!-- jsp脚本 -->
<%@page import="com.qf.pojo.Book"%>
<%@ 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>
<a href="xxxServlet">跳转</a>
<!-- 在如下代码中编写java代码 -->
<%String str = "helloworld";/* 打印到后端控制台 */System.out.println(str);/* 打印到前端页面 */out.println(str);out.println("我叫王成輝");
%><%/* 在一个类的根部使用alt+/进行导包 */Book book = new Book(1,"钢铁是怎么样炼成","20.02");out.println(book);
%>
<br>
<!-- 直接输出某个内容 -->
<%=book %>
</body>
</html>

6.2JSP的使用规则

JSP和HTML一样,都可以作为视图,给用户展示页面信息的
不同点是JSP可以写Java代码
JSP的后缀为.jsp
使用<% java代码 %>作为语法
使用<%=变量 %>可以直接输出一个变量的值
JSP的本质是一个Java类
服务器可以将JSP自动的转换为Java类,在服务器的work文件夹下

JSP转换的核心代码

public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)throws java.io.IOException, javax.servlet.ServletException {final javax.servlet.jsp.PageContext pageContext;javax.servlet.http.HttpSession session = null;final javax.servlet.ServletContext application;final javax.servlet.ServletConfig config;javax.servlet.jsp.JspWriter out = null;final java.lang.Object page = this;

6.3JSP内置对象

可以在JSP页面上直接使用的对象,在_jspService中已经定义好的对象
内置对象一共有9个

内置对象名类型说明
requestHttpServletRequest表示当前页面的请求***
responseHttpServletResponse表示当前页面的响应
pageContextPageContextJSP的上下文,可以获取到当前JSP的任何信息
sessionHttpSession表示浏览器和服务器之间的一次会话***
applicationServletContext表示当前的服务器,服务器上下文**
configServletConfigServlet类的信息
outJspWriterJSP页面的输出流,等价与<%= %>
pageObject当前页面对象(Object对象)
exceptionThrowable表示当前页面的错误信息

6.4错误页面的配置

6.4.1每个页面单独设置
<!-- 将当前页面声明为错误页面,所有的错误信息都向当前的页面跳转 -->
<%@ page isErrorPage="true"%>
<!-- 有错误的页面位置跳转 -->
<%@ page errorPage="error.jsp" %>
6.4.2统一的错误页面设置

在web.xml中进行设置

<error-page><error-code>404</error-code><location>/404.html</location>
</error-page><error-page><error-code>500</error-code><location>/500.html</location>
</error-page>

6.5域对象

JSP域对象四个:pageContext request session applicaion
Servlet域对象三个:request session application
域对象可以存储数据

<%// 表示当前的页面pageContext.setAttribute("name", "张三");// 表示一次请求request.setAttribute("age", 19);// 表示一次会话:浏览器从开启到关闭的过程session.setAttribute("hobby", "学习");// 表示服务器从开启到关闭application.setAttribute("gender", "男");
%>
//		request对象
request.setAttribute("age", 28);
//		session对象
HttpSession session = request.getSession();
session.setAttribute("hobby", "吃");
//		applicaion对象
ServletContext application = request.getServletContext();
application.setAttribute("gender", "未知");