Showing posts with label tomcat. Show all posts
Showing posts with label tomcat. Show all posts

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.

Monday, December 17, 2007

Tomcat java.net.BindException: Cannot assign requested address

I had a very strange error today when I tried to startup tomcat on some linux box that used to work. Since google was no help I figured I should document this somewhere.

Tomcat startup gave the notorious java.net.BindException: Cannot assign requested address. This usually means that another process is listening on the same port on this machine. I started with ps -ef grep java to make sure that no other tomcat process was running on the machine. Nothing.

I suspected that maybe some other web server was holding the port, but netstat -an showed that nothing binds this address. Moreover, telnet localhost 80 showed that no one is listening.

Looking at the server.xml I found out that the connector was binding to the hostname "demoserver" on port 80.

<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
address="demoserver" port="80"
minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="100" debug="0" connectionTimeout="20000"
useURIValidationHack="false" disableUploadTimeout="true" />

trying to ping demoserver gave me errors, but nslookup demoserver gave me the right ip address.

After a few minutes I noticed that some clown added demoserver to /etc/hosts with the wrong ip address. Fixing /etc/hosts to have the right ip address for demoserver made tomcat work smooth and nice.

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>

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:

Thursday, June 7, 2007

Server Header in Apache Tomcat

Altough you cannot disable the header, you can change it to anything you like by adding a "server" attribute to the connector tag in server.xml
e.g.

<connector
port="80" address="localhost"
...
server="Undefined"
>