首页 > 其他分享 >Request-共享数据(域对象)、获取ServletContext

Request-共享数据(域对象)、获取ServletContext

时间:2022-11-26 19:35:06浏览次数:37  
标签:request Request ServletContext import 共享 servlet javax

Request-共享数据(域对象)

 

 

 

共享数据:

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

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

  方法:

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

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

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

     

Request-获取ServletContext

  获取ServletContext:

    ServletContext getServletContext()

package com.example.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 servletContext = request.getServletContext();

        System.out.println(servletContext);

    }

    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/yuzong/p/16928110.html

相关文章