ServerEndpoint和web.xml [英] ServerEndpoint and web.xml

查看:110
本文介绍了ServerEndpoint和web.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Soap,REST servlet和现在一个WebSocket:

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

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

我有下一个麻烦:如果web.xml存在,WebSocket不可见。在web.xml中描述了jdbc资源,servlets ant其他...
当我删除web.xml时 - websocket成功可见。我该如何解决这个问题?

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?

更新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>


推荐答案

我认为你应该配置你的主servlet(MainService)提供所有网址 / * / MainService / * 并使用websocket作为 / MainService / game

I think you should configure your main servlet (MainService) to serve all urls /* or /MainService/* and than use websocket as /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.WSServletContextListener


ConfigServlet
com.example.ConfigServlet
1


MainService
com.sun.xml.ws.transport.http.servlet.WSServlet
1

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>

并制作你的js(或其他websocket客户端)连接到/ MainService / game而不是/ game

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

这篇关于ServerEndpoint和web.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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