Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAppContext pass argument to servlet constructor

I have webAppContext in main class and I have a servlet, that have WebServlet annotation and constructor with args. How I can pass args from Main class to Servlet?

Main.java:

String webappDirLocation = "src/main/java/frontend/webapp/";
WebAppContext webAppContext = new WebAppContext();
webAppContext.setResourceBase("public_html");
webAppContext.setContextPath("/");
webAppContext.setDescriptor(webappDirLocation + "WEB-INF/web.xml");
webAppContext.setConfigurations(new Configuration[]{
                new AnnotationConfiguration(), new WebXmlConfiguration(),
                new WebInfConfiguration(),
                new PlusConfiguration(),
                new MetaInfConfiguration(),
                new FragmentConfiguration(),
                new EnvConfiguration()}
);
webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/classes/.*");
webAppContext.setParentLoaderPriority(true);

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        metadata-complete="false"
        version="3.0">
</web-app>

Servlet:

@WebServlet(name = "WebSocketGameServlet", urlPatterns = {"/gameplay"})
public class WebSocketGameServlet extends WebSocketServlet {
    static final Logger logger = LogManager.getLogger(GameWebSocket.class);
    private final static int IDLE_TIME = 6000 * 1000;
    private AccountService accountService;
    private Game game;
    private WebSocketService webSocketService;

    public WebSocketGameServlet(AccountService accountService, Game game, WebSocketService webSocketService) {
        this.accountService = accountService;
        this.game = game;
        this.webSocketService = webSocketService;
    }

    @Override
    public void configure(WebSocketServletFactory factory) {
        factory.getPolicy().setIdleTimeout(IDLE_TIME);
        factory.setCreator(new GameWebSocketCreator(accountService, game, webSocketService));
    }
}
like image 235
Bulat Avatar asked Oct 20 '22 02:10

Bulat


1 Answers

Well, as far as I understand, you created web application using JavaSE and embedded Jetty. You use annotations to add servlets to your server. And then Jetty creates your servlets it calls default constructors. But you need somehow pass links to objects you created in main() to your servlets.

So, there are several solutions:

  1. Create you own "dependency injection": Add singleton class Context to your application. In main() put your services to the Map<Class<?>, Object> of the Context. And get them in servlets.
  2. Use dependency injection frameworks. Like Spring.
  3. Go to J2EE and use EJB. In this case you need to forget about your own main() and be managed by web server.

P.S. jetty server has method addBean(...). And you can add your "bean" to the server (in code or in config). But as far as I know, it is not possible to get this bean in a servlet.

like image 170
Vitaly Avatar answered Nov 15 '22 04:11

Vitaly