计算机系统应用教程网站

网站首页 > 技术文章 正文

《Servlet》第22节:获取ServletContext上下文对象的四种方式

btikc 2024-10-10 04:54:08 技术文章 8 ℃ 0 评论

前面已经把ServletContext上下文对象中的常用方法介绍完了,这一小节内容,介绍一下获取ServletContext上下文对象的四种方式。

1.1、通过request获取ServletContext

HttpServletRequest求对象中,提供了getServletContext()方法,可以用于获取ServletContext上下文对象。案例代码:

package com.gitcode.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2024/2/10 16:19
 * @Author ZhuYouBin
 * @Description:
 */
public class ContextServlet01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过request请求对象,获取上下文对象
        ServletContext context = request.getServletContext();
    }
}

1.2、通过GenericServlet获取ServletContext

GenericServlet抽象类中提供了一个getServletContext()方法,可以用于获取ServletContext上下文对象。案例代码:

package com.gitcode.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2024/2/10 16:19
 * @Author ZhuYouBin
 * @Description:
 */
public class ContextServlet02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通过GenericServlet中的getServletContext,获取上下文对象
        ServletContext context = this.getServletContext();
    }
}

1.3、通过HttpSession获取ServletContext

HttpServletRequest请求对象中,提供了getSession()方法,可以获取到一个HttpSession对象,而HttpSession对象中提供了一个getServletContext()方法,可以获取到ServletContext上下文对象。案例代码:

package com.gitcode.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2024/2/10 16:19
 * @Author ZhuYouBin
 * @Description:
 */
public class ContextServlet03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取Session对象
        HttpSession session = request.getSession();
        // 通过Session,获取上下文对象
        ServletContext context = session.getServletContext();
    }
}

1.4、通过ServletConfig获取ServletContext

GenericServlet抽象类实现了ServletConfig接口,ServletConfig接口中提供了一个getServletContext()方法,可以用于获取ServletContext上下文对象。案例代码:

package com.gitcode.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2024/2/10 16:19
 * @Author ZhuYouBin
 * @Description:
 */
public class ContextServlet04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取ServletConfig对象
        ServletConfig servletConfig = this.getServletConfig();
        // 通过ServletConfig对象,获取ServletContext对象
        ServletContext context = servletConfig.getServletContext();
    }
}

以上,就是四种获取ServletContext对象的方式。

今天就到这里,未完待续~~

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表