在JAX-RS中相当于Servlet的init()方法 [英] Servlet init() method equivalent in JAX-RS

查看:198
本文介绍了在JAX-RS中相当于Servlet的init()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个在Glassfish上运行的应用程序。我应该通过使用jax-rs和球衣来将servlet转换为适当的安静的东西。

我一直在尝试为init()方法找到解决方法,但直到现在我失败了。



这是使用servlets的原始部分:

  import javax .servlet。* 

public void init(ServletConfig config)throws ServletException {
super.init(config);
if(!isRunning()== true)){
/ *在这里做一些事情* /
}

logger.info(Deamon已经开始 );
}

和这个我试图使用jax-rs

  import javax.ws.rs。* 
import javax.servlet。*

public void init( @Context ServletConfig config)throws ServletException {
//uper.init(config);
if(!isRunning()== true)){
/ *在这里做一些事情* /
}

logger.info(Deamon已经开始 );
}

我已经检查了邮件列表并搜索了一下,但是找不到可行的方法对于这种情况。

任何想法如何使用servlet实现与init方法相同的行为?

解决方案

最后,在Google搜索一下后,我找到了一个合适的解决方案。 $ b

基本上,我已经扩展了
public class ContextListener实现了ServletContextListener 类,并实现了抽象方法 public void contextInitialized(ServletContextEvent sce )在应用程序加载时调用。我已经将servlet的逻辑移动到了这里来完成初始化和其他配置设置,然后它很流畅。


I am working on an application which is running on Glassfish. I am supposed to convert the servlets to proper restful stuff, by using jax-rs and jersey.

I have been trying to find a workaround for init() method, but till now i failed.

Here is the original part, using servlets:

import javax.servlet.*

public void init(ServletConfig config) throws ServletException {
super.init(config);
 if (!isRunning() == true)) {
     /* Do some stuff here*/
 }

 logger.info("Deamon has started");
}

and this one which i am trying to use jax-rs

import javax.ws.rs.*
import javax.servlet.*

public void init(@Context ServletConfig config) throws ServletException {
//uper.init(config);
if (!isRunning() == true)) {
  /* Do some stuff here*/
}

logger.info("Deamon has started");
}

I have checked mailing lists and googled around but couldnt find a way which could work for this case.

any ideas how to achieve the same behaviour with servlets for init method?

解决方案

finally, after googling a little bit more, i found a proper solution.

basically, i have extended public class ContextListener implements ServletContextListener class and implemented the abstract method public void contextInitialized(ServletContextEvent sce) which is called when the application is loaded. I have moved the logic from the servlet to here for doing the initialization and other config settings, then it was smooth.

这篇关于在JAX-RS中相当于Servlet的init()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆