ServerEndpoint and web.xml
Asked Answered
O

4

7

I have some Soap, REST servlets and now one WebSocket:

@ServerEndpoint("/game")
public class WebSocketgame{
...
}

I have next trouble: WebSocket dont visible, if web.xml is exists. In web.xml describes jdbc resources,servlets ant other... When i'm delete web.xml - websocket successfully visible. How can I fix this problem?

Update web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>ConfigServlet</servlet-name>
        <servlet-class>com.example.ConfigServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>MainService</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ConfigServlet</servlet-name>
        <url-pattern>/ConfigServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>JsonServlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.example.json</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JsonServlet</servlet-name>
        <url-pattern>/json/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>propfile</param-name>
        <param-value>/WEB-INF/server_config.txt</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>MainService</servlet-name>
        <url-pattern>/MainService</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <resource-ref>
        <description>postgreSQL Datasource example</description>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>
Ogg answered 13/10, 2014 at 11:44 Comment(3)
Please share your web.xml. I suspect that you are declaring servlet version <3.0, which could disable servlet scanning, resulting in not triggering appropriate ServletContainerInitializer.Heroworship
OK. The web.xml looks fine - I mean it could work. Can you intercept WebSocket handshake request and share it? What HTTP status code is in the response? 404?Heroworship
Did you try this https://mcmap.net/q/1629934/-mapping-websocketendpoints-in-a-web-xml-file?Deepfry
M
0

Try adding

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="annotated">

in beans.xml file

Mundford answered 13/10, 2014 at 11:49 Comment(0)
E
0

I believe you need to add servlet configuration for those with something like this

<servlet>
<servlet-name>gameServer</servlet-name>
<servlet-class>WebSocketgame.SocketEndPoint</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gameServer</servlet-name>
<url-pattern>/game</url-pattern>
</servlet-mapping>
Eley answered 24/4, 2018 at 22:14 Comment(0)
C
0

I think you should configure your main servlet (MainService) to serve all urls /* or /MainService/* and than use websocket as /MainService/game.

Your class

@ServerEndpoint("/MainService/game")
public class WebSocketgame{
...
}

And web.xml http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> com.sun.xml.ws.transport.http.servlet.WSServletContextListener ConfigServlet com.example.ConfigServlet 1 MainService com.sun.xml.ws.transport.http.servlet.WSServlet 1

<servlet-mapping>
    <servlet-name>ConfigServlet</servlet-name>
    <url-pattern>/ConfigServlet</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>JsonServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example.json</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JsonServlet</servlet-name>
    <url-pattern>/json/*</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>propfile</param-name>
    <param-value>/WEB-INF/server_config.txt</param-value>
</context-param>
<servlet-mapping>
    <servlet-name>MainService</servlet-name>
    <url-pattern>/MainService/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<resource-ref>
    <description>postgreSQL Datasource example</description>
    <res-ref-name>jdbc/postgres</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

And make your js (or other websocket client) to connect to /MainService/game instead of /game

Capriccioso answered 25/4, 2018 at 4:23 Comment(4)
Working config example?Fe
Thanks for example config, but cant see references to "WebSocketgame" class in that config. Note: this question as about: "ServerEndpoint and web.xml" and we are looking for a way to register ServerEndpoint in web.xml. The default implementation use resource auto-scanning. The servlet container scan all classes and looking for those marked with "ServerEndpoint" annotation and start them as a special services. But this scanning not works in particular conditions, like described in the question OR using older servlet container(< 3) that not support resource scanning.Fe
Have you been able to find solution @msangel?Adalai
Yes and no. Haven't find cross-container solution but I used API of servlet-container and register evrything I need inside of servlet even listener. Something like this: habr.com/ru/post/128380Fe
D
0

I had the same problem. My solution was extend the endpoint class to HttpServlet.

@ServerEndpoint("/game")
public class WebSocketgame extends HttpServlet {
...
}

And for web.xml definition

<servlet>
        <display-name>Webgame</display-name>        
        <servlet-name>Webgame</servlet-name>        
        <servlet-class>com.example.WebSocketgame</servlet-class>        
        <load-on-startup>1</load-on-startup>
    </servlet>
Debrahdebrecen answered 21/10, 2019 at 1:3 Comment(1)
This does nothing at all, the class is never even constructed.Tergal

© 2022 - 2024 — McMap. All rights reserved.