JSP对象解析

目录

jsp九大内置对象

jsp四大域对象

jsp中的out输出和response.getWriter输出的区别

out.print()和out.write() 

jsp常用标签

静态包含

动态包含

动态包含的特点:

请求转发

jsp练习

练习1

打印九九乘法表

练习2 

 存储学生信息并打印

请求转发使用说明

 Listener监听器

ServletContextListenter监听器

ServletContextListener监听器监听ServletContext对象的步骤


jsp九大内置对象

jsp九大内置对象,是指Tomcat在翻译jsp页面成为Servlet源代码后,内部提供的九大对象,叫内置对象。

request                  请求对象

response                响应对象

pageContext          jsp的上下文对象

session                 会话对象

application             ServletContext对象

config                     ServletConfig对象

exception                异常对象

out                          jsp输出流对象

jsp四大域对象

 域对象是可以向Map一样存取数据的对象。四个域对象功能一样,他们对数据的存取范围不同

四个域对象分别是:

域对象 所属类 访问范围
pageContext (PageContextImpl类) 当前jsp页面范围内有效
request (HttpServletRequest类) 一次请求内有效
session (HttpSession类) 一个会话范围内有效(打开浏览器访问服务器,直到关闭浏览器)
application (ServletContext类) 整个web工程范围内都有效(只要web工程不停止,数据都在)

//往四个域都分别保存了数据

<%

        pageContext.setAttribute("key","pageContext");

        request.setAttribute("key","request");

        session.setAttribute("key","session");

        application.setAttribute("key","application");

%>

pageContext域是否有值:<%=pageContext.getAttribute("key")%> <br>

request域是否有值:<%=pageContext.getAttribute("key")%> <br>

session域是否有值:<%=session.getAttribute("key")%> <br>

application域是否有值:<%=application.getAttribute("key")%> <br>

 创建另一个jsp页面:

 其他范围测试:

 他们的范围是从小到大的,使用时一般先使用小范围,小范围不够用再使用范围。(内存优化的原因)

小:pageContext
request
session
大:application

jsp中的out输出和response.getWriter输出的区别

 我们可以发现,无论谁在前输出的结果,都是response的在前

图示分析:

 当jsp页面中的所有代码执行完之后会做的操作:

1、执行out.flush()操作,会把out缓冲区的数据追加写入到response缓冲区末端。

2、会执行response的刷新操作,会把数据写给客户端。

验证: 

 由于jsp翻译之后,底层源代码都是使用out来进行输出,所以一般情况下,我们在jsp页面统一使用out进行输出。避免打乱页面输出的顺序。

out.print()和out.write() 

out.write()输出字符串字符串没问题

out.print()可以输出任意数据(都会转化成字符串后调用write输出)

结论:在jsp页面中,可以统一使用呢out.print()来进行输出

jsp常用标签

静态包含

web下创建一个include目录,里面分别写main.jsp和footer.jsp

footer.jsp下

<html>
<head>

    <meta charset="utf-8"/>
    <title>Insert title here</title>
</head>
<body>
页脚信息

</body>
</html>

 main.jsp下

</head>
<body>
首页<br>
主体<br>
<%--
    include file="" 就是静态包含
    file属性指定你要包含的页面路径
    地址中的第一个斜杆 /   表示http://ip:port/工程路径/ 映射到idea为web
--%>
    <%@include file="/include/footer.jsp" %>
</body>
</html>

include file=" " 就是静态包含

file属性指定你要包含的页面路径

地址中的第一个斜杆 / 表示http://ip:port/工程路径/ ,映射到idea中为web目录

  修改footer.jsp内容

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

<html>
<head>

    <meta charset="utf-8"/>
    <title>Insert title here</title>
</head>
<body>
页脚信息
修改后,主页显示
</body>
</html>

动态包含

格式:

<jsp:include page="/include/footer.jsp"></jsp:include>

动态包含也可以和静态包含一样

动态包含的特点:

1、动态包含会把包含的jsp页面也翻译成java代码

2、动态包含底层代码使用如下代码去调用被包含的jsp页面执行输出。

JspRuntimeLibrary.include(request,response,"/include/footer.jsp",out,false);

请求转发

格式:

<jsp:forward page=" "></jsp:forward>
<!--page属性设置请求转发的路径-->

jsp练习

练习1

打印九九乘法表

<html>
<head>
</head>
<body >

<h1 >九九乘法表</h1>

   <%
    for(int i=1;i<10;i++){
        for(int j=1;j<=i;j++){
    %>

        <%=j+"*"+i+"="+(i*j)%>

      <%
       }
        %>

    <br/>

   <%
    }
   %>

</body>
</html>

练习2 

 存储学生信息并打印

pojo包下的student类

package pojo;

public class Student {
private String name;
private int  id;
private int age;

    public Student(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

text1.jsp下

<%@ page import="java.util.List" %>
<%@ page import="pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<html>
<head>
   
    <meta charset="utf-8"/>
    <title>Insert title here</title>
<%--    设置样式<style></style>--%>
    <style>
        table{
            border: 1px black solid;
            width: 300px;
        }
        td,tr{
            border: 1px black solid;
            width: 300px;
        }
    </style>
</head>
<body>
<%
    List<Student> list=new ArrayList<>();
    for (int i=1;i<=10;i++){
      list.add(new Student("name"+i,i,10+i));
    }
%>
<table>
<%for (Student student:list){%>
<%--    tr是一行,td为一列--%>
<tr>
    <td><%=student.getName()%></td>
    <td><%=student.getId()%></td>
    <td><%=student.getAge()%></td>
</tr>

  <% } %>
</table>
</body>
</html>

请求转发使用说明

流程图:

 SearchStudentServlet类下

package com.Servlet;

import pojo.Student;

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

public class SearchStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        //发sql语句查询学生信息
        //使用for循环生成查询到的数据做模拟
        List<Student> list=new ArrayList<>();
        for (int i=1;i<=10;i++){
            list.add(new Student("name"+i,i,10+i));
        }
        //保存查询到的数据到Request域中
        req.setAttribute("stuList", list);
        //请求转发到之外的showStudent.jsp中
        req.getRequestDispatcher("/showStudent.jsp").forward(req, resp);

    }
}

web.xml下:

   <servlet>
       <servlet-name>SearchStudentServlet</servlet-name>
       <servlet-class>com.Servlet.SearchStudentServlet</servlet-class>
   </servlet>
    <servlet-mapping>
        <servlet-name>SearchStudentServlet</servlet-name>
        <url-pattern>/searchStudentServlet</url-pattern>
    </servlet-mapping>

showStudent.jsp下

<%@ page import="java.util.List" %>
<%@ page import="pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<html>
<head>

    <meta charset="utf-8"/>
    <title>Insert title here</title>
<%--    设置样式<style></style>--%>
    <style>
        table{
            border: 1px black solid;
            width: 300px;
        }
        td,tr{
            border: 1px black solid;
            width: 300px;
        }
    </style>
</head>
<body>
<%
    List<Student> list= (List<Student>) request.getAttribute("stuList");

%>
<table>
<%for (Student student:list){%>
<%--    tr是一行,td为一列--%>
<tr>
    <td><%=student.getName()%></td>
    <td><%=student.getId()%></td>
    <td><%=student.getAge()%></td>
</tr>

  <% } %>
</table>
</body>
</html>

 运行结果:

 Listener监听器

 1、Listener监听器他是JavaWeb的三大组件之一。javaweb的三大组件分别是servlet程序、filter过滤器、Listenter监听器。

2、Listenter他是javaEE的规范,规范就是接口

3、监听器的作用是,监听某种事务的变化,然后通过回调函数,反馈给客户或程序去做一些相应的处理。

ServletContextListenter监听器

ServletContextListener他可以监听ServletContext对象的创建和销毁。

ServletContext对象在web工程启动的时候,在web工程停止的时候销毁。

ServletContextListener监听器监听ServletContext对象的步骤

1、编写一个类去实现ServletContextListener

2、实现器两个回调方法

3、到web.xml中去配置监听器

创建类和实线两个方法

package com.Listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListenerImpl implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("Servlet对象被创建了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("Servlet对象被销毁了");

    }
}

web.xml中配置

<listener>
        <listener-class>com.Listener.MyServletContextListenerImpl</listener-class>
    </listener>

猜你喜欢

转载自blog.csdn.net/weixin_60719453/article/details/122892589