Recent Notes
Displaying keyword search results 51 - 60
Created by alfa on April 06, 2011 12:07:12
Last update: April 06, 2011 12:08:01
You can use Class.getresourceAsStream or ClassLoader.getresourceAsStream to read a file from classpath. Class.getresourceAsStream delegates to ClassLoader.getresourceAsStream , but it does some preprocessing before delegation:
If the file name begins with a '/', then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is: package_name_with_dot_replaced_by_slash/file_name
package com.demo.io;
import java.io.*;
...
Created by Dr. Xi on March 31, 2011 15:03:26
Last update: April 01, 2011 12:34:50
Create an openssl configuration file which enables subject alternative names ( openssl.cnf ):
[req]
distinguished_name = req_distinguished_...
Create a certificate request using above configuration file:
C:\work>openssl req -new -key testServer.key -out ...
Verify the request was created successfully:
C:\work>openssl req -text -noout -in testServer.cs...
(Optional) self-sign the certificate request:
C:\work>openssl x509 -req -days 3650 -in testServe...
Created by Dr. Xi on March 29, 2011 16:06:57
Last update: April 01, 2011 12:33:52
This utility class retrieves SSL certificates from the server and print them out to the stdout. The output can be saved to a file and imported to a Java keystore. This is useful in your test environment where the SSL certificate is self-signed.
import java.io.InputStream;
import java.io.Outp...
Retrieve and import the a certificate:
E:\test>java RetrieveSSLCert 192.168.69.144 8081 >...
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 java.io.*;
import java.nio.ByteBuffer;
...
According to the VM Spec , a Java class file has this structure:
ClassFile {
u4 magic;
...
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 March 02, 2011 11:39:18
Last update: March 09, 2011 12:19:30
Some peculiarities about Java PrintWriter: PrintWriter never throws any exceptions. From JavaDoc : Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError(). When error occurs, you'll never know anything more than that it occured, because checkError returns boolean. When a character is out of the range of the character encoding of the PrintWriter, it prints a question mark (?). But this is not an error. Test code:
import java.io.*; public class TestPrintWri... Latin1 test result: java TestPrintWriter iso-8859-1 | od -bc 000000... UTF-8 test result: java TestPrintWriter utf-8 | od -bc 0000000 141... Also, the constructor throws a FileNotFoundException when you try to write to a...
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 nogeek on February 08, 2011 13:46:18
Last update: February 08, 2011 13:46:18
Simple Java code that does XSLT on an XML file. The transform results go to STDOUT.
import java.io.*;
import javax.xml.parsers....
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 .