Hi,

Jay SenSharma

Jay SenSharma

We can use virtual-directory-mapping element to specify document roots other than the default document root of the Web application for certain kinds of requests, such as image requests. All images for a set of Web applications can be stored in a single location, and need not be copied to the document root of each Web application that uses them. For an incoming request, if a virtual directory has been specified servlet container will search for the requested resource first in the virtual directory and then in the Web application’s original document root. This defines the precedence if the same document exists in both places.

For more informations please refer to: http://download.oracle.com/docs/cd/E13222_01/wls/docs100/webapp/weblogic_xml.html#wp1039396

Here is a Simple demonstration of using Virtual directory mapping for our WebApplications…The advantage of using this techniqueue…Suppose if we want to keep on changing our LOGO Images or some images of our webapplication…then it becomes very easy Just go to the Physical Location of the Virtual Directory and replace the Images with new Image with the same name…and then refresh the webpage…dont forget to clear the WebBrowsers Cache.

Step1). Create a Directory somewhere in your file system “C:\DELETE\MyImages” and place your Image files here…like i have placed two images here: “HappyNewYear.jpg” and inside “C:\DELETE\MyImages\aaa” directory i have placed “Struts_20_A.jpg” image. (FileNames & Directory names are case Sensitive)

Step2). Develope a Web Application VirtualDirWebApp inside “C:\JavaTest” or any other location you can choose…

Step3). Provide the following JSP page inside your webapplication: “index.jsp”

<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html>
   <body>
    <center><h1> Hello</h1></center>
    <img src="HappyNewYear.jpg"/>
    <HR>
    <img src="aaa/Struts_20_A.JPG"/>
   </body>
</html>

Step4). Provide the following “web.xml” file inside: “C:\JavaTest\VirtualDirWebApp\WEB-INF”

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>

Step5). Provide the virtual directory mapping in side “weblogic.xml” file in “C:JavaTest\VirtualDirWebAppWEB-INF”

<?xml version="1.0" encoding="ISO-8859-1"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90">
  <virtual-directory-mapping>
     <local-path>C:\DELETE\MyImages</local-path>
     <url-pattern>*.jpg</url-pattern>
     <url-pattern>aaa/*</url-pattern>
  </virtual-directory-mapping>
</weblogic-web-app>

Step6). Deploy the Application “VirtualDirWebApp” on the WebLogic Server and then Hit the JSP Page…you will be able to see the JSP Page with 2 images…

Step7). Now just go and replace image with some other file (with the same Name) in the virtual directotry “C:\DELETE\MyImages” and then refresh the JSP page…you should be able to see the New Images there….Make sure that you have Cleared the Browsers Cache…or try to use different browsers to test it..

Source code for this demo on WebLogic 12c is available on :
https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/WebLogic/General/WLS12c_Virtual_Directory_Demo
.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.