Context path for web application on Glassfish 3.1.

2019-02-13 13:11发布

问题:

I'm trying to find a way to explicitly specify the context path of a web application being deployed to Glassfish 3.1.2.2 but I've had no luck so far. Can anyone provide guidance on this? The background to this is below:

I have a web application that consists of two separate Netbeans (7.0) projects. The first is a web service and is called FooWS. The second is a user facing web application which uses the FooWS webservice. It's called FooApp.

I've recently upgraded glassfish to 3.1.2.2 in the hope of resolving some other issue and now when I deploy the FooWS app, it deploys successfully but with the context path /web rather than /FooWS. This is not something I would particularly care about except that when I try to deploy FooApp, glassfish also tries to deploy that to /web leading to the following error:

SEVERE: Exception while loading the app : java.lang.Exception: WEB0113: Virtual server [server] already has a web module [FooWS] loaded at [/web]; therefore web module [FooApp] cannot be loaded at this context path on this virtual server.

The web.xml for FooApp looks as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>defaultWebRootId</param-name>
        <param-value>2631</param-value>
    </context-param>

    <listener>
        <listener-class>com.foo.service.AppInitialiser</listener-class>
    </listener>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

The configuration for FooWS is similar. Neither contains any mention of the application context so my expectation is that it should use /FooWS rather than default to /web.

The obvious solution would seem to be to override the context path in the web.xml but I can't find any way of doing this. Any suggestions?

Just some follow up, I accidentally changed the context path to /FooW. This time it deployed as expected to /FooW. Changing it back if /FooWS causes the old behaviour to return, that is, it deploys again to /web. It seems like I have a workaround for the moment.

For the benefit of anyone following this, I got the same behaviour with 3.1.2. I have now returned to 3.1 (b43) and it behaves as expected.

回答1:

Add a glassfish-web.xml file in the same folder as web.xml

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
    <context-root>/FooWS</context-root>
</glassfish-web-app>