我有一些Soap、REST servlet,现在还有一个WebSocket:
@ServerEndpoint("/game")
public class WebSocketgame{
...
}我有下一个麻烦:如果web.xml存在,WebSocket是不可见的。在web.xml中描述了jdbc资源,servlets和其他...当我删除web.xml时- websocket成功可见。我如何解决这个问题?
更新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>发布于 2014-10-13 19:49:05
尝试添加
<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">在beans.xml文件中
发布于 2018-04-25 06:14:36
我认为您需要为那些具有以下内容的人添加servlet配置
<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>发布于 2018-04-25 12:23:28
我认为您应该将主servlet (MainService)配置为提供所有urls /*或/MainService/*,然后使用websocket作为/MainService/game。
你的班级
@ServerEndpoint("/MainService/game")
public class WebSocketgame{
...
}和web.xml ">http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> com.sun.xml.ws.transport.http.servlet.WSServlet 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>
并使您的js (或其他websocket客户端)连接到/MainService/game而不是/game
https://stackoverflow.com/questions/26339217
复制相似问题