Archive for the ‘Web’ category

Ant build script for java web project with tomcat on ubuntu

December 7th, 2013

Here is a sample ant build script for java web project with tomcat on ubuntu. It should be put under the root directory of the web project.



<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectName" default="war" basedir=".">
	<property environment="env" />
	<property name="webapp.name" value="ProjectName" />
	<property name="webapp.dir" value="/data/public/JavaWebapps" />
	<property name="tomcat.lib" value="/usr/share/tomcat7/lib" />
	<property name="ant.lib" value="/usr/share/ant/lib" />
	
	<property name="dist.dir" value="${basedir}/war" />
	<property name="webRoot.dir" value="${basedir}/WebContent" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	
	<!-- Init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		</fileset>
		<fileset dir="${tomcat.lib}">
			<include name="*.jar" />
		</fileset>
		<!-- ant lib  -->
		<fileset dir="${ant.lib}">
			<include name="*.jar" />
		</fileset>
	</path>

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
             property="echo.path.compile"
             refid="project.classpath">
	</pathconvert>
	
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	</target>
	
	<target name="clear" description="Clear the project">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${webapp.name}/${webapp.name}.war" />
		<delete dir="${webapp.name}/${webapp.name}" />
	</target>

	<target name="init" depends="clear" description="Init the directories">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />
	</target>

	<target name="compile" depends="init" description="Compile source files">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			target="1.7" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			>
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		</javac>
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			</fileset>
		</copy>
		<echo message="end compile..." />
	</target>

	<!--  
	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${webapp.name}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 
	            </fileset> 
	        </jar> 
	    </target> 
	-->

	<target name="war" depends="compile" description="Package the project as .war file.">
		<echo message="begin packing war..." />
		<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}" 
			webxml="${webRoot.dir}/WEB-INF/web.xml">
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
			</fileset>
		</war>
		<echo message="end war..." />
	</target>

	<target name="deploy" depends="war" description="Deploy the war to tomcat">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${webapp.name}.war" todir="${webapp.dir}" />
		<echo message="end deploy..." />
	</target>

</project>

Memo of using analog to measure website traffic

July 6th, 2012

A sudden heavy load was detected on our server. On this server the only public service is https which serves a few websites. I check the auth logs and didn’t find anything abnormal. So I wonder it’s some sudden heavy requests were thrown on the apache server. I have never used any web log analyzer before, and googled a little and found analog should do the job for me. Here is just a memo of what I did today.

Install the analog package

On a debian/ubuntu server, one can easily install analog by:
#> (sudo) apt-get install analog
On windows, one can simply download the latest binary package, extract it, and then use it.

Basics of analog

Analog is a program that is used to analyze the apache logs and show the statics that may interest you.

To use analog, you need to provide it a config file, telling it where the logs are, and where to output the results, and what things should be in the results. The default config file in debian (if you install it by “apt-get”) will be /etc/analog.cfg. You can edit the file and then run analog without any parameter. Here is an example:

<VirtualHost 212.87.82.24>
# tell analog where the log file is
LOGFILE /var/log/apache2/access.log
# tell analog where and the format to output the result
OUTFILE /home/me/host.stat.html
# the name of the host. this will be part of the title of the report.
HOSTNAME "ONTILES.COM"
# the link of the host.
HOSTURL "http://www.ontiles.com/"
# analog use some simple images to show the bars or to make the report more friendly. If you installed analog by "apt-get", there are under "/usr/share/analog/images/"; if you installed analog by download the binary package, the images directory is under the extracted root directory.
IMAGEDIR "/usr/share/analog/images/"

How to use a different config file and how to analyse more than one website

see this great article HOW-TO Use Analog for Virtual Hosts (Using Separate Logfiles)

How to show different sections or columns

analog can show the report by hourly, daily, monthly or yearly. Each can be a section in the report. And in each section, you can choose what things to show. Refer to http://www.analog.cx/docs/timereps.html & http://www.analog.cx/docs/othreps.html to see the things you can show.

One can find the detail document here: http://www.analog.cx/docs/Readme.html

.htaccess rewrite to redirect non-www to www

August 25th, 2011

htaccess redirect www to non-www


#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

htaccess redirect non-www to www


#redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

htaccess redirect from one site to another


RewriteEngine on
RewriteCond %{HTTP_HOST} ^OLD_DOMAIN\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.OLD_DOMAIN\.com$
RewriteRule ^(.*)$ "http\:\/\/NEW_DOMAIN\.com\/$1" [R=301,L]

How to install MemCache on Windows 7 X64 for PHP with WAMP

August 22nd, 2011

Basics

The memcache sonsists of 2 parts: the server MemCached and the client MemCache.

  • MemCached is the cache server, the same as Mysqld (mysql server), which save and feed cache to clients.
  • MemCache is a client. With php, we need to install it as a mod, just the same as pdo_mysql (php_pdo_mysql.dll on windows).

Install MemCached

  • Download memcached from http://code.jellycan.com/memcached/ (get the win32 binary version). Here we suppose you extract the memcached in d:\memcached\.
  • If you are on Sindows Vista/7, rightclick on memcached.exe and select Properties; click on the Compatibility tab. At the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
  • Open the command line by Win + R (or Start -> All Programs -> Accessories -> Command Prompt), and then run: d:\memcached\memcached.exe -d install to install the service.
  • Still in the command line, run d:\memcached\memcached.exe -d start, or net start "memcached Server" to start the service.
  • Check whether the service is running. Open Windows Task Manager, go to Services tab, you should find status of “memcached server” is “Running”.

Install MemCache php module

Note

  • To change MemCached memory size (default is 64mb), go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to:
    “C:\memcached\memcached.exe” -d runservice -m 512
  • More details see: memcached -help

SQLSTATE[HY000]: General error: 2053 in ZF

May 15th, 2011

After updating the PDO, my program began to see some weird problems. One of them is “SQLSTATE[HY000]: General error: 2053″. After some time,  I finally found that it is because I passed NULL to the second parameter ($bind) of Zend_Db_Adapter_Abstract::fetchAll($sql, $bind = array(), $fetchMode = null).

So, instead of this:

$popList = $this->getAdapter()->fetchAll($sql, null, Zend_Db::FETCH_OBJ);

one should:

$popList = $this->getAdapter()->fetchAll($sql, array(), Zend_Db::FETCH_OBJ);

when you don’t need to bind any parameter with the SQL.

loop through json object

July 2nd, 2010

p = {k1: v1, k2, v2, k3, v3};
for (var key in p) {
if (p.hasOwnProperty(key)) { // make sure that the key you get is an actual property of an object, and doesn’t come from the prototype:
alert(p[key]);
}
}

compact js

May 28th, 2010

Tool list:

Here are some tools I have found for compact js file in order to save the downloading time.

  • JSMin. It can remove comments and unnecessary whitespaces from js code. The source code (a C file) can be downloaded here. After compilation:

    gcc -o jsmin jsmin.c

    , you can use it like this:

    jsmin <global.js >global.min.js

  • jscompact. It promises to both compact and obfuscate javascript code. However, it depends on ‘Mozilla libjs’ (also called SpiderMonkey) which you can download the src and install it by yourself. NOTE: the newest version of libjs till now is 1.8.0-rc1 which, however is not compatible with the newest jscompact-1.1.1 that (I think) is based on libjs-1.6.0. And if you compile jscompact with js-1.8.0-rc1, you will get errors like this:

    jscompact.c:45: warning: format not a string literal and no format arguments
    jscompact.c: In function ‘shorten_function_vars’:
    jscompact.c:185: error: invalid type argument of ‘->’ (have ‘JSObject’)
    jscompact.c:190: error: ‘js_GetLocalVariable’ undeclared (first use in this function)
    jscompact.c:190: error: (Each undeclared identifier is reported only once
    jscompact.c:190: error: for each function it appears in.)
    jscompact.c:191: error: ‘js_GetArgument’ undeclared (first use in this function)

    OK, here are the right steps to build jscompact. (Remember to replace DIR_LIBJS & DIR_JSCOMPACT with your own dirs.)

    • download libjs-1.6.0 and extract it into DIR_LIBJS.
    • download jscompact and extract it into DIR_JSCOMPACT.
    • compile libjs. Enter DIR_LIBJS/src, compile it with:

      gmake BUILD_OPT=1 -f Makefile.ref

      . And it will build the targets into a new dir Linux_All_OPT.OBJ under src.

    • install the libjs.so. Make a soft link of libjs.so into /usr/lib

      ln -s -LDIR_LIBJS/src/Linux_All_OPT.OBJ/libjs.so /usr/lib/libjs.so

      which will be used by the final executable jscompact. Of course, you can also simply copy the Linux_All_OPT.OBJ/libjs.so into /usr/lib

    • compile jscompact. Enter DIR_JSCOMPACT, and build it:

      gcc -IDIR_LIBJS/src/Linux_All_OPT.OBJ -IDIR_LIBJS/src -g -DXP_UNIX -LDIR_LIBJS/src/Linux_All_OPT.OBJ -ljs -Wall -ojscompact jscompact.c

    Now you can use it like this:

    /jscompact –infile global.js –outfile global.min.js

  • Javascript Minimizer. It’s a .exe file and promises to remove insignificant to javascript characters (comments, tabs, newline, etc…) from *.js files. Haven’t tried yet.

Comparation:

JsMin will removes all the unnecessary white spaces but won’t shorten the function arguments and the local scope variables. JsCompact will remove most of the white spaces and shorten arguments and variables as well. However it leaves a lot of spaces which I think it’s not necessary. Given a 65,280B file, JsCompact reduces the size to 40,532B, while JsMin reduces it to 39,981B.

So I think a good choice is to combine the both. First compact with JsCompact, and remove the spaces with JsMin. With this method, the file 65,280B file can be finally reduced to 37,271B.

Sloving the ajax error c00ce56e

May 28th, 2010

Well, this is an embarrassing error which only occurs in IE but not in Firefox or Chorme. Haven’t tested it in Safari or Opera yet. This error  -”could not complete the operation due to error c00ce56e” – occurs after sending the request and geting the results successfully. It’s due to the encoding problem. I got the solutions here – http://learningconstantly.blogspot.com/2007/11/error-in-ajax-script-could-not-complete.html:

  • add the following header on each of the .php files like so:
  • header(“Content-type: text/html; charset=ISO-8859-1″);

  • In your php.ini file, change:
  • default_mimetype = “text/html”

    to

    default_mimetype = “text/html; Charset=utf-8″

    Pure CSS Hide and Display Content – Pure CSS tabs

    February 18th, 2010

    http://www.devinrolsen.com/css-hide-and-display-content/

    And with JS:

    http://www.devinrolsen.com/javascript-css-hide-display-content/