Recent Notes
Displaying keyword search results 81 - 90
Created by meiu on March 31, 2011 19:54:05
Last update: March 31, 2011 19:54:05
With StringBuffer/StringBuilder:
public class ReverseString {
private static...
Without StringBuffer/StringBuilder:
public class ReverseString {
private static...
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 October 16, 2008 20:45:40
Last update: March 28, 2011 20:23:22
Java's built-in classes are way too complex/flexible for a simple protocol like HTTP. This is a wrapper to simplify HTTP GET and POST.
import java.io.*;
import java.net.*;
imp...
A simple test:
import java.io.*;
import java.util.*;
...
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 freyo on February 23, 2011 13:38:23
Last update: February 23, 2011 13:38:23
The Java keytool utility does not support importing a private key directly from a file. But it does support merging a keystore with the -importkeystore command. So, for a private key generated with OpenSSL in PEM format, you first convert the PEM key into PKCS12 format , then merge the one-key PKCS12 store with the Java KeyStore:
C:\>keytool -importkeystore -srckeystore openssl_c...
Created by freyo on February 23, 2011 13:21:12
Last update: February 23, 2011 13:21:12
I tried to convert a private key from PEM to PKCS12 with OpenSSL and got this error:
C:\myworks>openssl pkcs12 -export -in openssl_ca3....
The problem was that the -in parameter expects both private key and certificate in the same input file, i.e., openssl_ca3.pem in the above example. Appending the certificate file to the key fixed the problem:
C:\myworks>cat openssl_ca3.crt >>openssl_ca3.pem
...
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 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 October 26, 2010 16:07:40
Last update: October 26, 2010 16:07:40
This is a more generic version, which can be expanded to accommodate additional file signatures.
import java.io.*;
import java.util.*;
pu...