Recent Notes

Displaying keyword search results 1 - 7
Created by Dr. Xi on June 21, 2011 15:41:51    Last update: June 22, 2011 11:33:36
Demo code for CSV parsing with Apache Commons CSV parser . Java code: import java.io.*; import org.apache.commons.csv... Test with a simple CSV file: psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,... Result: Line 1 has 11 values: |psmith01| |CLASS2B|... The parser worked correctly. Test with a more complicated CSV file: "psmith01 abc", "CLASS2B " , " Peter... Result: Line 1 has 4 values: |psmith01 abc| |CLASS... The third line is invalid input, but throwing a Java IOException is a bit grave. Also, the parser is not able to escape a backslash. Add a new line in item two: "One", "Two ", "Three" Result: Line 2 has 3 values: |One| |Two | |...
Created by alfa on May 24, 2011 15:59:41    Last update: May 24, 2011 15:59:41
Java reflection with Apache beanutils example. import org.apache.commons.beanutils.MethodUtils; ...
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...
Created by Dr. Xi on March 24, 2011 12:11:14    Last update: March 24, 2011 12:22:03
This is the task: your client wants to know how the web application is used. That is pretty easy. A plethora of commercial tools or any of the free log analysis tools such as analog and AWStats would fit the bill. But here's the catch: they want to know not only what pages are visited by how many people and when, but also who logged in and did what. Your application is using form based authentication and therefore, everyone is anonymous in the web access log. What to do? This is a servlet filter that generates a web access log with authenticated user info that can be fed to log analysis tools such as analog and AWStats . Filter code (the output format is Apache...
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 java.io.*; 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 November 23, 2010 22:11:54    Last update: November 23, 2010 22:12:49
JavaDoc says that you can call getOutputStream or getWriter on ServletResponse , but you cannot call both. The second call will get IllegalStateException . So this works: import java.io.*; import javax.servlet.*; im... So does this: import java.io.*; import javax.servlet.*; im... But not this: import java.io.*; import javax.servlet.*; im... You can make the last servlet work if you insert a filter like this: import java.io.*; import javax.servlet.*; im... But depending on the underlying implementation, the order of the output strings may be undetermined.
Created by Dr. Xi on September 13, 2008 20:13:48    Last update: September 13, 2008 20:14:05
Use CreditCardValidator to validate credit card number. You need the commons-validator.jar from apache. package example; import example.datatypes.C...