JSP

JSP

JSP全称Java Server Pages,是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。

JSP是一种Java servlet,主要用于实现Java web应用程序的用户界面部分。网页开发者们通过结合HTML代码、XHTML代码、XML元素以及嵌入JSP操作和命令来编写JSP。

JSP通过网页表单获取用户输入数据、访问数据库及其他数据源,然后动态地创建网页。

JSP书写规范

  • jsp文件中不可以直接写java语句,需要使用jsp语法

java语句格式

<% int num = 100; %>

条件判断

<%
    if (num < 6) {
%>
        <h1>小了</h1>
<%
    } else if (num > 6) {
%>
        <h1>大了</h1>
<%
    }
%>

循环语句

<!--循环语句-->
<%
    for (int i=0; i<num;i++){
%>
        <h2><%=i%></h2>
<%
    }
%>

变量格式

<%=num%>

对象使用

<%
    for (Student stu:list){
%>
        <h2><%=stu.getSid()%></h2>
        <h2><%=stu.getName()%></h2>
<%
    }
%>

事例:

test.jsp

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

<!--条件判断语句-->
<%
    if (num < 6) {
%>
        <h1>小了</h1>
<%
    } else if (num > 6) {
%>
        <h1>大了</h1>
<%
    }
%>

<!--循环语句-->
<%
    for (int i=0; i<num;i++){
%>
        <h2><%=i%></h2>
<%
    }
%>
</body>
</html>

Test.java

package lwd.javaweb;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Test extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        request.getRequestDispatcher("/test.jsp").forward(request, response);
    }
}

JSP内置对象

  • ServletContext application: 全局作用域对象
  • HttpSession session 会话作用于对象
  • HttpServletRequest request 请求作用于对象
  • PageContext pageContext 当前页作用于对象,这是jsp文件独有的作用域对象。servlet中不存在。在当前页作用于对象中存放的共享数据只能在当前jsp文件中使用,不能共享给servlet和其他jsp文件。

Test.java

package lwd.javaweb;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class Test extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("name", "lowell");
        ServletContext application = request.getServletContext();
        application.setAttribute("age", 18);
        HttpSession session = request.getSession();
        session.setAttribute("gender", "男");

        request.getRequestDispatcher("/test.jsp").forward(request, response);
    }
}

test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String name = (String)request.getAttribute("name");
    Integer age = (Integer) application.getAttribute("age");
    String gender = (String)session.getAttribute("gender");
%>
<%=name%><br>
<%=age%><br>
<%=gender%>
</body>
</html>

JSP运行原理

  1. Http服务器将JSP文件内容编辑成一个Servlet接口实现类(.java),可在work文件夹下找到。work会在windows在c盘AppData(可能是隐藏目录)
  2. Http服务器将Servlet接口实现类编译成class文件
  3. Http服务器负责创建这个class的实例对象,也是servlet实例对象。
  4. Http服务器通过servlet实例对象调用service方法,将jsp文件内容写入响应体中。

C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Tomcat_9_0_37_myjavaweb\work\Catalina\localhost\myjavaweb\org\apache\jsp中找到了.java和.class文件

EL工具

语法

${sessionScope.gender}
${作用域对象别名.共享数据}

EL表达式提供作用域对象别名

JSP EL
application $
session $
request $
pageContext $

EL表达式没有提供遍历功能,因此无法从作用域对象中读取集合内容输出。

事例:

Test.java

package lwd.javaweb;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class Test extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("name", "lowell");
        ServletContext application = request.getServletContext();
        application.setAttribute("age", 18);
        HttpSession session = request.getSession();
        session.setAttribute("gender", "男");

        request.getRequestDispatcher("/test.jsp").forward(request, response);
    }
}

test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
name: ${requestScope.name}
age: ${applicationScope.age}
gender: ${sessionScope.gender}
</body>
</html>

EL表达式简化版

命令格式,EL表达式允许开发人员省略作用域对象别名

${共享数据名}

EL表达式由于没有作用域对象别名,所以执行时按照顺序查找

  1. 先找【pageContext】定位共享数据,存在读取数据输出并结束。
  2. 如果【pageContext】没有,到【request】中定位共享数据,存在读取数据输出并结束。
  3. 如果【request】没有,到【session】中定位共享数据,存在读取数据输出并结束。
  4. 如果【session】没有,到【application】中定位共享数据,存在读取数据输出并结束。
  5. 如果【application】没有,返回null。

EL中的运算符(empty)

常用运算符

  • 算术运算符:+、-、*、/、%(不支持++、–)
  • 关系运算符:==、!=、>、>=、<、<=
  • 逻辑运算符:!、&&、||、not、and、or
  • 条件运算符:?:
  • 取值运算符:[]、点号

empty运算符

用法为${empty 变量},结果为布尔值

EL表达式其他内置对象

param获取请求中的指定参数

其底层实际调用request.getParameter()

${param.请求参数名}

事例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--访问地址--%>
<%--http://localhost:8080/myjavaweb/test.jsp?name=lowell--%>
name: ${param.name}  <!--name: lowell-->
</body>
</html>

paramValues

获取请求中的指定参数的所有值,并写入响应体,其底层实际调用request.getParameterValues()

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--访问地址--%>
<%--http://localhost:8080/myjavaweb/test.jsp?name=lowell&name=xiaoqi--%>
第一个name:${paramValues.name[0]}<br>
第二个name:${paramValues.name[1]}
<%--第一个name:lowell--%>
<%--第二个name:xiaoqi--%>
</body>
</html>
posted @ 2020-09-09 14:00  Lowell  阅读(95)  评论(0)    收藏  举报