register
other register

Monday, November 12, 2007

Enable logging remote host name is tomcat access log.

To enable remote host name to be logged in tomcat access log, in conf/server.xml, you have to do two things:

1.

<Service name="Catalina">
...
<Connector port="8080" ... enableLookups="true"..>
...
<Service>


2.

<Host name="localhost" ..>
...
<Valve className="org.apache.catalina.valves.FastCommonAccessLogValve"
directory="logs" prefix="access_log." suffix=".txt"
pattern="combined" resolveHosts="true" rotatable="true" fileDateFormat="yyyy-MM-dd"/>
...
</Host>


In the access log, it will look like:


host217-43-97-182.range217-43.btcentralplus.com - - [12/Nov/2007:16:39:48 +0000] "GET /styles/topmenu.css HTTP/1.1" 200 1184 "http://www.jetonrails.com" "
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)"

The first part is the remote host name instead of remote IP address.

Friday, November 09, 2007

Escape xml in JSTL

For <c:out> in JSTL:

<c:out value="<p>Hello</p>"/>

It will generate source code like:

& lt;p& gt;hello& lt;/p& gt;

And it will show the wording in the browser like:

<p>Hello</p>


Solution: put excapteXml="false" in.

<c:out escapexml="false" value="<p>Hello</p>"/>

It will generate source code like:

<p>Hello</p>

And it will show the wording in the browser like:

Hello

Monday, November 05, 2007

Select Distinct Values in XML

For the xml file like below:

<books>
<book author="Kevin Williams" title="Professional XML 2.0" />
<book author="Lewis Carroll" title="Alice in Wonderland" />
<book author="Lewis Carroll" title="Through the Looking-Glass" />
</books>

If you want to select distinct author from it, then make sure you use: xslt 2.0, and SaxonBB parser (in OxygenXML for example)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />

<xsl:template match="units">
<xsl:for-each select="distinct-values(unit_instance/lev_code)">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Thursday, November 01, 2007

Link spans two lines.

If a link spans two lines and looks terrible, then use the following code in the CSS.

line-height: 14pt;

Thursday, September 13, 2007

Website Skip Navigation

Refer to the link. Creating a "Skip Navigation" in the web site allows visual disabled people to use the screen reader to jump over the navigation links and directly to the content.

Friday, August 17, 2007

Retrieve a text file from a web address

Requirement:

Retrieve a text file from a web address and write it to a local file.



// Retrieve the web address from the property file, in which you can change the web address without changing the Java code

Properties properties = new Properties();
try {
properties.load(new FileInputStream("file.properties"));
} catch (IOException e){
System.out.println("Can't fild file.properties");
}
String webAddress= properties.getProperty("webAddress");

// Construct the url
URL url = new URL(webAddress);

// Establish the connection
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

// Retrieve the response status code
int statusCode = httpURLConnection.getResponseCode();

if (statusCode == 200){
// Create an empty file
File file = new File ("folderName" + "/" + "fileName" + ".extensionName");
if (file.exists()) {
file.delete();
}
// Create a buffered writer for output
BufferedWriter output = new BufferedWriter(new FileWriter(file, true));


// Create a buffered reader to retrieve content from the web address
BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)httpURLConnection.getContent()));

String lLine = "";

// Read web content line by line and write it to the output file
while ((line = reader.readLine()) != null) {
output.append(line);
}

// Close the buffered writer
output.close();
}

Thursday, August 16, 2007

Vi: Search and Replace

Search (Wraped around at end of file):
  Search STRING forward :   / STRING.
Search STRING backward: ? STRING.

Repeat search: n
Repeat search in opposite direction: N (SHIFT-n)

Replace: Same as with sed, Replace OLD with NEW:

First occurrence on current line: :s/OLD/NEW

Globally (all) on current line: :s/OLD/NEW/g

Between two lines #,#: :#,#s/OLD/NEW/g

Every occurrence in file: :%s/OLD/NEW/g

Friday, August 10, 2007

svn merge example

Problem Solving:

Merge code from one branch to the trunk.

The release candidate server has the code from trunk.

In release_candidate server's source code directory:

svn merge --dry-run -r rev1:rev2 branch_url

# rev1 is the revision number when first branch out from trunk
# rev2 is the latest revision number from the branch

svn merge -r rev1:rev2 branch_url

svn status | grep "C "

# see any conflicts after merge, "C" stands for conflict

# for those conflict files, edit them to fix the conflict.

# filename.working is the local copy (here is the trunk code)
# filename.right is the latest from the branch
# filename.left is the original from the brach when first branched out from trunk

# happy

svn resolved filename

svn commit -m "commit rev1:rev2 branch_url"

# commit the merged code to the trunk

# done

For more svn knowledge, please see SVN RedBook

Sorting Map

The following definition is from Java API.

HashMap:

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

LinkedHashMap:

Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

TreeMap:

Red-Black tree based implementation of the SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

Problem Solving:
One university department has three levels of courses, namely undergraduate, postgraduate taught, and postgraduate research to offer. The department wants to show these courses in alphabatical order within respective level of course. The order of the level should be: undergraduate, postgraduate taught, postgraduate research.


LinkedHashMap courseMap = new LinkedHashMap();
TreeMap ugCourseTreeMap = new TreeMap();
courseMap.put("UG", ugCourseTreeMap);

TreeMap pgtCourseTreeMap = new TreeMap();
courseMap.put("PGT", pgtCourseTreeMap);

TreeMap pgrCourseTreeMap = new TreeMap();
courseMap.put("PGR", pgrCourseTreeMap);

// looping begins
// courseName is a String
// course is a class

ugCourseTreeMap.put(courseName, course);
pgtCourseTreeMap.put(courseName, course);
pgrCourseTreeMap.put(courseName, course);

// looping end

Iterator it = ugCourseTreeMap.values().iterator();
while(it.hasNext()) {

}


/**************No need to do the following************
/**************Because TreeMap is ordered*************
ArrayList keysUG = new ArrayList();
keysUG.addAll(ugCourseMap.keySet());
Collections.sort(keysUG);

ArrayList keysPGT = new ArrayList();
keysPGT.addAll(pgtCourseMap.keySet());
Collections.sort(keysPGT);

ArrayList keysPGR = new ArrayList();
keysPGR.addAll(pgrCourseMap.keySet());
Collections.sort(keysPGR);

// Iteration example
Iterator it = ugCourseMap.keySet().iterator();
while (it.hasNext()) {
..
}

/**********************************************

Search a keyword in a directory recursively

If you want to search a keyword in files of the current directory and its sub directories:

$/usr/local/home/jet>

then do:

find . | xargs grep whatever

Wednesday, August 08, 2007

Monday, August 06, 2007

The use of Link in HTML

Can't add Alt in background image in CSS

When using background: url(/imagesource); in CSS, please be aware that there no way to add Alt text. Therefore, using normal html text instead.

Friday, August 03, 2007

Replacing string in multiple files without changing filenames.

#!/bin/sh

for file in /path/to/my/files/*
do
sed 's/stringA/stringB/g' $file > $file.new
mv $file.new $file
done
exit 0

Make the script executable
$ chmod u+x foo.sh

Then run it (assuming it's in the current directory)
$ ./foo.sh

For more sed examples, please have a look at the IBM articles

Thursday, August 02, 2007

WTP Tutorials - JavaServer Faces Tools Tutorial

Any Eclipse fans who want to learn JSF should have a look at this tutorial.

Make sure you say "Hello World!" ;)