0


浅析Servlet中的四大作用域

Servlet的四大作用域:Page,Request,Session,Application。主要是用来存放共享数据!

一、 Page作用域

对应的作用域访问对象为pageContext对象。

作用范围:当前整个jsp页面。

生命周期:当对JSP的请求时开始,当响应结束时销毁。

package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        out.print("页面1");
        
        // page域(当前页面使用)
        String name1="张无忌";
        out.print(name1);

        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

输出结果为:

当我们在页面2输出name1时,发现直接报错输出不了的,证明只在当前页面起作用。

二、Request作用域

对应的作用域访问对象为request对象。

作用范围:request表示一个请求,只要发出一个请求就会创建一个request,它作用于整个请求链(转发并不会终止此请求链)。

生命周期:当浏览器发送请求到后台时创建,当请求结束,request被销毁。

package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        out.print("页面1");
        
        //request域(在同一个请求中使用,需要转发)
        String name2="周芷若";
        request.setAttribute("name2", name2);
        out.print(request.getAttribute("name2"));
        
        //转发
//        request.getRequestDispatcher("Demo2Servlet").forward(request, response);

        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

当不转发时,拿到的页面1和页面2的结果如下:

当做了转发后,地址栏并未改变,但是页面2就拿到了值:

三、Session作用域

对应的作用域访问对象为session对象。

作用范围:在当前浏览器进行数据共享,一次会话。

生命周期:在浏览器启动时会创建,当浏览器关闭时被销毁,存在无操作默认销毁时间。

package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        out.print("页面1");
        
        //session域(在同一个会话中)
        String name3="赵敏";
        session.setAttribute("name3", name3);
        out.print(session.getAttribute("name3"));

        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Demo2Servlet")
public class Demo2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        out.print("页面2");
        
        //session域(在同一个会话中)
        out.print(session.getAttribute("name3"));

        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

输出结果为:

四、Application作用域

对应的作用域访问对象为application对象。

作用范围:整个web项目。

生命周期:在启动Web项目时创建,代表整个web项目,是其中最大的作用域,当服务关闭或时项目停止时被销毁。

package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Demo1Servlet")
public class Demo1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        ServletContext sc = getServletContext();
        out.print("页面1");
        
        //application域(整个应用中,服务器重启)
        String name4="殷离";
        sc.setAttribute("name4", name4);
        out.print(sc.getAttribute("name4"));
        
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package com.ape.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Demo2Servlet")
public class Demo2Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取资源
        PrintWriter out = response.getWriter();
        ServletContext sc = getServletContext();
        out.print("页面2");
        
        //application域(整个应用中,服务器重启)
        out.print(sc.getAttribute("name4"));
        
        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

输出结果为:

** 四个作用域的区别:**

Application > Session > Request > Page

** 小结:**

标签: servlet java 前端

本文转载自: https://blog.csdn.net/m0_67942533/article/details/125585135
版权归原作者 崇令 所有, 如有侵权,请联系我们删除。

“浅析Servlet中的四大作用域”的评论:

还没有评论