开发者

How to auto direct to "my web application" from "root (/)" context in JBoss?

开发者 https://www.devze.com 2023-02-27 01:40 出处:网络
I am using JBoss 6.0 . I have deployed my web application: myApp.ear under the web-context: \"/test\".

I am using JBoss 6.0 .

I have deployed my web application: myApp.ear under the web-context: "/test". So in browser-url if I type "http://localhost:8080/test/", I do get my login page (myLogin.jsp).

Since my WAR exists inside a EAR file, I have specified the context root in the application.xml file using a context-root element inside of the web module - i.e.

<module>
    <web>
        <web-uri>myWeb.war</web-uri>
        <context-root>/test</context-root>
    </web>
</module>

My question is how to auto direct user to my web-app from "root context"?

I mean if user types "http://localhost:8080/", I would expect my web-application's login page to load (instead of JBoss's default ROOT.war's index.html page).

I deleted existing index.html from {JBOSS}\server\default\deploy\ROOT.war and created a login.jsp there. Now I can see that "login.jsp" is getting invoked when I type http://localhost:8080/. But I can not redirect user-request to my web-app's login page.

In that login.jsp, I have tried with: <jsp:forward page="/test" />, but I get error: "HTTP Status 404 - /test".

If I invoke like <jsp:forward page="/test/myLogin.jsp" /> I still get the same 404 error.

Can any one suggest ho开发者_如何学运维w to achieve the auto-direct to my web-app from root-context?


You need to keep index.html in default deploy folder and forward request to your web module.

For example keep following line only in index.html

<META HTTP-EQUIV="Refresh" CONTENT="0; URL=/test/"/> 


The answer from Senthil works nice, but user could see the actual redirect done by the browser (page blinks). The redirect can be done also with rewrite [1, 2] functionality of the JBoss server, which supports HTTP Redirect with 30x codes (no blink).

You can either add the rewrite to your app directly (web.xml, jboss-web.xml) and specify the redirect rules in rewrite.properties - not shown here.

Or you can modify the server configuration on it's own without touching the original application. I find this solution handy because the application is left intact.

Use case: We use this for EJBCA deployment (not our app), it sets it's context root to /ejbca. We want to preserve the default deployment process provided by the packaged ant script while in the same time we would like to add a redirect from / to /ejbca as some kind of default, for user friendliness. If user wants to change it, it's done simply by modifying standalone.xml without need to redeploy the whole app.

Edit standalone.xml:

<subsystem xmlns="urn:jboss:domain:web:2.2" default-virtual-server="default-host" native="false">
    <virtual-server name="default-host" enable-welcome-root="true">
        <alias name="localhost"/>
        <rewrite pattern="^/$" substitution="/test" flags="L,QSA,R" />
    </virtual-server>
</subsystem>


This has worked for me on Wildfly 26.1.1 and JBoss EAP 7.4.

    <subsystem xmlns="urn:jboss:domain:undertow:12.0" default-server="default-server" default-virtual-host="default-host" default-servlet-container="default" default-security-domain="other" statistics-enabled="${wildfly.undertow.statistics-enabled:${wildfly.statistics-enabled:false}}">
        <buffer-cache name="default"/>
        <filters>
            <rewrite name="myfilter" redirect="true" target="/myapp/"/>
        </filters>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
            <https-listener name="https" socket-binding="https" ssl-context="applicationSSC" enable-http2="true"/>
            <host name="default-host" alias="localhost">
                <location name="/" handler="welcome-content"/>
                <http-invoker http-authentication-factory="application-http-authentication"/>
                <filter-ref name="myfilter" predicate="path('/')"/> 
            </host>
        </server>
        <servlet-container name="default">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
        </handlers>
        <application-security-domains>
            <application-security-domain name="other" security-domain="ApplicationDomain"/>
        </application-security-domains>
    </subsystem>

It looks like much but actually is not. My solution shows the default configuration, I just had to add the <filters> section and the <filter-ref>.

Be aware the <filters> section must come above <server> otherwise JBoss will complain at startup. This was not documented at https://access.redhat.com/solutions/2996371 but after all I found https://docs.wildfly.org/19/wildscribe/subsystem/undertow/index.html.

In jboss-cli this means:

/subsystem=undertow/configuration=filter/rewrite=myfilter/:add(redirect=true, target=/myapp)
/subsystem=undertow/server=default-server/host=default-host/filter-ref=myfilter/:add(predicate="path('/')")

The automatically generated standalone.xml does not have exactly the same structure as above but works as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号