Encoding filter for Java web applications

Dealing correctly with encodings is one of the most important things in Java web applications (if not even in Java). The best way to avoid troubles with different encodings is to use only one encoding throughout the entire web application. The encoding of choice is UTF-8 which is able to deal with almost every known written language.

The first thing you have to ensure is that every content delivery from your server tells the client (browser) the correct encoding to use. You do this by setting the meta header field in your HTML pages:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

and define the contentType in the page directive of your JSP

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

The attribute pageEncoding tells the JSP compiler the encoding in which the JSP is stored on the disk.

Now your content should be delivered correctly to the client. The next important thing to do is to handle data the client sends to you with the correct encoding. Most application containers use a hard coded standard encoding do decode request data. For the Apache Tomcat the default encoding is iso-8859-1.

If you have an input form that is delivered in utf-8 the browser will submit the form data in the same encoding. Now you get a problem if you call request.getParameter in your application and the parameter contains special characters. To tell the application server the right encoding to decode your request you have to set request.setCharacterEncoding before accessing a request parameter the first time.

Therefore you should use a Filter like shown in the following code:

public class EncodingFilter implements Filter {

	private String encoding = "utf-8";

	public void doFilter(ServletRequest request,

	ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		filterChain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		String encodingParam = filterConfig.getInitParameter("encoding");
		if (encodingParam != null) {
			encoding = encodingParam;
		}
	}

	public void destroy() {
		// nothing todo
	}

}

You have to configure this filter in your web.xml to be executed before every request:

<filter>

	<filter-name>EncodingFilter</filter-name>
	<filter-class>
		net.einwaller.filters.EncodingFilter
	</filter-class>

	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>

</filter>

<filter-mapping>
	<filter-name>EncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

If you mind all these tips your web application will not have any encoding problems – EXCEPT with GET requests. In GET requests the parameters are encoded into the URL not in the body of the request like in POST requests. These parameters are handled different by the application container.

Tomcat again uses iso-8859-1 per default to decode those GET parameters regardless of which encoding is set in the request by your filter. To change this behavior you have to change connector configuration inside the server.xml of your Tomcat as described here. The attribute URIEncoding defines a fixed encoding the server uses for every request. What we want to use is the attribute useBodyEncodingForURI which tells the server to use the encoding defined for the body for the GET parameters too.

Tomcat remote debugging

Sometimes your web applications behave different when deployed to the application server although everything worked fine when running inside Eclipse on your workstation.

In this case it is helpful to remote debug the application. In case of using Tomcat as application server just add the following VM parameters to JAVA_OPTS:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

I do this inside the file setenv.sh in the bin directory by adding a line JAVA_OPTS=”…options…”

The paramete -Xdebug tells the VM to start in debug mode. -Xrunjdwp selects the protocol. Change the port 8000 to an appropriate value for your server (one that is not used by another process and is reachable). By setting the value suspend=n the VM does not wait until a remote debugging session connects until starting.

Now all you have to to is start the server and run a remote debug session in Eclipse. You do this by selecting “Run -> Debug…” from the menu. Then the following dialog appears:

Remote Debugging in Eclipse

Select the project that contains the application running on the server, enter the hostname of the server and the port you configured in the VM options and click Debug. Now set your breakpoints and debug your application.


Firefox proxy plugin


Today I found a great Firefox plugin for configuring proxy servers. It allows you to define multiple proxy servers and patterns (per wildcards or regular expressions) when to use which proxy server.

As a software developer it is very usefull to be able to define which site is loaded thru a proxy and which not (localhost, intranet servers).

Ctrl-S in a linux shell

When using vi to edit documents on linux systems from time to time I press Ctrl-S to save my changes – damn Windows shortcuts ;-) This kind of freezes the shell because it locks the terminal output.

If this happens to you press Ctrl-Q which causes the shell to resume terminal output. See this blog for more linux terminal shortcuts.

Cool interview with Bill Gates

Scoble made a really cool interview with Bill Gates and provides the video of it on his blog. Gates seems really relaxed unlike in many other interviews I have seen. He talks about his retirement and (of course) the future of Microsoft and Computers in general.

Must be great to sit on one table with one THE software pioneers – thanks for sharing this experience Scoble!

And finally it’s out: the iPhone

Blogs of tech enthusiasts are really busy these days ’cause CES Las Vegas is happening. But the biggest news today may be the release of the iPhone. Steve Jobs presented it today in his Keynote.

I love Apple slogans: Introducing iPhone. Apple reinvents the Phone.

iPhone

Check out these cool sites having all the details of Apples next coup:

http://www.engadget.com/2007/01/09/the-apple-iphone/

http://www.mactechnews.de/mwsf2007ticker/index.html

http://www.tuaw.com/2007/01/09/iphone-announced

And last but not least: the official iPhone page

The only question for me: Will it have BlackBerry functionality? ;-)