Grizzly + Jersey从@Context ServletContext获取Init或Context参数

时间:2012-09-04 13:10:51

标签: java servlets jersey grizzly

我是Grizzly + Jersey的新手,目前效果很好。但我发现我无法从@Context ServletContext获取Init或Context参数。以下是我的代码。

public static void main( String[] args ) throws IOException
    {

        GrizzlyWebServer ws = new GrizzlyWebServer(9998);
        ServletAdapter jAdapter = new ServletAdapter();
        jAdapter.setContextPath("/api");
        jAdapter.setServletInstance(new ServletContainer());                
        jAdapter.addInitParameter("com.sun.jersey.config.property.packages", "test.ui.controller");
        jAdapter.addInitParameter("test1","test1");   
        jAdapter.addContextParameter("test2","test2");   
        ws.addGrizzlyAdapter(jAdapter, null);

        ws.start();
        System.out.println("Running... please press enter to stop the server.");

        System.in.read();
        ws.stop();

    }

我尝试从@Context获取参数,如下所示。

@Path("/home")
public class LoginController {
    @GET 
    @Produces("text/html")
    public String getIt(@Context HttpServletRequest req,@Context ServletContext context) {                      
        System.out.println(context.getAttribute("test1"));
            System.out.println(context.getAttribute("test2")); 
            System.out.println(context.getInitParameter("test1"));
            System.out.println(context.getInitParameter("test2"));

        return "hello";
    }       
}

但我所有的尝试都失败了。输出为null,null,null,null。

0 个答案:

没有答案