Recent Notes
Displaying keyword search results 1 - 10
Created by Fang on March 30, 2012 10:07:25
Last update: March 08, 2013 13:41:57
After a user resets a password, I want to force the user to change the password before she gets access to secured content. This is usually done with a servlet filter. But with Spring MVC, you can also use a HandlerInterceptor . According to Spring JavaDoc: HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing. Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context. As a basic guideline, fine-grained handler-related preprocessing tasks are candidates...
Created by Fang on November 21, 2011 15:57:49
Last update: November 22, 2011 09:51:26
The improved custom taglib works with existing facelet ui taglibs. For example:
<ui:param name="theName" value="John"/> <my:hel... produces the expected output. However, a problem exists with the ui:repeat tag: <h3>With ui:repeat</h3> <ui:repeat var="theName... When tested with a URL like: http://localhost:8080/facelet-demo/?name=Zack&name... the raw EL prints out the correct names, but my custom tag substitutes empty string for theName2 ! In theory, the response is rendered in the Render Response phase, way after the Apply Request Values phase, actual values should be available to EL. The answer to this anomaly turned out to be very deep ! Yes, right there in the code! I would consider this a bug in facelets implementation, but the JSF spec did not tell what the expected behavior should be. In my custom...
Created by Fang on September 07, 2009 20:44:15
Last update: November 03, 2011 14:43:19
Step 1: Repackage a web app as EAR A Java EE application is a multimodule Maven project. At the very least you'll need to package a WAR and an EAR. To get started, I'll simply re-package the simple webapp as an EAR. Create a directory named javaee-app Copy the webapp from here to javaee-app . Rename struts1app to webapp . Create pom.xml under javaee-app :
<project> <modelVersion>4.0.0</modelVersion>... Create a directory named ear under javaee-app . Create pom.xml under ear : <project> <modelVersion>4.0.0</modelVersion>... Modify pom.xml in the webapp directory so that it looks like this: <project> <modelVersion>4.0.0</modelVersion> ... Build with " mvn package " in the javaee-app directory. You can see that ear-1.0.ear is successfully generated in javaee-app/ear/target . Maven successfully resolves dependencies between the sub-projects....
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/DumpXml.java
package com.android.xmltool;
import java.ut...
Screenshot
Pre-built APK can be downloaded from: http://code.google.com/p/android-binxml-dump/
Created by freyo on May 23, 2011 14:30:18
Last update: May 23, 2011 14:31:08
There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware.
Code without namespace:
import java.io.*;
import javax.xml.parsers.*;
...
code with namespace:
import java.io.*;
import java.util.Iterator;
...
XML without namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
XML with namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
The same XPath expression works for both XML files when the parser is not namespace aware.
When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: " /test-license/licensee/name/text() " works for the XML file without namespace, while " /p:test-license/p:licensee/p:name/text() " works for the XML file with namespace.
Created by freyo on May 17, 2011 11:13:17
Last update: May 17, 2011 11:13:17
This is an odd-ball content provider in that it doesn't provide database records, but provides a resource as a stream. It can be used to provide media files or XML resources. Start the project with:
tools/android create project --package com.android... Create assets directory and add an XML file ( assets/demo.xml ): <? xml version="1.0" encoding="UTF-8"?> <people... Edit the layout ( res/layout/main.xml ): <?xml version="1.0" encoding="utf-8"?> <LinearL... Edit src/com/android/cptest/Dummy.java : package com.android.cptest; import java.io.... Add content provider ( src/com/android/cptest/XmlResource.java ): package com.android.cptest; import java.io.... Update AndroidManifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifes... Add this section to the end of build.xml : <target name="-package-resources"> <ech... Build and install: ant install Screenshot: Remove the Dummy activity ( AndroidManifest.xml ): <?xml version="1.0" encoding="utf-8"?> <manifes... Create a new project for...
Created by Dr. Xi on April 26, 2011 21:25:55
Last update: April 27, 2011 11:08:57
The following code validates a web.xml file against the web-app 2.5 schema:
// Java XML validation with schema
import java....
web.xml used for testing:
<?xml version="1.0" encoding="UTF-8"?>
<web-ap...
According to Java API doc , validation can also be done while parsing by calling setSchema on the parsing factory. However, validation doesn't work for the SAXParserFactory!
// Java XML validation with schema
import java....
Created by freyo on April 18, 2011 15:08:21
Last update: April 18, 2011 15:12:20
Generate android project
$ ~/android-sdk-linux_86/tools/android create proj...
Create XML file res/xml/books.xml :
<?xml version="1.0"?>
<catalog>
<book id=...
Edit layout ( res/layout/main.xml ):
<?xml version="1.0" encoding="utf-8"?>
<Lin...
Edit code ( src/com/android/xmlres/XMLResource.java ):
package com.android.xmlres;
import java.io....
Change activity label from app_name to booklist ( AndroidManifest.xml ):
<?xml version="1.0" encoding="utf-8"?>
<manifes...
Add value for string resource ( res/values/string.xml ):
<?xml version="1.0" encoding="utf-8"?>
<resourc...
Deploy and test:
ant install
Created by Dr. Xi on April 05, 2011 08:04:37
Last update: April 05, 2011 08:11:37
There's no difference between a Java HTTP client and a Java HTTPS client. Ignore JavaWorld Java Tip 96 , it's way too old. The following code gets an HTTP page as well as an HTTPS page.
import java.io.*;
import java.net.*;
pub...
There's one catch . If you are using the code on a test server with a self-signed certificate, it fails. In that case, I would suggest that you download the certificate from the server and import it to your keystore as a trusted key. You may also need to add a subject alternative name to the certificate if the host name does not match the certificate.
You may also choose to use a custom TrustManager and HostnameVerifier to ignore the certificate verification errors.
Created by Dr. Xi on March 28, 2011 20:51:30
Last update: March 28, 2011 20:53:46
HTTP basic authentication is just Base64 encoded user name and password passed in as the Authorization header. So the following code works:
// encode user name and password
String credent...
However, since JDK 1.2, there's a more Java friendly way:
Authenticator.setDefault(new Authenticator() {
...
Test code:
import java.net.*;
import java.io.*;
pub...