JavaWeb-Servlet保存作用域


保存作用域

page-页面级别(已过时)

request-一次请求响应范围

准备两个demo

package com.fancy.demo;

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 java.io.IOException;

/**
 *  @Name demo02
 *  @Description 演示request保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo02")
public class Demo02Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取request保存作用域保存的数据,key为uname
        Object unameObj = req.getAttribute("uname");
        System.out.println("unameObj =" + unameObj);
    }
}
package com.fancy.demo;

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 java.io.IOException;

/**
 *  @Name demo01
 *  @Description request保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.向request保存作用域保存数据
        req.setAttribute("uname","keke");
        //2.客户端重定向
        resp.sendRedirect("demo02");
    }
}

使用客户端重定向,客户端获取不到value的值keke

JavaWeb-Servlet保存作用域

JavaWeb-Servlet保存作用域

接下来使用服务器内部转发

更改demo02代码

JavaWeb-Servlet保存作用域

package com.fancy.demo;

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 java.io.IOException;

/**
 *  @Name demo01
 *  @Description request保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo01")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.向request保存作用域保存数据
        req.setAttribute("uname","keke");
/*        //2.客户端重定向
        resp.sendRedirect("demo02");*/
        //服务器内部转发
        req.getRequestDispatcher("demo02").forward(req,resp);
    }
}

JavaWeb-Servlet保存作用域

获取成功

session-一次会话范围

准备两个demo

package com.fancy.demo;

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 java.io.IOException;

/**
 *  @Name demo03
 *  @Description session保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo03")
public class Demo03Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.想request保存作用域保存数据
        req.getSession().setAttribute("uname","keke");
/*        //2.客户端重定向
        resp.sendRedirect("demo02");*/
        //服务器内部转发
        req.getRequestDispatcher("demo04").forward(req,resp);
    }
}
package com.fancy.demo;

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 java.io.IOException;

@WebServlet("/demo04")
public class Demo04Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session保存作用域保存的数据,key为uname
        Object unameObj = req.getSession().getAttribute("uname");
        System.out.println("unameObj =" + unameObj);
    }
}

从session中也可以成功获取value值

JavaWeb-Servlet保存作用域

application-一次应用程序范围有效

JavaWeb-Servlet保存作用域

package com.fancy.demo;

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;
import java.io.IOException;

/**
 *  @Name demo05
 *  @Description application保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo05")
public class Demo05Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.想request保存作用域保存数据
        //Servlet上下文
        ServletContext application = req.getServletContext();
        application.setAttribute("uname","keke");
        //2.客户端重定向
        resp.sendRedirect("demo06");
        /*//服务器内部转发
        req.getRequestDispatcher("demo06").forward(req,resp);*/
    }
}
package com.fancy.demo;

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;
import java.io.IOException;

/**
 *  @Name demo06
 *  @Description application保存作用域
 *  @Author Fancy
 *  @return 2022/8/3
 *  @Version 1.0
 */
@WebServlet("/demo06")
public class Demo06Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取application保存作用域保存的数据,key为uname
        ServletContext application = req.getServletContext();
        Object unameObj = application.getAttribute("uname");
        System.out.println("unameObj =" + unameObj);
    }
}

可获取到value值

JavaWeb-Servlet保存作用域

并且使用其它浏览器访问application作用域也可以得到value值(Tomcat未停止)

JavaWeb-Servlet保存作用域

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/278826.html

(0)
上一篇 2022年8月4日
下一篇 2022年8月4日

相关推荐

发表回复

登录后才能评论