首页 > 其他分享 >Request_共享数据(域对象) 与Request_获取ServletContext

Request_共享数据(域对象) 与Request_获取ServletContext

时间:2023-01-31 10:13:34浏览次数:41  
标签:Request request ServletContext import 共享 servlet javax

Request_共享数据(域对象)

 

 

    共享数据:

       域对象:一个有作用范围的对象,可以在范围内共享数据

       request:代表一次的请求的范围,一般用于请求转发的多个资源中共共享数据 

       方法:

          1.void setAttribute(String name,Object obj):存储数据

          2.Object  getAttitude(String  name):通过键获取值 

          3.void  removeAttribute(String name):通过键移除键值对

    

  //存储数据到request域中
        request.setAttribute("msg","ikun");

 

 //获取数据
        Object ikun = request.getAttribute("msg");
        System.out.println(ikun);

 

Request_获取ServletContext

    ServletContext

        ServletContext getServletContext( )

RequestDemo10
package com.example.day14_servlethttprequest.request;

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;

@WebServlet("/RequestDemo10")
public class RequestDemo10 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext ikun = request.getServletContext();

        System.out.println(ikun);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }
}

启动结果

 

 

 

   

标签:Request,request,ServletContext,import,共享,servlet,javax
From: https://www.cnblogs.com/x3449/p/17077985.html

相关文章