Load JavaScript and CSS from Server Nearby"

From Documentation
m (remove empty version history (via JWB))
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 
__TOC__
 
__TOC__
<blockquote>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.
+
 
 +
<blockquote>If some of the client machines are far away from the application server, we could set up a server near 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 images it refers) from the server near the clients.
 
</blockquote>
 
</blockquote>
 
[[File:URLEncoder.png|866px]]
 
[[File:URLEncoder.png|866px]]
  
<span style="font-weight: bold;color:red;" >*Notice :</span> the ZK static resource server is a  simple server which <b>deploy official ZK library</b>, not whole application of yours.
+
<span style="font-weight: bold;color:red;" >*Notice :</span> the ZK static resource server is an application server which you <b>deploy only official ZK library to</b>, not your whole application.
==How to==
+
 
#Implement the <javadoc  type="interface">org.zkoss.web.servlet.http.Encodes.URLEncoder</javadoc>
+
 
#Add '''library-property''' configuration to the <span style="color:green;font-style:italic;">zk.xml</span>
+
= How-to =
 +
# Implement the <javadoc  type="interface">org.zkoss.web.servlet.http.Encodes.URLEncoder</javadoc>
 +
# Add '''library-property''' configuration to the <span style="color:green;font-style:italic;">zk.xml</span>
 
#: Document : [[ZK Configuration Reference/zk.xml/The Library Properties/org.zkoss.web.servlet.http.URLEncoder]].
 
#: Document : [[ZK Configuration Reference/zk.xml/The Library Properties/org.zkoss.web.servlet.http.URLEncoder]].
#Host ZK static resouce server
+
# Host ZK static resource server
 +
 
 
Following is a sample :
 
Following is a sample :
  
==== Configuration ====
+
== Configuration ==
 
<source lang="xml">
 
<source lang="xml">
 
<library-property>
 
<library-property>
Line 21: Line 25:
 
</source>
 
</source>
  
====Implementation====
+
==Implementation ==
 +
 
 
<source lang="java" >
 
<source lang="java" >
 
package org.zkoss.test;
 
package org.zkoss.test;
Line 44: Line 49:
 
*/
 
*/
 
private boolean isStaticResource(String url) {
 
private boolean isStaticResource(String url) {
return url.startsWith("~./") && (url.endsWith(".wpd") || url.endsWith(".wcs"));
+
// zul.lang.wpd should not be a static resource
 +
return url.startsWith("~./") && !url.endsWith("zul.lang.wpd") && (url.endsWith(".wpd") || url.endsWith(".wcs"));
 
}
 
}
  
 
/**
 
/**
* Detect where the ip is/ who is login / what kind of resouce server will
+
* Detect where the ip is / who login / what kind of resource server
 
*  
 
*  
 
* @return the host name include protocol prefix. (Client will retrieve resource from it)
 
* @return the host name include protocol prefix. (Client will retrieve resource from it)
Line 59: Line 65:
 
</source>
 
</source>
  
====Hosting ZK Static Resource====
+
== Hosting ZK Static Resource ==
Simply deploy '''ZK Library''' to a server (near your customer) and add the URL to your implementation of URLEncoder.
+
 
 +
# Create a WAR with all ZK JARs only (without your application code)
 +
# configure <code>web.xml</code> as a normal ZK application
 +
# deploy such special '''ZK WAR''' to a server near your customer and add the URL to your implementation of URLEncoder.
  
 
<i>Don't know how to deploy on server ?  Please refer to [[ZK Installation Guide|Installation Guide]]</i>.
 
<i>Don't know how to deploy on server ?  Please refer to [[ZK Installation Guide|Installation Guide]]</i>.
  
=Version History=
+
 
{{LastUpdated}}
 
{| border='1px' | width="100%"
 
! Version !! Date !! Content
 
|-
 
| &nbsp;
 
| &nbsp;
 
| &nbsp;
 
|}
 
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 10:23, 5 February 2024


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 near 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 images it refers) from the server near the clients.

URLEncoder.png

*Notice : the ZK static resource server is an application server which you deploy only official ZK library to, not your whole application.


How-to

  1. Implement the Encodes.URLEncoder
  2. Add library-property configuration to the zk.xml
    Document : ZK Configuration Reference/zk.xml/The Library Properties/org.zkoss.web.servlet.http.URLEncoder.
  3. Host ZK static resource 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) {
		// zul.lang.wpd should not be a static resource
		return url.startsWith("~./") && !url.endsWith("zul.lang.wpd") && (url.endsWith(".wpd") || url.endsWith(".wcs"));
	}

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

}

Hosting ZK Static Resource

  1. Create a WAR with all ZK JARs only (without your application code)
  2. configure web.xml as a normal ZK application
  3. deploy such special ZK WAR to a server near your customer and add the URL to your implementation of URLEncoder.

Don't know how to deploy on server ? Please refer to Installation Guide.




Last Update : 2024/02/05

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