在JAX-RS资源中获取ServletContext

lee*_*roy 65 tomcat servlets jax-rs application-variables java-ee

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以抓住ServletContext我的JAX-RS资源吗?

Ade*_*ari 97

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

@javax.ws.rs.core.Context 
ServletContext context;
Run Code Online (Sandbox Code Playgroud)

在您点击服务方法之前,注入不会发生

public class MyService {
    @Context ServletContext context;

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

    @GET
    @Path("/thing") {               
             print("in  wizard service " + context); // available here
Run Code Online (Sandbox Code Playgroud)


Joh*_*hnC 8

正如其他人所说,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); 
    }
}
Run Code Online (Sandbox Code Playgroud)

这将允许您使用可用的servletContext执行其他初始化.

明显的注意 - 您不必使用方法名称setServletContext.你可以使用你想,只要你遵循制定者标准的Java bean的命名模式,任何方法名无效的setXXX(富富),并使用@Context注解.


小智 6

实现ServletContextListener时,servlet上下文也可用.这样可以在启动时轻松加载连接字符串等参数.您可以在web.xml中定义侦听器类,该类在Web应用程序启动时加载ServletContextListener.

在web.xml文件中,添加<listener><context-param>标记.该<listener>规定被称为启动类.该<context-param>标签定义上下文参数就是你的Web应用程序中使用.

首先,在web.xml文件中包含<listener><context-param>标记:

<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>
Run Code Online (Sandbox Code Playgroud)

现在创建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
}
Run Code Online (Sandbox Code Playgroud)

您现在可以选择要分配您已读取的参数的静态变量.这允许您在启动时读取参数一次,并通过您为其分配的静态变量重复使用许多时间.


ZZ *_*der 5

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

@Resource ServletContext servletContext;
Run Code Online (Sandbox Code Playgroud)