Tuesday, October 19, 2010

Virtual Directory mapping in WebApps

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.

Here is a Simple demonstration of using Virtual directory mapping for our Web Applications…
The advantage of using this technique is Suppose if we want to keep on changing our LOGO Images or some images of our web application 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, don’t forget to clear the Web Browsers Cache.
Step1)Create a Directory somewhere in your file system “C:\images\MyImages” and place your Image files here…like i have placed two images here: “test.jpg” and inside “C:\images\MyImages\test2” directory i have placed “test2.jpg” image. (Filenames & Directory names are case Sensitive)

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

Step3). Provide the following JSP page inside your web application: “index.jsp
———–
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<html>
<body>
<center><h1> virtual directory test</h1></center>
<img src="test.jpg"/>
<HR>
<img src="test2/test2.JPG"/>
</body>
</html>
Step4)Provide the following “web.xml” file inside: “C:\JavaTest\VirtualDirWebApp\WEB-INF”



<web-app>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>

————————-

Step5). Provide the virtual directory mapping in side weblogic.xml file in“C:\JavaTest\VirtualDirWebApp\WEB-INF”
<weblogic-web-app>
<virtual-directory-mapping>
<local-path> C:\images\MyImages </local-path>
<url-pattern>*.jpg</url-pattern>
<url-pattern>test2/*</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 directory “C:\images\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..
.

No comments:

Post a Comment