龙空技术网

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

Java新手笔记 61

前言:

当前大家对“如何获取session对象”大约比较珍视,同学们都需要剖析一些“如何获取session对象”的相关知识。那么小编同时在网上汇集了一些对于“如何获取session对象””的相关资讯,希望小伙伴们能喜欢,我们一起来学习一下吧!

前面已经把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对象的方式。

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

标签: #如何获取session对象