首页 > 编程语言 >javaweb 8、JSP

javaweb 8、JSP

时间:2022-12-28 14:44:22浏览次数:40  
标签:java javaweb jsp JSP servlet pageContext String

什么是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>

标签:java,javaweb,jsp,JSP,servlet,pageContext,String
From: https://www.cnblogs.com/zibinchen/p/17010110.html

相关文章

  • jsp的9大内置对象
    1.什么是内置对象内置对象!!! 2.9大内置对象  out对象out对象类型,JspWriter类,相当于带缓存的PrintWriterPrintWriter:    wrier(内容):直接向浏览器写出内容......
  • JavaWeb
    JavaWeb1、基本概念1.1前言静态webhtmlcss动态web技术栈:Servlet/JSP、ASP、php动态web资源开发技术统称为JavaWeb1.2web应用程序:可以提供浏览......
  • 前端 CST和GMT+0800时间转换(js/vue/react/jsp通用)
    this.beginTime="2031-03-2000:00:00"this.endTime="2031-03-2000:00:00"this.beginTime=newDate(this.beginTime)......
  • JavaWeb项目实战(3)软件快速下载
    前两篇文章里提到的所有文件均可在这里下载:​​https://www.lmonkey.com/tools/java​​......
  • JavaWeb项目实战(1)数据库环境搭载
    学业不精,趁放假看网课学习JavaWeb,为了激励自己学习,全程记录学习过程1.安装数据库MySQL官网:​​https://www.mysql.com/​​我下载的是mysql-8.0.16-winx64版本。将文件解压......
  • JavaWeb项目实战(2)安装JDK和Tomcat以及Eclipse构造开发环境
    1.安装JDK下载JDK,官网下载地址​​https://www.oracle.com/java/technologies/javase-jdk8-downloads.html​​这是我下载的版本双击exe文件,安装到你喜欢的地方,我放在了C......
  • JSP的基本语法
    文章目录​​一.JSP(JavaServerPage)概述​​​​1.定义​​​​2.JSP是一个动态网页​​​​3.注意事项​​​​二.JSP页面元素​​​​1.注释​​​​(1).HTML注......
  • javaweb基本概念
        web应用程序编写完毕后,若要提供给外界访问:需要一个服务器统一管理。       ......
  • javaweb 7、Cookie、Session
    Cookie创建项目javaweb-session-cookie2.补全目录java、resouces更新web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="https://jakarta.ee/xml/......
  • 基于JSP的网上订餐管理系统的设计与实现(包调试成功)
    第1页毕业设计(论文)题目:基于​JSP的网上订餐管理系统​的设计与实现毕业设计(论文)要求及原始数据(资料):1.综述目前国内外网上订餐管理系统的现状;2.深入了解网上订餐系统的管理方式......