Load JavaScript and CSS from Server Nearby"

From Documentation
Line 17: Line 17:
 
<library-property>
 
<library-property>
 
     <name>org.zkoss.web.servlet.http.URLEncoder</name>
 
     <name>org.zkoss.web.servlet.http.URLEncoder</name>
     <value>org.zkoss.test.TestEncoder</value> <!-- Where the Class is -->
+
     <value>org.zkoss.test.TestEncoder</value> <!-- Where the Implementation Class is -->
 
</library-property>
 
</library-property>
 
</source>
 
</source>

Revision as of 04:28, 20 October 2010


DocumentationZK Developer's ReferencePerformance TipsLoad JavaScript and CSS from Server Nearby
Load JavaScript and CSS from Server Nearby


If some of the client machines are far away from the application server, we could set up a server nearby the clients to host ZK's JavaScript and CSS files, and then configure the application server to generate the URLs of JavaScript and CSS (and iamges it refers) from the the sever nearby clients.

URLEncoder.png

*Notice : the ZK static resource server is a simple server which deploy official ZK library, not whole application of yours.

How to

  1. Implement the Encodes.URLEncoder
  2. Add library-property configuration to the zk.xml
    Document : URLEncoder.
  3. Host ZK static resouce server

Following is a sample :

Configuration

<library-property>
    <name>org.zkoss.web.servlet.http.URLEncoder</name>
    <value>org.zkoss.test.TestEncoder</value> <!-- Where the Implementation Class is -->
</library-property>

Implementation

package org.zkoss.test;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.zkoss.web.servlet.http.Encodes.URLEncoder;
public class TestEncoder implements URLEncoder {

	@Override
	public String encodeURL(ServletContext ctx, ServletRequest request, ServletResponse response, String uri, URLEncoder defaultEncoder) throws Exception {
		if (isStaticResource(uri)) {
			return getResourceHost() + uri.replace("~./", "");
		} else {
			return defaultEncoder.encodeURL(ctx, request, response, uri, defaultEncoder);
		}
	}
	/**
	 * file .wcs : CSS File
	 * file .wpd : Javscript File
	 */
	private boolean isStaticResource(String url) {
		return url.startsWith("~./") && (url.endsWith(".wpd") || url.endsWith(".wcs"));
	}

	/**
	 * Detect where the ip is/ who is login / what kind of resouce server will
	 * 
	 * @return the host name include protocol prefix. (Client will retrieve resource from it)
	 */
	private String getResourceHost() {
		return "http://SomeWhereNearbyMe/DefaultContext/zkau/web/";
	}

}

Hosting

Simply deploy Official ZK Library on some where (near your customer) and add the URL to your implementation.

Don't know how to deploy on server ? see our Installation Guide :

Version History

Last Update : 2010/10/20

Version Date Content
     



Last Update : 2010/10/20

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.