获取 JAX-RS 资源中的 ServletContext

发布于 2024-08-13 01:15:34 字数 258 浏览 5 评论 0原文

我正在玩 JAX-RS,部署在 Tomcat 上。基本上是:

@Path("/hello")
@Produces({"text/plain"})
public class Hellohandler {

    @GET
    public String hello() {
        return "Hello World";
    }

}

有什么方法可以获取 JAX-RS 资源中的 ServletContext 吗?

I'm playing around with JAX-RS, deploying on Tomcat. It's basically:

@Path("/hello")
@Produces({"text/plain"})
public class Hellohandler {

    @GET
    public String hello() {
        return "Hello World";
    }

}

Is there any way I can get hold of the ServletContext within my JAX-RS resource?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木有鱼丸 2024-08-20 01:15:34

此外,@Resource 注释可能不起作用。试试这个,

@javax.ws.rs.core.Context 
ServletContext context;

直到你点击服务方法时,注入才会发生

public class MyService {
    @Context ServletContext context;

    public MyService() {
         print("Constructor " + context);  // null here     
    }

    @GET
    @Path("/thing") {               
             print("in  wizard service " + context); // available here

Furthermore, @Resource annotation might not work. Try this

@javax.ws.rs.core.Context 
ServletContext context;

The injection doesn't happen until you hit the service method

public class MyService {
    @Context ServletContext context;

    public MyService() {
         print("Constructor " + context);  // null here     
    }

    @GET
    @Path("/thing") {               
             print("in  wizard service " + context); // available here
风吹过旳痕迹 2024-08-20 01:15:34

正如其他人所指出的,servletContext 可以在字段级别注入。它也可以在方法级别注入:

public static class MyService {
    private ServletContext context;
    private int minFoo;

    public MyService() {
        System.out.println("Constructor " + context); // null here
    }

    @Context
    public void setServletContext(ServletContext context) {
        System.out.println("servlet context set here");
        this.context = context;

        minFoo = Integer.parseInt(servletContext.getInitParameter("minFoo")).intValue();

    }

    @GET
    @Path("/thing")
    public void foo() {
        System.out.println("in wizard service " + context); // available here
        System.out.println("minFoo " + minFoo); 
    }
}

这将允许您使用可用的 servletContext 执行额外的初始化。

明显的注意事项 - 您不必使用方法名称 setServletContext。您可以使用任何您想要的方法名称,只要您遵循 setter 的标准 java bean 命名模式 void setXXX(Foo foo) 并使用 @Context 注释。

As others have noted, the servletContext can be injected at the field level. It can also be injected at the method level:

public static class MyService {
    private ServletContext context;
    private int minFoo;

    public MyService() {
        System.out.println("Constructor " + context); // null here
    }

    @Context
    public void setServletContext(ServletContext context) {
        System.out.println("servlet context set here");
        this.context = context;

        minFoo = Integer.parseInt(servletContext.getInitParameter("minFoo")).intValue();

    }

    @GET
    @Path("/thing")
    public void foo() {
        System.out.println("in wizard service " + context); // available here
        System.out.println("minFoo " + minFoo); 
    }
}

This will allow you to perform additional initialization with the servletContext available.

Obvious note - you don't have to use the method name setServletContext. You can use any method name you want so long as you follow the standard java bean naming pattern for setters, void setXXX(Foo foo) and use the @Context annotation.

爱你是孤单的心事 2024-08-20 01:15:34

当您实现 ServletContextListener 时,servlet 上下文也可用。这使得在启动时加载连接字符串等参数变得容易。您可以在 web.xml 中定义侦听器类,以便在 Web 应用程序启动时加载 ServletContextListener。

在 web.xml 文件中,添加 标记。 指定启动时调用的类。 标记定义 Web 应用程序中可用的上下文参数。

首先,在 web.xml 文件中包含 标记:

<web-app>
  <!-- ... -->
  <listener>
    <listener-class>com.your.package.ServletContextClass</listener-class>
  </listener>

  <!-- Init parameters for db connection -->
  <context-param>
    <param-name>your_param</param-name>
    <param-value>your_param_value</param-value>
  </context-param>
  <!-- ... -->
</web-app>

现在创建 servlet 上下文类,如下所示。

public class ServletContextClass implements ServletContextListener
{
  public void contextInitialized(ServletContextEvent arg0) 
   {
    //use the ServletContextEvent argument to access the 
    //parameter from the context-param
    String my_param = arg0.getServletContext().getInitParameter("your_param");
   }//end contextInitialized method

  @Override
  public void contextDestroyed(ServletContextEvent arg0) 
  { }//end constextDestroyed method
}

您现在可以选择哪个静态变量来分配您已读取的参数。这允许您在启动时读取一次参数,并通过分配给它的静态变量多次重复使用。

The servlet context is also available when you implement the ServletContextListener. This makes it easy to load parameters such as connection string at start-up. You can define the listener class in web.xml that loads you ServletContextListener at startup of your web application.

Inside the web.xml file, add the <listener>and <context-param> tags. The <listener> specifies the class that is called at startup. The <context-param> tag defines context parameter that is available within your web application.

First, include the <listener>and <context-param> tags in the web.xml file:

<web-app>
  <!-- ... -->
  <listener>
    <listener-class>com.your.package.ServletContextClass</listener-class>
  </listener>

  <!-- Init parameters for db connection -->
  <context-param>
    <param-name>your_param</param-name>
    <param-value>your_param_value</param-value>
  </context-param>
  <!-- ... -->
</web-app>

Now create the servlet context class as follows.

public class ServletContextClass implements ServletContextListener
{
  public void contextInitialized(ServletContextEvent arg0) 
   {
    //use the ServletContextEvent argument to access the 
    //parameter from the context-param
    String my_param = arg0.getServletContext().getInitParameter("your_param");
   }//end contextInitialized method

  @Override
  public void contextDestroyed(ServletContextEvent arg0) 
  { }//end constextDestroyed method
}

You can now choose which static variable to assign the parameter you have read. This allows you to read the parameter once at start-up, and reuse many time through the static variable that you assign it to.

梦太阳 2024-08-20 01:15:34

只需像这样使用资源注入,

@Resource ServletContext servletContext;

Just use resource injection like this,

@Resource ServletContext servletContext;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文