JSP

JSP

1. 什么是JSP

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

最大的特点:

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

2. JSP原理

思路:JSP到底怎么执行

  • 代码层面没有任何问题

  • 服务器内部工作

    • Tomcat中有一个work目录

    • IDEA中使用Tomcat会在IDEA的Tomcat中产生一个work目录

      C:UsersWangAppDataLocalJetBrainsIntelliJIdea2020.2 omcatUnnamed_untitledwork

      我的电脑的地址:

      C:UsersWangAppDataLocalJetBrainsIntelliJIdea2020.2 omcatUnnamed_Cookie-and-Session-StudyworkCatalinalocalhostROOTorgapachejsp

      发现页面转变成了Java程序

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

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

  • JSP本质上就是一个Servlet:继承了HttpServlet

  • JSP源码分析

//初始化
public void _jspInit() {
}

//销毁
public void _jspDestroy() {
}

//JSPService
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
    throws java.io.IOException, javax.servlet.ServletException {

  if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
    final java.lang.String _jspx_method = request.getMethod();
    if ("OPTIONS".equals(_jspx_method)) {
      response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
      return;
    }
    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
      response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
      return;
    }
  }
  1. 判断请求(get还是post)

  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;
    

    以上的这些对象,我们可以在JSP页面中直接使用

img

在JSP页面中:

  • 只要是JAVA代码,就会原封不动的输出

  • 如果是HTML代码,就会被转换为:

    out.write("<html>
    ");
    

    这样的格式,输出到前端

3. JSP基础语法

任何语言都有自己的语法

JSP作为Java技术的一种应用,它拥有一些自己扩充的语法(了解,知道即可!)

Java所有语法都支持!

1. JSP表达式

  <%--JSP表达式
    作用:用来将程序的输出,输出到客户端
    <%= 变量或表达式%>
  --%>
  <%=new java.util.Date()%>

2. JSP脚本片段

<%--JSP脚本片段--%>
<%
  int sum = 0;
  for (int i = 1; i <= 100; i++) {
    sum += i;
  }
  //可以直接用out.println输出,里面可以写html代码
  out.println("<h1>Sum =" + sum + "</h1>");
%>

脚本片段的再实现

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

  <hr>

  <%--在代码中嵌入HTML元素
      将花括号分开在两个JSP片段中,这样中间可以直接写HTML代码
      同时,HTML代码中也可以嵌套JSP片段--%>
  <%
    for (int i = 0; i < 5; i++) {

  %>
    <h1>Hello, World! <%=i%></h1>
  <%
    }
  %>

3. JSP声明

<%--JSP声明
<%! 声明%>
--%>
<%!
  static {
    System.out.println("loading Servlet!");
  }
  private int globalVar = 0;
  public void wang() {
    System.out.println("进入了方法wang()");
  }
%>

JSP声明:会被编译到JSP生成的Java类的类中!(作用域更高)

其他的(表达式,脚本片段)就会被生成到_jspService方法中!

在JSP中嵌入Java代码即可

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

4. JSP指令

1. 定制错误页面

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

1. 利用JSP

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 14:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%--定制错误页面--%>
<%--错误页面走error文件夹下的500.jsp--%>
<%@page errorPage="error/500.jsp" %>
<head>
    <title>Title</title>
</head>
<body>

<%
    int x = 1 / 0;
%>

</body>
</html>

500.jsp

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 14:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--显示图片--%>
<img src="../img/500.png" alt="500">

</body>
</html>

2. 利用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>

404.jsp

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 15:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<img src="../img/404.png" alt="404">

</body>
</html>

2. 使用公共资源

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <%--@include会将两个页面合二为一: 将多个jsp文件合为一个,如果存在同名的对象会报错--%>
    <%@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>

footer.jsp

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>

header.jsp

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 15:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Header</h1>

5. 九大内置对象

  • PageContext 存东西
  • Request 存东西
  • Response
  • Session 存东西
  • Application [ServletContext] 存东西
  • config [ServletConfig]
  • out
  • page 不用了解
  • exception

存东西的优先级

pageContext.setAttribute("name1", "wamg_1");    //保存的数据只在一个页面中有效
request.setAttribute("name2", "wamg_2");        //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3", "wamg_3");        //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4", "wamg_4");    //保存的数据只在服务器中有效,从打开服务器到关闭服务器

通过pageContext取出

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 16:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--内置对象--%>

<%
    pageContext.setAttribute("name1", "wamg_1");    //保存的数据只在一个页面中有效
    request.setAttribute("name2", "wamg_2");        //保存的数据只在一次请求中有效,请求转发会携带这个数据
    session.setAttribute("name3", "wamg_3");        //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
    application.setAttribute("name4", "wamg_4");    //保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>

<%--脚本片段中的代码会被原封不动的生成到XXX.jsp.java中--%>
<%--要求:这里面的代码必须保证java语法的正确性--%>
<%
    //从pageContext取出,我们通过寻找的方式来
    //从底层到高层(作用域):page-->request-->session-->application
    //JVM:双亲委派机制
    String name1 = (String) pageContext.getAttribute("name1");
    String name2 = (String) pageContext.getAttribute("name2");
    String name3 = (String) pageContext.getAttribute("name3");
    String name4 = (String) pageContext.getAttribute("name4");
    String name5 = (String) pageContext.getAttribute("name5");  //不存在
%>

<%--使用EL表达式输出
    ${}
    会过滤掉不存在的值
--%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>

</body>
</html>

作用域对于对象的影响

注意,如果使用转发,此时可以取到request的值!这是由于请求也被转发了.但是page的值取不到了,因为不在同一个页面上

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 16:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    //从pageContext取出,我们通过寻找的方式来
    //从底层到高层(作用域)
    String name1 = (String) pageContext.getAttribute("name1");
    String name2 = (String) pageContext.getAttribute("name2");
    String name3 = (String) pageContext.getAttribute("name3");
    String name4 = (String) pageContext.getAttribute("name4");
    String name5 = (String) pageContext.getAttribute("name5");  //不存在
%>

<%--使用EL表达式输出
    ${}
    会过滤掉不存在的值
--%>
<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>

<%--此时由于作用域的原因,只能取出name3和name4--%>

</body>
</html>

利用pageContext的scope属性实行其他三个内置对象的存取

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 16:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--
    //scope: 作用域
    public void setAttribute(String name, Object attribute, int scope) {
        switch(scope) {
        case 1:
            this.mPage.put(name, attribute);
            break;
        case 2:
            this.mRequest.put(name, attribute);
            break;
        case 3:
            this.mSession.put(name, attribute);
            break;
        case 4:
            this.mApp.put(name, attribute);
            break;
        default:
            throw new IllegalArgumentException("Bad scope " + scope);
        }

    }
--%>

<%
    pageContext.setAttribute("hello1", "hello1", PageContext.SESSION_SCOPE);
    //等价于
    session.setAttribute("hello1", "hello1");
%>

</body>
</html>

利用pageContext实现转发

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 17:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    //以下是jsp中实现转发的方式
    pageContext.forward("/index.jsp");
    //以下是Servlet中实现的转发
    request.getRequestDispatcher("/index.jsp").forward(request, response);
%>

</body>
</html>

一些应用场景

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

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

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

6. JSP标签&JSTL标签&EL表达式

1. EL表达式(Expression Language)

要导入以下两个包

<!--JSTL表达式的依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>
    <artifactId>jstl-api</artifactId>
    <version>1.2</version>
</dependency>
<!--Standard标签库-->
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

EL表达式的格式:${}

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

2. JSP标签

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 17:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>1</h1>

<%--<jsp:include page=""/>--%>

<%--携带参数的访问,url如下--%>
<%--http://localhost:8080/jsptag.jsp?value1=value1&value2=value2--%>
<jsp:forward page="jsptag2.jsp">
    <jsp:param name="value1" value="value1"/>
    <jsp:param name="value2" value="value2"/>
</jsp:forward>

</body>
</html>

在jsptag2.jsp中取出参数,用JSP的request对象

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/19
  Time: 17:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>2</h1>

<%--取出参数--%>
<p>值1:<%=request.getParameter("value1")%></p>
<p>值2:<%=request.getParameter("value2")%></p>

</body>
</html>

3. JSTL表达式

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

1. 核心标签(掌握部分)

JSTL 核心标签库标签共有13个,功能上分为4类:

1.表达式控制标签:out、set、remove、catch

2.流程控制标签:if、choose、when、otherwise

3.循环标签:forEach、forTokens

4.URL操作标签:import、url、redirect

2. 使用步骤

  • 引用对应的taglib
  • 使用其中的方法
  • 在tomcat中也需要引入JSTL的包(jstl和standard的jar包),否则会报错JSTL解析错误的包

3. 测试代码

if

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/20
  Time: 9:43
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

switch

<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/20
  Time: 10:12
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

<%--定义一个变量score,值为85--%>
<c:set var="score" value="85"/>

<%--c:chose相当于switch--%>
<%--c:when是条件--%>
<c:choose>
    <c:when test="${score >= 90}">
        你的成绩为优秀
    </c:when>
    <c:when test="${score >= 80}">
        你的成绩为一般
    </c:when>
    <c:when test="${score >= 70}">
        你的成绩为良好
    </c:when>
    <c:when test="${score >= 60}">
        你的成绩为及格
    </c:when>
</c:choose>

</body>
</html>

foreach

<%@ page import="java.util.ArrayList" %>
<%--
  Created by IntelliJ IDEA.
  User: Wang
  Date: 2020/8/20
  Time: 10:17
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>

<%
    ArrayList<String> people = new ArrayList<>();
    people.add("张三");
    people.add("李四");
    people.add("王五");
    people.add("赵六");
    people.add("钱七");
    request.setAttribute("list", people);
%>

<%--var,每一次遍历出来的变量--%>
<%--items,要遍历的对象--%>
<c:forEach var="people" items="${list}">
    <c:out value="${people}"/>
    <br>
</c:forEach>
<%--
var     每一次遍历出来的变量
items   要遍历的对象
begin   从哪里开始(索引从0开始)
end     到哪里
step    步长
--%>
<c:forEach begin="1" end="3" step="2" var="people" items="${list}">
    <c:out value="${people}"/>
    <br>
</c:forEach>

</body>
</html>
原文地址:https://www.cnblogs.com/wang-sky/p/13533682.html