<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>betweenGo &#187; Java SE</title>
	<atom:link href="http://betweengo.com/category/java/se/feed/" rel="self" type="application/rss+xml" />
	<link>http://betweengo.com</link>
	<description>We make Ruby on Rails easy.  We make ATG easy.</description>
	<lastBuildDate>Thu, 24 Jun 2010 19:42:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>URLEncoder.encode is Deprecated So What Do I Use for Encoding?</title>
		<link>http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/</link>
		<comments>http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 18:39:54 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[deprecated]]></category>

		<guid isPermaLink="false">http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/</guid>
		<description><![CDATA[
(Photo: The Surface by Daniel*1977)
URLEncoder.encode(String url) is deprecated.&#160; Java wants you to use URLEncoder.encode(String url, String enc).&#160; But what do you put for the encoding parameter?&#160; I always forget which is the whole point of this post.  
URLEncoder.encode(url, &#34;UTF-8&#34;);
Also on Windows if you want you can do:
URLEncoder.encode(url, &#34;Cp1252&#34;);
For further reading please see default encoding [...]


Related posts:<ol><li><a href='http://betweengo.com/2009/03/17/encode-uri/' rel='bookmark' title='Permanent Link: Encode URI'>Encode URI</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><a href="http://www.flickr.com/photos/didmyself/3978834571/"><img title="The Surface on Flickr" alt="The Surface on Flickr" src="http://farm3.static.flickr.com/2476/3978834571_3e2528811f.jpg" /></a></p>
<p style="text-align: center">(Photo: <a title="The Surface on Flickr" href="http://www.flickr.com/photos/didmyself/3978834571/">The Surface</a> by <a title="Flickr: Daniel*1977&#39;s photostream" href="http://www.flickr.com/photos/didmyself/">Daniel*1977</a>)</p>
<p><a title="URLEncoder.encode(String url)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLEncoder.html#encode%28java.lang.String%29">URLEncoder.encode(String url)</a> is deprecated.&#160; Java wants you to use <a title="URLEncoder.encode(String url, String enc)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLEncoder.html#encode%28java.lang.String,%20java.lang.String%29">URLEncoder.encode(String url, String enc)</a>.&#160; But what do you put for the encoding parameter?&#160; I always forget which is the whole point of this post. <img src='http://betweengo.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<pre>URLEncoder.encode(url, &quot;UTF-8&quot;);</pre>
<p>Also on Windows if you want you can do:</p>
<pre>URLEncoder.encode(url, &quot;Cp1252&quot;);</pre>
<p>For further reading please see <a title="default encoding of a jvm" href="http://www.velocityreviews.com/forums/t124404-default-encoding-of-a-jvm.html">default encoding of a jvm</a>.</p>


<p>Related posts:<ol><li><a href='http://betweengo.com/2009/03/17/encode-uri/' rel='bookmark' title='Permanent Link: Encode URI'>Encode URI</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ResourceBundle and MessageFormat for Error Messages</title>
		<link>http://betweengo.com/2009/09/07/using-resourcebundle-and-messageformat-for-error-messages/</link>
		<comments>http://betweengo.com/2009/09/07/using-resourcebundle-and-messageformat-for-error-messages/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 12:00:00 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MessageFormat]]></category>
		<category><![CDATA[ResourceBundle]]></category>

		<guid isPermaLink="false">http://betweengo.com/2009/09/04/using-resourcebundle-and-messageformat-for-error-messages/</guid>
		<description><![CDATA[When generating error messages, two Java utility classes, ResourceBundle and MessageFormat, are extremely practical and powerful.&#160; From the ResourceBundle JavaDoc:
Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user&#8217;s locale. In this way, [...]


Related posts:<ol><li><a href='http://betweengo.com/2008/05/05/color-code-outlook-messages/' rel='bookmark' title='Permanent Link: Color Code Outlook Messages'>Color Code Outlook Messages</a></li>
<li><a href='http://betweengo.com/2006/03/20/want-to-disable-windows-error-reporting/' rel='bookmark' title='Permanent Link: HOWTO: Disable Windows Error Reporting?'>HOWTO: Disable Windows Error Reporting?</a></li>
<li><a href='http://betweengo.com/2008/04/03/error-in-my_thread_global_end-when-running-rake-dbmigrate/' rel='bookmark' title='Permanent Link: &#8216;Error in my_thread_global_end()&#8217; when running rake db:migrate'>&#8216;Error in my_thread_global_end()&#8217; when running rake db:migrate</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When generating error messages, two Java utility classes, <a title="ResourceBundle (Java Platform SE 6)" href="http://java.sun.com/javase/6/docs/api/java/util/ResourceBundle.html">ResourceBundle</a> and <a title="MessageFormat (Java Platform SE 6)" href="http://java.sun.com/javase/6/docs/api/java/text/MessageFormat.html">MessageFormat</a>, are extremely practical and powerful.&#160; From the ResourceBundle JavaDoc:</p>
<blockquote><p>Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a <a title="String (Java Platform SE 6)" href="http://java.sun.com/javase/6/docs/api/java/lang/String.html"><code>String</code></a> for example, your program can load it from the resource bundle that is appropriate for the current user&#8217;s locale. In this way, you can write program code that is largely independent of the user&#8217;s locale isolating most, if not all, of the locale-specific information in resource bundles. </p>
<p>This allows you to write programs that can: </p>
<ul>
<li>be easily localized, or translated, into different languages </li>
<li>handle multiple locales at once </li>
<li>be easily modified later to support even more locales </li>
</ul>
</blockquote>
<p>And from the MessageFormat JavaDoc:</p>
<blockquote><p><code>MessageFormat</code> provides a means to produce concatenated messages in a language-neutral way. Use this to construct messages displayed for end users. </p>
<p><code>MessageFormat</code> takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places. </p>
</blockquote>
<p>This is an example of an error message resource bundle, <tt>ErrorMessagesResources.properties</tt>.</p>
<pre>userAlreadyExists=A user already exists with the name {0}.
passwordInvalid=Please enter a valid password.</pre>
<p>This is an example of how you would use this resource bundle.</p>
<pre>protected static final ResourceBundle resourceBundle =
    ResourceBundle.getBundle(&quot;com.betweengo.ErrorMessageResources&quot;);

public boolean handleLogin(DynamoHttpServletRequest pReq, DynamoHttpServletResponse pRes) {

  ...

  // user already exists
  String errMsg1 = resourceBundle.getString(&quot;userAlreadyExists&quot;);
  errMsg1 = MessageFormat.format(errMsg1, userName);

  ...

  // password invalid
  String errMsg2 = resourceBundle.getString(&quot;passwordInvalid&quot;);

  ...
}</pre>


<p>Related posts:<ol><li><a href='http://betweengo.com/2008/05/05/color-code-outlook-messages/' rel='bookmark' title='Permanent Link: Color Code Outlook Messages'>Color Code Outlook Messages</a></li>
<li><a href='http://betweengo.com/2006/03/20/want-to-disable-windows-error-reporting/' rel='bookmark' title='Permanent Link: HOWTO: Disable Windows Error Reporting?'>HOWTO: Disable Windows Error Reporting?</a></li>
<li><a href='http://betweengo.com/2008/04/03/error-in-my_thread_global_end-when-running-rake-dbmigrate/' rel='bookmark' title='Permanent Link: &#8216;Error in my_thread_global_end()&#8217; when running rake db:migrate'>&#8216;Error in my_thread_global_end()&#8217; when running rake db:migrate</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/09/07/using-resourcebundle-and-messageformat-for-error-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enums in Java</title>
		<link>http://betweengo.com/2009/09/02/enums-in-java/</link>
		<comments>http://betweengo.com/2009/09/02/enums-in-java/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 17:00:00 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[enums]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/2009/09/01/enums-in-java/</guid>
		<description><![CDATA[
(Photo: Slide-together : now with cards by fdecomite)
Enums are highly useful data types introduced in Java SE 5.0.  Though I love using them I often forget the exact syntax so this post is to remind me later how to use it.
public enum Example {
  FOO,BAR
}

// create one using its name
Example myExample = Example.valueOf(“bar”.toUpperCase());

// if [...]


Related posts:<ol><li><a href='http://betweengo.com/2007/03/28/printing-out-an-array/' rel='bookmark' title='Permanent Link: Printing out an array'>Printing out an array</a></li>
<li><a href='http://betweengo.com/2008/09/01/java-method-with-generic-return-type/' rel='bookmark' title='Permanent Link: Java Method with Generic Return Type'>Java Method with Generic Return Type</a></li>
<li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><a href="http://www.flickr.com/photos/fdecomite/3611388781/"><img title="Slide-together : now with cards on Flickr" src="http://farm4.static.flickr.com/3303/3611388781_0110324b5f.jpg" alt="Slide-together : now with cards on Flickr" /></a><br />
(Photo: <a title="Slide-together : now with cards on Flickr" href="http://www.flickr.com/photos/fdecomite/3611388781/">Slide-together : now with cards</a> by <a title="Flickr: fdecomite's Photostream" href="http://www.flickr.com/photos/fdecomite/">fdecomite</a>)</p>
<p><a title="Enum (Java 2 SE Platform 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Enum.html">Enums</a> are highly useful data types introduced in <a title="J2SE 5.0" href="http://java.sun.com/j2se/1.5.0/">Java SE 5.0</a>.  Though I love using them I often forget the exact syntax so this post is to remind me later how to use it.</p>
<pre>public enum Example {
  FOO,BAR
}

// create one using its name
Example myExample = Example.valueOf(“bar”.toUpperCase());

// if statement
if (myExample == Example.FOO) System.out.println(“FOO!”);

// switch statement
switch (myExample) {
  case FOO: System.out.println(“FOO!”);
  case BAR: System.out.println(“BAR!”);
}

// output as String using name
System.out.println(myExample.name());
</pre>
<p>For further reading please see Java’s <a title="Enums" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html">Enums guide</a> and <a title="Enum Types (The Java™ Tutorials &gt; Learning the Java Language &gt; Classes and Objects)" href="http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html">Enum Types (The Java™ Tutorials &gt; Learning the Java Language &gt; Classes and Objects)</a>.</p>


<p>Related posts:<ol><li><a href='http://betweengo.com/2007/03/28/printing-out-an-array/' rel='bookmark' title='Permanent Link: Printing out an array'>Printing out an array</a></li>
<li><a href='http://betweengo.com/2008/09/01/java-method-with-generic-return-type/' rel='bookmark' title='Permanent Link: Java Method with Generic Return Type'>Java Method with Generic Return Type</a></li>
<li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/09/02/enums-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Test for Threaded Logging</title>
		<link>http://betweengo.com/2009/05/04/unit-test-for-threaded-logging/</link>
		<comments>http://betweengo.com/2009/05/04/unit-test-for-threaded-logging/#comments</comments>
		<pubDate>Mon, 04 May 2009 17:18:45 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Logging]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=709</guid>
		<description><![CDATA[Brian Ploetz sent me this great unit test for threaded logging.  In it we are trying to find if a deadlock occurs.
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;

/**
 * Unit test for the ThrottlingFilter in a multi-threaded environment
 */
public class ThrottlingFilterThreadUTest extends TestCase {

  private static final Log logger = LogFactory.getLog(ThrottlingFilterThreadUTest.class);

  [...]


Related posts:<ol><li><a href='http://betweengo.com/2006/04/04/howto-log-sql-on-jboss/' rel='bookmark' title='Permanent Link: How to Log SQL on JBoss'>How to Log SQL on JBoss</a></li>
<li><a href='http://betweengo.com/2008/05/27/log4j-levels/' rel='bookmark' title='Permanent Link: Log4j levels'>Log4j levels</a></li>
<li><a href='http://betweengo.com/2008/06/10/static-import/' rel='bookmark' title='Permanent Link: Static Import'>Static Import</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a title="Brian Ploetz" href="http://www.linkedin.com/in/bploetz">Brian Ploetz</a> sent me this great unit test for threaded logging.  In it we are trying to find if a deadlock occurs.</p>
<pre>import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;

/**
 * Unit test for the ThrottlingFilter in a multi-threaded environment
 */
public class ThrottlingFilterThreadUTest extends TestCase {

  private static final Log logger = LogFactory.getLog(ThrottlingFilterThreadUTest.class);

  private static ThreadMXBean threadMXBean;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    threadMXBean = ManagementFactory.getThreadMXBean();
    logger.info("Thread contention monitoring supported: "
        + threadMXBean.isThreadContentionMonitoringSupported());
    logger.info("Thread contention monitoring enabled: "
        + threadMXBean.isThreadContentionMonitoringEnabled());
    threadMXBean.setThreadContentionMonitoringEnabled(true);
    logger.info("Thread contention monitoring enabled: "
        + threadMXBean.isThreadContentionMonitoringEnabled());
  }

  /**
   * Tests multiple threads using the same filter instance at the same time
   */
  public void testThreads() {
    Logger rootLogger = Logger.getRootLogger();
    assertNotNull(rootLogger);
    Appender fileAppender = rootLogger.getAppender("FILE");
    assertNotNull(fileAppender);
    ThrottlingFilter throttlingFilter = (ThrottlingFilter) fileAppender.getFilter();
    assertNotNull(throttlingFilter);

    ThreadGroup infoThreadGroup = new ThreadGroup("info-group");
    ThreadGroup errorThreadGroup = new ThreadGroup("error-group");
    Thread errorThread1 = new ErrorThread(errorThreadGroup, "error-thread-1");
    Thread infoThread1 = new InfoThread(infoThreadGroup, "info-thread-1");
    Thread errorThread2 = new ErrorThread(errorThreadGroup, "error-thread-2");
    Thread infoThread2 = new InfoThread(infoThreadGroup, "info-thread-2");
    infoThread1.start();
    errorThread1.start();
    errorThread2.start();
    infoThread2.start();

    while (true) {
      ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds());
      for (int i = 0; i &lt; threadInfos.length; i++) {
        ThreadInfo threadInfo = threadInfos[i];
        if (threadInfo != null &amp;&amp; threadInfo.getThreadState() == Thread.State.BLOCKED) {
          System.out.println("Thread '" + threadInfo.getThreadName()
              + "' is blocked on the monitor lock '" + threadInfo.getLockName()
              + "' held by thread '" + threadInfo.getLockOwnerName() + "'");
        }
      }

      if (!infoThread1.isAlive() &amp;&amp; !errorThread1.isAlive() &amp;&amp; !infoThread2.isAlive()
          &amp;&amp; !errorThread2.isAlive())
        break;
    }
  }

  public static class ErrorThread extends Thread {

    private static final Log logger = LogFactory.getLog(ErrorThread.class);

    public ErrorThread(ThreadGroup tg, String name) {
      super(tg, name);
    }

    public void run() {
      for (int i = 0; i &lt; 10; i++) {
        try {
          test(0);
        } catch (Exception e) {
          long start = System.currentTimeMillis();
          logger.error("Error!", e);
          long end = System.currentTimeMillis();
          System.out.println("Took " + (end-start) + "ms to log error");
        }
      }
    }

    // simulate large stack traces
    private void test(int i) {
      if (i &gt;= 500)
        throw new RuntimeException("D'OH!");
      test(i+1);
    }
  }

  public static class InfoThread extends Thread {

    private static final Log logger = LogFactory.getLog(InfoThread.class);

    public InfoThread(ThreadGroup tg, String name) {
      super(tg, name);
    }

    public void run() {
      for (int i = 0; i &lt; 100; i++) {
        logger.info("Hi!");
      }
    }
  }
}</pre>
<p>The log4j.xml test file.</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;

&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
    debug="false"&gt;

    &lt;!-- ================================= --&gt;
    &lt;!--           Appenders               --&gt;
    &lt;!-- ================================= --&gt;

    &lt;!-- A time/date based rolling file appender --&gt;
    &lt;appender name="FILE"
        class="org.apache.log4j.DailyRollingFileAppender"&gt;
        &lt;param name="File" value="server.log" /&gt;
        &lt;param name="Append" value="true" /&gt;

        &lt;!-- Rollover at midnight each day --&gt;
        &lt;param name="DatePattern" value="'.'yyyy-MM-dd" /&gt;

        &lt;layout class="org.apache.log4j.PatternLayout"&gt;
            &lt;!-- The default pattern: Date Priority [Category] Message\n --&gt;
            &lt;param name="ConversionPattern" value="%d %-5p [%c] %m%n" /&gt;
        &lt;/layout&gt;

        &lt;filter class="com.betweengo.log4j.ThrottlingFilter"&gt;
          &lt;param name="maxCountSameMessage" value="100"/&gt;
          &lt;param name="maxCountSavedMessages" value="100"/&gt;
          &lt;param name="waitInterval" value="60"/&gt;
        &lt;/filter&gt;
    &lt;/appender&gt;

    &lt;!-- ======================= --&gt;
    &lt;!-- Setup the Root category --&gt;
    &lt;!-- ======================= --&gt;

    &lt;root&gt;
        &lt;level value="INFO" /&gt;
        &lt;appender-ref ref="FILE" /&gt;
    &lt;/root&gt;

&lt;/log4j:configuration&gt;</pre>


<p>Related posts:<ol><li><a href='http://betweengo.com/2006/04/04/howto-log-sql-on-jboss/' rel='bookmark' title='Permanent Link: How to Log SQL on JBoss'>How to Log SQL on JBoss</a></li>
<li><a href='http://betweengo.com/2008/05/27/log4j-levels/' rel='bookmark' title='Permanent Link: Log4j levels'>Log4j levels</a></li>
<li><a href='http://betweengo.com/2008/06/10/static-import/' rel='bookmark' title='Permanent Link: Static Import'>Static Import</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/05/04/unit-test-for-threaded-logging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encode URI</title>
		<link>http://betweengo.com/2009/03/17/encode-uri/</link>
		<comments>http://betweengo.com/2009/03/17/encode-uri/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 21:14:56 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=683</guid>
		<description><![CDATA[To encode an URI you can simply use Java&#8217;s URLEncoder&#8217;s encode method which has been available since JDK 1.4.
String encodedUri;
  try {
    encodedUri = URLEncoder.encode(uri, "UTF-8");
  }
  catch (UnsupportedEncodingException exc) {
    // this should never happen
    logger.warn("UTF-8 is not a supported encoding?  [...]


Related posts:<ol><li><a href='http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/' rel='bookmark' title='Permanent Link: URLEncoder.encode is Deprecated So What Do I Use for Encoding?'>URLEncoder.encode is Deprecated So What Do I Use for Encoding?</a></li>
<li><a href='http://betweengo.com/2008/05/27/jstl-for-current-uri/' rel='bookmark' title='Permanent Link: JSTL for current URI'>JSTL for current URI</a></li>
<li><a href='http://betweengo.com/2007/03/28/printing-out-an-array/' rel='bookmark' title='Permanent Link: Printing out an array'>Printing out an array</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>To encode an URI you can simply use Java&#8217;s <a title="URLEncoder (Java 2 Platform SE v1.4.2)" href="http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLEncoder.html#encode(java.lang.String,%20java.lang.String)">URLEncoder&#8217;s encode</a> method which has been available since JDK 1.4.</p>
<pre>String encodedUri;
  try {
    encodedUri = URLEncoder.encode(uri, "UTF-8");
  }
  catch (UnsupportedEncodingException exc) {
    // this should never happen
    logger.warn("UTF-8 is not a supported encoding?  Not encoding for now...", exc);
    encodedUri = uri;
  }</pre>


<p>Related posts:<ol><li><a href='http://betweengo.com/2009/10/31/urlencoder-encode-is-deprecated-so-what-do-i-use-for-encoding/' rel='bookmark' title='Permanent Link: URLEncoder.encode is Deprecated So What Do I Use for Encoding?'>URLEncoder.encode is Deprecated So What Do I Use for Encoding?</a></li>
<li><a href='http://betweengo.com/2008/05/27/jstl-for-current-uri/' rel='bookmark' title='Permanent Link: JSTL for current URI'>JSTL for current URI</a></li>
<li><a href='http://betweengo.com/2007/03/28/printing-out-an-array/' rel='bookmark' title='Permanent Link: Printing out an array'>Printing out an array</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/03/17/encode-uri/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sleep</title>
		<link>http://betweengo.com/2009/02/26/sleep/</link>
		<comments>http://betweengo.com/2009/02/26/sleep/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 21:30:13 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=443</guid>
		<description><![CDATA[I always forget how to sleep or wait in Java though it&#8217;s quite easy, just use the static method Thread.sleep.
For example:
    // sleep the filter's wait interval
    try {
      Thread.sleep(filter.getWaitInterval() * 1000);
    }
    catch (InterruptedException exc) {
      logger.error("unexpected interrupt", exc);
    }
Sun has a tutorial calling Pausing Execution with Sleep.


Related posts:How to schedule tasks or perform [...]


Related posts:<ol><li><a href='http://betweengo.com/2005/09/29/schedule-tasks/' rel='bookmark' title='Permanent Link: How to schedule tasks or perform tasks repeatedly'>How to schedule tasks or perform tasks repeatedly</a></li>
<li><a href='http://betweengo.com/2009/03/17/encode-uri/' rel='bookmark' title='Permanent Link: Encode URI'>Encode URI</a></li>
<li><a href='http://betweengo.com/2009/05/04/unit-test-for-threaded-logging/' rel='bookmark' title='Permanent Link: Unit Test for Threaded Logging'>Unit Test for Threaded Logging</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I always forget how to sleep or wait in Java though it&#8217;s quite easy, just use the static method <a title="Thread.sleep(long)" href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#sleep(long)">Thread.sleep</a>.</p>
<p>For example:</p>
<pre>    // sleep the filter's wait interval
    try {
      Thread.sleep(filter.getWaitInterval() * 1000);
    }
    catch (InterruptedException exc) {
      logger.error("unexpected interrupt", exc);
    }</pre>
<p>Sun has a tutorial calling <a title="Pausing Execution with Sleep" href="http://java.sun.com/docs/books/tutorial/essential/concurrency/sleep.html">Pausing Execution with Sleep</a>.</p>


<p>Related posts:<ol><li><a href='http://betweengo.com/2005/09/29/schedule-tasks/' rel='bookmark' title='Permanent Link: How to schedule tasks or perform tasks repeatedly'>How to schedule tasks or perform tasks repeatedly</a></li>
<li><a href='http://betweengo.com/2009/03/17/encode-uri/' rel='bookmark' title='Permanent Link: Encode URI'>Encode URI</a></li>
<li><a href='http://betweengo.com/2009/05/04/unit-test-for-threaded-logging/' rel='bookmark' title='Permanent Link: Unit Test for Threaded Logging'>Unit Test for Threaded Logging</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/02/26/sleep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Do Not Return From a Try Block</title>
		<link>http://betweengo.com/2009/01/29/do-not-return-from-a-try-block/</link>
		<comments>http://betweengo.com/2009/01/29/do-not-return-from-a-try-block/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 17:54:44 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=399</guid>
		<description><![CDATA[In Peter Hagar&#8217;s book, Practical Java, he recommends that you do not return from a try block.  This is because the finally block may change the return value.
Traditionally, programmers think that when they execute a return statement they immediately leave the method they are executing.  In Java, this is no longer true with the usage [...]


Related posts:<ol><li><a href='http://betweengo.com/2008/09/01/java-method-with-generic-return-type/' rel='bookmark' title='Permanent Link: Java Method with Generic Return Type'>Java Method with Generic Return Type</a></li>
<li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
<li><a href='http://betweengo.com/2008/09/01/effective-java-collections/' rel='bookmark' title='Permanent Link: Effective Java Collections'>Effective Java Collections</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.amazon.com/gp/product/0201616467?ie=UTF8&amp;tag=frankkimsride-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201616467"><img class="size-full wp-image-402 alignright" title="Practical Java(TM) Programming Language Guide (Addison-Wesley Professional Computing Series)" src="http://betweengo.com/wp-content/uploads/2009/01/practical-java.jpg" alt="Practical Java(TM) Programming Language Guide (Addison-Wesley Professional Computing Series)" width="128" height="160" /></a>In Peter Hagar&#8217;s book, <a title="Practical Java(TM) Programming Language Guide (Addison-Wesley Professional Computing Series)" href="http://www.amazon.com/gp/product/0201616467?ie=UTF8&amp;tag=frankkimsride-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0201616467">Practical Java</a>, he recommends that you do not return from a try block.  This is because the finally block may change the return value.</p>
<blockquote><p>Traditionally, programmers think that when they execute a return statement they immediately leave the method they are executing.  In Java, this is no longer true with the usage of <tt>finally</tt>&#8230;</p>
<p>To avoid this pitfall, be sure you do not issue a <tt>return</tt>, <tt>break</tt> or <tt>continue</tt> statement inside of a <tt>try</tt> block.  If you cannot avoid this, be sure the existence of a <tt>finally</tt> does not change the return value of the method.  This particular problem can arise during maintenance of your code, even with careful design and implementation.  Good comments and careful code reviews ward it off.</p>
<p><a href="http://books.google.com/books?id=iWPeqljHNcoC&amp;pg=PA79">Practical Java Programming Language &#8230; &#8211; Google Book Search</a></p></blockquote>


<p>Related posts:<ol><li><a href='http://betweengo.com/2008/09/01/java-method-with-generic-return-type/' rel='bookmark' title='Permanent Link: Java Method with Generic Return Type'>Java Method with Generic Return Type</a></li>
<li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
<li><a href='http://betweengo.com/2008/09/01/effective-java-collections/' rel='bookmark' title='Permanent Link: Effective Java Collections'>Effective Java Collections</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/01/29/do-not-return-from-a-try-block/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>URL.equals and hashCode make blocking Internet connections</title>
		<link>http://betweengo.com/2009/01/15/urlequals-and-hashcode-make-blocking-internet-connections/</link>
		<comments>http://betweengo.com/2009/01/15/urlequals-and-hashcode-make-blocking-internet-connections/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 15:03:45 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=396</guid>
		<description><![CDATA[Who knew that something as innocent as java.net.URL.equals and hashCode would make blocking Internet connections?
The javadoc of URL.equals says: &#8220;Since hosts comparison requires name resolution, this operation is a blocking operation.&#8221;, but who reads the documentation of equals?  There is a general contract around equals.  Joshua Bloch writes in Effective Java: &#8220;Don&#8217;t write an equals [...]


Related posts:<ol><li><a href='http://betweengo.com/2005/09/14/java-filenotfoundexception-for-valid-url/' rel='bookmark' title='Permanent Link: java.io.FileNotFoundException for valid URL'>java.io.FileNotFoundException for valid URL</a></li>
<li><a href='http://betweengo.com/2009/08/11/javascript-invalid-argument-internet-explorer-only/' rel='bookmark' title='Permanent Link: JavaScript Invalid Argument in Internet Explorer Only'>JavaScript Invalid Argument in Internet Explorer Only</a></li>
<li><a href='http://betweengo.com/2005/08/08/internet-explorer-form-does-not-invoke-atg-handler/' rel='bookmark' title='Permanent Link: Internet Explorer form does not invoke ATG handler'>Internet Explorer form does not invoke ATG handler</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Who knew that something as innocent as java.net.URL.equals and hashCode would make blocking Internet connections?</p>
<blockquote><p>The javadoc of <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html#equals%28java.lang.Object%29">URL.equals</a> says: &#8220;Since hosts comparison requires name resolution, this operation is a blocking operation.&#8221;, but who reads the documentation of equals?  There is a general contract around equals.  Joshua Bloch writes in <a href="http://java.sun.com/docs/books/effective/">Effective Java</a>: &#8220;Don&#8217;t write an equals that relies on unreliable resources&#8221; (<a href="http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf">Chapter 3, page 34</a>). Hey Sun, as far as I know, the Internet is not reliable <img src='http://betweengo.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p><a href="http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html">Eclipse and Java Blog by Michael Scharf: java.net.URL.equals and hashCode make (blocking) Internet connections&#8230;.</a></p></blockquote>


<p>Related posts:<ol><li><a href='http://betweengo.com/2005/09/14/java-filenotfoundexception-for-valid-url/' rel='bookmark' title='Permanent Link: java.io.FileNotFoundException for valid URL'>java.io.FileNotFoundException for valid URL</a></li>
<li><a href='http://betweengo.com/2009/08/11/javascript-invalid-argument-internet-explorer-only/' rel='bookmark' title='Permanent Link: JavaScript Invalid Argument in Internet Explorer Only'>JavaScript Invalid Argument in Internet Explorer Only</a></li>
<li><a href='http://betweengo.com/2005/08/08/internet-explorer-form-does-not-invoke-atg-handler/' rel='bookmark' title='Permanent Link: Internet Explorer form does not invoke ATG handler'>Internet Explorer form does not invoke ATG handler</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2009/01/15/urlequals-and-hashcode-make-blocking-internet-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ServletException root cause</title>
		<link>http://betweengo.com/2008/12/30/servletexception-root-cause/</link>
		<comments>http://betweengo.com/2008/12/30/servletexception-root-cause/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 21:46:20 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=380</guid>
		<description><![CDATA[Java&#8217;s Throwable class defines the getCause() method for accessing the cause of the exception.  This method returns a Throwable object which itself could have a cause.  By traversing down this chain you can find the root cause of an exception.
However for some unknown reason in ServletException the getRootCause() method was added.  Therefore when trying to [...]


Related posts:<ol><li><a href='http://betweengo.com/2009/03/02/dynamically-generate-sitemapxml/' rel='bookmark' title='Permanent Link: Dynamically generate sitemap.xml'>Dynamically generate sitemap.xml</a></li>
<li><a href='http://betweengo.com/2007/02/06/accessing-class-from-static-method-within-class/' rel='bookmark' title='Permanent Link: Accessing the class from a static method which the class owns'>Accessing the class from a static method which the class owns</a></li>
<li><a href='http://betweengo.com/2009/03/02/mod_rewrite-to-bypass-security/' rel='bookmark' title='Permanent Link: mod_rewrite to bypass security'>mod_rewrite to bypass security</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Java&#8217;s <a title="Throwable (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html">Throwable</a> class defines the <a title="Throwable getCause()" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#getCause()">getCause()</a> method for accessing the cause of the exception.  This method returns a Throwable object which itself could have a cause.  By traversing down this chain you can find the root cause of an exception.</p>
<p>However for some unknown reason in <a title="ServletException (Java EE 5)" href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletException.html">ServletException</a> the <a title="ServletException getRootCause()" href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletException.html#getRootCause()">getRootCause()</a> method was added.  Therefore when trying to determine the root cause of an exception in a J2EE environment one has to check what type of exception you have.  I do this in the following code.</p>
<pre>  /**
   * Logs all the nested exceptions for the specified exception.
   *
   * @param ex the exception
   */
  protected void logNestedExceptions(Throwable ex) {
    int count = 1;
    Throwable cause = getCause(ex);
    while (cause != null) {
      logger.error("Nested Exception " + count, cause);
      cause = getCause(cause);
      count++;
    }
  }

  /**
   * Gets the cause of the exception.
   *
   * @param ex the exception
   * @return the cause
   */
  protected Throwable getCause(Throwable ex) {
    Throwable cause;
    if (ex instanceof ServletException) {
      ServletException sex = (ServletException) ex;
      cause = sex.getRootCause();
    }
    else {
      cause = ex.getCause();
    }
    return cause;
  }</pre>
<p>Finally you need to configure web.xml to use your SiteMap servlet.</p>
<pre>&lt;servlet&gt;
    &lt;servlet-name&gt;sitemap&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.betweengo.servlet.SiteMap&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;sitemap&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/sitemap.xml&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;</pre>


<p>Related posts:<ol><li><a href='http://betweengo.com/2009/03/02/dynamically-generate-sitemapxml/' rel='bookmark' title='Permanent Link: Dynamically generate sitemap.xml'>Dynamically generate sitemap.xml</a></li>
<li><a href='http://betweengo.com/2007/02/06/accessing-class-from-static-method-within-class/' rel='bookmark' title='Permanent Link: Accessing the class from a static method which the class owns'>Accessing the class from a static method which the class owns</a></li>
<li><a href='http://betweengo.com/2009/03/02/mod_rewrite-to-bypass-security/' rel='bookmark' title='Permanent Link: mod_rewrite to bypass security'>mod_rewrite to bypass security</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2008/12/30/servletexception-root-cause/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Method with Generic Return Type</title>
		<link>http://betweengo.com/2008/09/01/java-method-with-generic-return-type/</link>
		<comments>http://betweengo.com/2008/09/01/java-method-with-generic-return-type/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 04:43:48 +0000</pubDate>
		<dc:creator>Frank Kim</dc:creator>
				<category><![CDATA[Java SE]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://betweengo.com/?p=298</guid>
		<description><![CDATA[To create a Java method with a generic return type one can write a method like this.
  protected static &#60;T&#62; T evaluateExpression(String tagName,
      String attributeName, String attributeValue,
      Class&#60;T&#62; expectedType, Tag tagRef,
      PageContext pageContext)
You can learn more at this Java Generics [...]


Related posts:<ol><li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
<li><a href='http://betweengo.com/2007/02/06/accessing-class-from-static-method-within-class/' rel='bookmark' title='Permanent Link: Accessing the class from a static method which the class owns'>Accessing the class from a static method which the class owns</a></li>
<li><a href='http://betweengo.com/2008/09/01/effective-java-collections/' rel='bookmark' title='Permanent Link: Effective Java Collections'>Effective Java Collections</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>To create a Java method with a generic return type one can write a method like this.</p>
<pre>  protected static &lt;T&gt; T evaluateExpression(String tagName,
      String attributeName, String attributeValue,
      Class&lt;T&gt; expectedType, Tag tagRef,
      PageContext pageContext)</pre>
<p>You can learn more at this <a title="Java Generics FAQs - Programming With Java Generics " href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html">Java Generics FAQ</a>.</p>


<p>Related posts:<ol><li><a href='http://betweengo.com/2008/06/12/covariant-return-types/' rel='bookmark' title='Permanent Link: Covariant Return Types in Java'>Covariant Return Types in Java</a></li>
<li><a href='http://betweengo.com/2007/02/06/accessing-class-from-static-method-within-class/' rel='bookmark' title='Permanent Link: Accessing the class from a static method which the class owns'>Accessing the class from a static method which the class owns</a></li>
<li><a href='http://betweengo.com/2008/09/01/effective-java-collections/' rel='bookmark' title='Permanent Link: Effective Java Collections'>Effective Java Collections</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://betweengo.com/2008/09/01/java-method-with-generic-return-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->