Weblogic: Changing context root and redirection

Sometime you might have to change the context root(CR) of the application deployed on weblogic from CR1 to CR2. This can be easily done by updating the context root in the application.xml file in your ear file.

But then if you might also want to redirect all the old URL references to this new context root. So if somebody access http://yourhost/CR1 it should redirect to http://yourhost/CR2. It’s easy to do in Apache server using mod_rewrite module. But weblogic as such don’t provide this functionality.

 

So here is quick handy way to achieve this:

1.    Create a dummy web module under CR1.

2.    Update application.xml as follows:

   

<module > 

       <web>

         <web-uri>newAppDir</web-uri>

         <context-root>/CR2</context-root>

      </web>

   </module>

    <module> 

       <web>

         <web-uri>oldAppDir</web-uri>

         <context-root>/CR1</context-root>

      </web>

   </module>

     

3.    Configure web.xml for this dummy web application such that all the requests will be handled by a single JSP, redirector.jsp

a.    Set the welcome file as redirector.jsp

b.    Add Following servlet mapping to catch all the URLs and let it handle by redirector.jsp

 

<servlet-name>wlRedirector</servlet-name>

<jsp-file>/redirector.jsp</jsp-file>

</servlet>

<servlet-mapping>

     <servlet-name>wlRedirector</servlet-name>

     <url-pattern>/*</url-pattern>

 </servlet-mapping>

 

4.    In the redirector.jsp add code to change the CR1 to CR2 and sendRedirect to CR2

                                String recvUrl = request.getRequestURI();

          recvUrl = "/CR2" + recvUrl;

         if (request.getQueryString() != null) {

          recvUrl += '?' + request.getQueryString();

         }

URL reconstructedURL = new URL(request.getScheme(),

                               request.getServerName(),

                               request.getServerPort(),

                               recvUrl);

response.sendRedirect (reconstructedURL.toString());

 

 

Rahul Ner

 

No comments: