Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, January 26, 2010

IIS JNLP 404 Problem

If your IIS 6 returns a 404 Not Found when you request a Java Web Start jnlp file, then you probably need to add the jnlp mime-type to the IIS configuration.

IIS Settings -> Right click the machine -> mime-types -> new Extension: .jnlp MIME Type: application/x-java-jnlp-file


Sunday, February 8, 2009

utf8 and hebrew in tomcat

In tomcat 5.0 and above, if your UTF-8 request parameters are received as gibberish you might need to do the following:

In your server.xml add the URIEncoding="UTF-8" and useBodyEncodingForURI="true" to the Connector tag(s):

<Connector
useBodyEncodingForURI="true"
URIEncoding="UTF-8"

acceptCount="100"
enableLookups="false"
maxSpareThreads="75"
maxThreads="150"
minSpareThreads="25"
port="8080"
redirectPort="8443" />

This should make GET requests work properly.

For some reason the above does not work for POST requests. If you ask the tomcat people they'll mumble something about W3C, RFC, and RTFM. The short way to have this work for POST requests is to write a small filter to set the request encoding properly. We are using something similar to this:

package com.realcommerce.filters;
import javax.servlet.*;
import java.io.*;
public class RequestEncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
//Do Nothing
}
public void destroy() {
//Do Nothing
}
public void doFilter(ServletRequest request,ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
}

This made POST requests pass Hebrew (or any UTF-8) parameters properly.

Wednesday, November 21, 2007

Java TTL Cache, the Quick and Dirty Way

There are many caching systems in java. I really like some of them (especially Ian Schumacher's LRU Cache). But sometimes you just to drop a quick few lines to get something in a local cache. Here is one way of doing it:

private static Map urlItemCache = Collections.synchronizedMap(new HashMap());
private static Map urlItemCacheTtl = Collections.synchronizedMap(new HashMap());
private static final long TTL=1000*60*60;//one hour in milliseconds

public static String findItemByUrl( String url) {
String item="";
Long now=Long.valueOf(Calendar.getInstance().getTimeInMillis());

//try to get the item from the local cache
item = urlItemCache.get(url);
if ( !isEmpty(item)) {
//urlItemCacheTtl.get(url) is guaranteed to have a value, since it's added at the same time urlItemCache is.
if (now.longValue() - urlItemCacheTtl.get(url).longValue() < TTL ) {
urlItemCacheTtl.put(url, now); //see comment below
return item;
} else {
urlItemCache.remove(url);
urlItemCacheTtl.remove(url);
}
}
//not found in cache
//... do you application magic here to get the item ...
if ( !isEmpty(item) ) {
urlItemCache.put(url, item); //add to local cache
urlItemCacheTtl.put(url, now);
return item;
} //else
return null;
}

Wednesday, September 26, 2007

Reload webapp from ant script

A co-worker asked me how he can reload a web-application from an ant script. GDS was very helpfull finding the line in an old ant script I used to use. In the example below you might want to define $project.output

<property name="project.output" value="WEB-INF/classes"/>
<!-- reload the current webapp to refresh the classes that we just changed -->
<get
src="http://localhost:8080/manager/reload?path=/your-web-app"
username="admin-user"
password="admin-pass"
dest="${project.output}/log/reload.log"
/>
<concat><fileset dir="${project.output}/log" includes="reload.log"/></concat>

Monday, September 24, 2007

RequestDispatcher.forward() and filters

If you have read this post then you probably know that if you use RequestDispatcher.forward() (or RequestDispatcher.include()) the filter chain does not get re-called for the forwarded servlet/jsp. However, if you read more closely you see that Craig was talking about the 2.3 servlet spec. Since we are using tomcat 5.5 which conforms to the 2.4 spec, you should know that there's a new <dispatcher> element in the deployment descriptor with possible values REQUEST, FORWARD, INCLUDE, and ERROR. You can add any number of entries to a <filter-mapping> tag and individually decide which filter gets called when. An Example:


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

<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

Simpler GUI for Jad, The Java Decompiler from As I Learn

This is definitely the most useful two-liner I've seen in a while:
Simpler GUI for Jad, The Java Decompiler.

I naturally customized it to:



jad -o -s java -lnc -d%TEMP% %1
"C:\Program Files\TextPad 5\TextPad.exe" %TEMP%.\%~n1.java

Changed extension to java so that textpad does syntax highlighting; added -lnc so that jad outputs the original line numbers as comments and I can trace the exceptions I get

(after a few questions from people i'm editing this post:)
You will need to download jad and extract the exeutable to somewhere on your path, say C:\windows (or c:\winnt). Then create a new .cmd file, paste the above into the .cmd file, and put it somewhere. Then rightclick any .class file you can find, choose "Open With..."->"Choose Program"->"Browse..." and find the cmd file you created (don't forget to check the "Always use this program"). This way, any .class you'll double-click will decompile and open in textpad.

Monday, September 17, 2007

Using java regular expressions to match groups

It's been demonstrated over and over again, but I always seem to forget the "simple example". So here it is:


import java.util.regex.Matcher;
import java.util.regex.Pattern;
...
/*
*Going to match a URL that looks like this
* /url/prefix/--sitename--/menu.--menuid--/?param=--itemid--
* and change it into
* /url/prefix/--sitename--/--menuname--/--itemname--
*/
String expr="(/url/prefix/[a-zA-Z0-9_-]+)/menu\\.([a-zA-Z0-9_-]+)/\\?param=([a-zA-Z0-9_-]+)";
Pattern p = Pattern.compile(expr);
Matcher m = p.matcher(uri);
if ( m.matches() ) {
prefix= m.group(1);
menuitem = m.group(2);
guid = m.group(3);
if ( guid != null && prefix != null && menuitem != null ) {
uribuilder.append(prefix);
uribuilder.append("/");
uribuilder.append(getMenuName(menuitem));
uribuilder.append("/");
uribuilder.append(getItemName(guid));
uri=uribuilder.toString();
}
}

Tuesday, July 31, 2007

Two webapps using shared jar - ClassLoader problem

Yeah, yeah, I know it's obvious, but i thought i might be smarter. I am not:


Charles R Caldarale Writes:

[When using Tomcat,] classes from different webapps cannot access each other - no ifs, ands, or buts. Anything that is to be accessed from more than one webapp must be put in a common location [such as tomcat/shared].

See: