Search code examples
javaapachetomcat6reverse-proxymod-jk

Posting to HttpURLConnection with Apache, Tomcat and modjk from Java web application


I encoutered a problem and i don't know how to solve it. I have a java application deployed on a tomcat. The tomcat is behind an apache using modjk. In the application I'm posting some XML to an URL like this:

URLConnection uc = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) uc;
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", "text/xml");
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.write(xml);
pw.close();
ServletOutputStream stream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
int readBytes;
while ((readBytes = bis.read()) != -1) {
  stream.write(readBytes);
}
bis.close();
stream.close();

The URL points to a web application on another server.

If I'm running the application directly from tomcat, everything works fine. After posting the XML, the requested page opens in the browser.

If I'm running the application using the apache with modjk, just the HTML code of the requested page instead of the rendered page is shown. The rest of the application works fine.

I guess there is a problem posting to the URL using apache and modjk. Does anyone know how to solve this problem?


Solution

  • I figured out a solution for this issue. I just needed to set the content type explicitly:

    response.setContentType("text/html;charset=UTF-8");
    

    By using only tomcat, it wasn't nessesary, but it seems to be required when using apache as reverse proxy.