Recent Notes

Displaying keyword search results 1 - 10
Created by Fang on December 06, 2011 19:03:25    Last update: December 07, 2011 08:54:11
Our custom tag, as implemented in the previous note , is broken when a template is used. Create a template file ( home-template.xhtml ): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Stric... and a test page that uses it ( home.xhtml ): <?xml version="1.0" encoding="UTF-8"?> <ui:comp... Then request the page with URL: http://localhost:8080/facelet-demo/home.jsf?name=Jack . You'll find that our hello tag works inside ui:repeat but fails to get the value defined by ui:param ! What's the problem? Our hello tag implementation evaluated the EL with the wrong EL context! This is the corrected implementation: package com.example; import
Created by Dr. Xi on November 11, 2011 10:05:22    Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute. import java.util.regex.*; import*; ... Test file: <div id="HTML snippet"> <img src="img/big/txt-m...
Created by Fang on October 22, 2011 19:51:05    Last update: October 22, 2011 20:31:48
I built a very basic JSF application and deployed to Tomcat 7.0.22, but it failed with this error: Caused by: java.lang.ClassFormatError: Absent Code... That looks weird and I wasn't able to find a sensible explanation! So I copied the jsf-api-2.1.jar , which was downloaded from the Maven repository by Maven, into a temp folder. And tested it with this simple program: public class ClassFormatErrorTest { public ... I also copied servlet-api.jar from Tomcat's lib folder to the temp folder. Sure enough it failed with the same error: C:\tmp>java -cp .;jsf-api-2.1.jar;servlet-api.jar ... But when I replaced the javax.faces.webapp.FacesServlet class with one I compiled from source, the error disappears! Conclusions: The jar file jsf-api-2.1.jar from Maven repository is good for compilation only (cannot be used...
Created by freyo on May 13, 2011 15:45:29    Last update: September 20, 2011 08:08:12
This is an Android app that dumps any binarized xml file as plain text - to the sdcard on the device or emulator. build.xml : <?xml version="1.0" encoding="UTF-8"?> <project... AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <man... res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <Lin... res/values/strings.xml : <?xml version="1.0" encoding="utf-8"?> <res... src/com/android/xmltool/ package; import java.ut... Screenshot Pre-built APK can be downloaded from:
Created by freyo on June 30, 2011 12:36:26    Last update: July 01, 2011 09:43:54
Code public void install(View view) { String url = ... Screenshots: Logcat: D/InstallApp( 338): install: file:///sdcard/GetAp... Using http://... as the URI for the APK does not work. Android fails to find a handler for the intent (logcat): No Activity found to handle Intent { act=android.i... Therefore, download the APK to sdcard then fire the Intent.
Created by Dr. Xi on January 14, 2010 00:28:27    Last update: March 30, 2011 15:37:44
A task that a Java developer does so frequently is to find out where a certain class can be found - to resolve compilation errors, classpath issues, or version conflicts of the same class introduced by multiple class loaders. A long while back I wrote a simple Perl script to perform the task. Later I was informed that there are Swing based Jar Browser and Jars Browser . Then, there are a couple of shell one-liners: # one liner 1 find -name "*.jar" -print0 | xarg... But all of them share the same problem: if a class is in a jar nested in another jar, it cannot be found. Such is the case for a class inside a jar under the WEB-INF/lib directory of a...
Created by Dr. Xi on March 30, 2011 13:43:05    Last update: March 30, 2011 13:45:27
Method 1 - use javap with verbose flag: $ javap HelloWorld -verbose | head Compiled fro... Method 2 - use a utility class: import*; import java.nio.ByteBuffer; ... According to the VM Spec , a Java class file has this structure: ClassFile { u4 magic; ...
Created by Dr. Xi on November 23, 2010 20:20:01    Last update: March 01, 2011 13:38:51
I tried to find a GZIP compression servlet filter to compress a large log file that we send down to the browser. Most of the implementations I found were overly complicated and many buggy. This is a simple implementation that worked for me. The filter: package filter.demo; import*; i... Config web.xml : <filter> <filter-name>gzipFilter</filte... The ugly anonymous inner class could have been avoided if the servlet API did not insist on ServletResponse.getOutputStream returning the bogus ServletOutputStream class (instead of the plain OutputStream ). Additional Note: In an earlier version of this filter, the gzip headers were added in doFilter , like this: // This is NOT good! if (supportsGzip) { ... It turned out that the ServletResponse methods sendError bypasses the gzip...
Created by Dr. Xi on February 01, 2011 14:38:55    Last update: February 01, 2011 14:40:59
Create the stuff you want to manufacture with the factory: package tc.demo; public class Junk { ... Create the factory: package tc.demo; import java.util.Enumerati... Tell Tomcat to use your factory. Create file context.xml and put it under the directory META-INF of your web application: <Context> <Resource name="/find/junk/here" ... Note that beside name , type and factory , you can put any arbitrary attribute in the Resource element. Access the thing with JNDI: <%@page language="java" import="javax.naming.*,tc.... The server side log looked like this: INFO: jndiName: here INFO: name: scope, value: ... Also note that, in contrast to Tomcat documentation , resource-ref is not needed in web.xml .
Created by Dr. Xi on July 19, 2010 21:58:34    Last update: July 23, 2010 21:37:23
Parsing XML in Java is really simple: import*; import javax.xml.parsers.Docu... The parser implementation details are hidden behind the JAXP API. In case you want to know which parser implementation is used, this is what the JavaDoc for DocumentBuilderFactory.newInstance says: Use the javax.xml.parsers.DocumentBuilderFactory system property. Use the properties file " lib/ " in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to...
Previous  1 2 Next