【问题标题】:Change application context from inside a WAR inside EAR从 EAR 内的 WAR 内更改应用程序上下文
【发布时间】:2015-01-22 14:40:41
【问题描述】:

是否有机会缩短访问我的 servlet 的 URL? 当前部署的 URL 是 server:port/WebApp/myservlet。 我想把它缩短为server:port/myservlet

servlet 位于 EAR 存档中的 WAR 存档中。

WebApp.war/WEB-INF 中的 web.xml 内容

<display-name>WebApp</display-name>
<servlet>
    <description></description>
    <display-name>MyServlet</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.company.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/myservlet</url-pattern>
</servlet-mapping>

此处为 Servlet 代码:

@WebServlet("/*")
public class MyServlet extends HttpServlet implements Servlet {

    public MyServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // some magic here
    }
}

P.S.:我需要它同时在 JBoss 和 Websphere 上工作。

编辑: 抱歉,我忘了提及我无法使用 application.xml 更改上下文。

【问题讨论】:

    标签: jakarta-ee jboss websphere


    【解决方案1】:

    您需要将应用程序上下文根从 /WebApp 更改为 /,而不是 servlet 映射。最好的方法是在application.xml 文件中定义它,如下所示:

    <application>
        <display-name>MyApplication</display-name>
        <module>    
            <web>
                <web-uri>WebApp.war</web-uri>
                <context-root>/</context-root>
            </web>
        </module>
    </application>
    

    更新
    如果您没有 application.xml,那么您需要专有描述符。
    JBoss - jboss-web.xml 参见 here

    <jboss-web>
        <context-root>/</context-root>
    </jboss-web>
    

    WebSphere - ibm-web-ext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-ext
        xmlns="http://websphere.ibm.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_1.xsd"
        version="1.1">
        <context-root uri="/"/>
    </web-ext>
    

    【讨论】:

    • 谢谢,我已经在 J​​Boss 网站上找到了这个解决方案,但是。我正在编写一小段代码,它应该是非常大的应用程序的一部分。并且没有使用 application.xml,我不能那样做。对不起,我忘了提。否则你是绝对正确的。 +1
    • 我想弄清楚如何让它在 JBoss 上工作 - 一起配置 web.xml、jboss-web.xml 和 servlet 注释。之后我会专注于 WAS。
    • @Kousalik 查看更新的答案。您需要专有描述符。但我建议使用application.xml,因为这是一种可移植的方式,并且将该文件的创建添加到您正在使用的任何构建过程中应该不是一个大问题。
    • 嘿,加油!感谢您的更新,您的想法是 corerct。但它不能以这种方式工作。但它反过来工作!将上下文根设置为 /WebApp 并将我的 servlet 映射为“/”。如果您相应地更新它,我会接受您的回答。干杯
    • 顺便说一句,我真的不能采用 application.xml 的方式,因为这是一个大型企业产品,针对特定场景进行定制。除非不允许,否则我可能弊大于利。
    猜你喜欢
    • 2017-04-26
    • 2016-03-01
    • 1970-01-01
    • 2011-04-17
    • 2012-11-25
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多