11

There is a servlet running on tomcat7 and it makes a webservice call to a third party website. The call works fine from the windows machine but when run from tomcat it fails. Wont Tomcat automatically use the Windows' proxy settings? I added

set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxySet=true"
set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyHost=IP"
set JAVA_OPTS=%JAVA_OPTS% "-Dhttp.proxyPort=8080"

to CATALINA.BAT and

http.proxyHost=IP
http.proxyPort=8080

to catalina.properties But still there is no change. How do we set Tomcat to use the proxy settings of windows and is there a way to check if tomcat is picking up the proxy settings specified?

icedek
  • 574
  • 2
  • 13
  • 31

6 Answers6

32

I do not agree with the usage of java.net.Proxy.

What happens if you need to change it ? New build, new release. The setting of the proxy should be easy. It works well with both system properties or tomcat JAVA_OPTS. I used it in both ways. Just pay attention and be sure you know what JAVA_OPTS are loaded, what java is used and so on, because there are tomcats that have their own java version. Regardint the previous post, there is no way java can be used before was loaded :). So Tomcat cannot use it before the system properties are used...only if tomcat uses another JRE that does not read system properties.

I just test this setup :

set "JAVA_OPTS=%JAVA_OPTS% -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 "

in catalina.bat and works well.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • 4
    Best solution in my opinion. – Diego Magdaleno Apr 17 '15 at 19:33
  • best sollution and according to the documentation. – giannisapi Jun 15 '16 at 09:54
  • What happens if you need to change it is that you put the configuration elements (host, port) into a configuration file, which is all you have to change. No new build required, and no release either necessarily. – user207421 Apr 23 '19 at 06:05
  • Sorry for the necropost, I just wanted to tell that this solution is still working (CentOS 7, Tomcat 8.0.53, installed from .tar.gz) with the right modifications. Thanks to who posted the solution, you restored my mental sanity! :D – jinzo78 Apr 10 '20 at 07:55
  • It's now likely you'll need to set the https proxy instead -Dhttps.proxyHost=proxy.com -Dhttps.proxyPort=8080 – Stomf May 13 '20 at 15:40
5

While specifying proxy settings, you have to define the proxy server name like below:

"-Dhttp.proxyHost=proxy.example.com"
Sully
  • 14,672
  • 5
  • 54
  • 79
Satheesh
  • 803
  • 1
  • 11
  • 28
3

Create a /bin/setenv.sh (for WINDOWS \bin\setenv.bat):

JAVA_OPTS="-Dhttp.proxySet=true -Dhttp.proxyHost=<proxy_hostname> -Dhttp.proxyPort=<port_number> -Dhttp.nonProxyHosts=<domain_one>|<domain two> $JAVA_OPTS"

NOTE: if you already have setenv.sh/setenv.bat, you can add a line of above command. Tomcat startup script automatically runs setenv script before starting a tomcat instance.

Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
3

You can implement HTTP proxy, HTTPS proxy and HTTP/HTTPS non-proxy hosts also in Tomcat. You need to update two files i.e, bin/Catalina.sh and conf/catalina.properties.

user207421
  • 305,947
  • 44
  • 307
  • 483
Siva Kumar Reddy G
  • 1,274
  • 3
  • 18
  • 32
2

No, Tomcat won't automatically use the system proxy settings.

I suggest you look into the facilities provided by java.net.Proxy. This allows you to dynamically specifiy a proxy at runtime. The system properties work but they are only read once, and if Tomcat has already used an HttpURLConnection for its own purposes prior to you setting them that's the end of that: the setting has no effect.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • So in other words,it is best to do it in java. Also HttpURLConnection if already used in Tomcat, Is there no way of overriding it?Would java.net.Proxy override them? – icedek Sep 07 '12 at 14:46
  • @icedek When you look up the Javadoc, as suggested, your questions will be answered. – user207421 Sep 08 '12 at 03:17
  • As said by Nicolae Petridan, doing this in the code can be ugly to maintain. Either use java.net.Proxy or the system properties, which do work properly: it depends on your use case (permanent proxy because your server is behing a company proxy, or specific proxy calls which vary at every HTTP request). Icedek's problem was likely caused by another JVM being used and settings ignored. See also : http://stackoverflow.com/questions/1432961/how-do-i-make-httpurlconnection-use-a-proxy – spiritoo Feb 05 '14 at 17:02
  • @spirito And as said above, using the system properties can be infeasible. There is no simple answer here. – user207421 Feb 05 '14 at 22:44
1

You can use jProxyLoader library. Using this lib you can configure Tomcat to use proxy only for connections to specific host. In your case you can configure Tomcat to go via proxy only for connections to host serving the webservice (all the other connections will be handled by Tomcat "normal" way - without proxy).

Complete setup is explained on project website: http://jproxyloader.sourceforge.net/examples/web-application-on-tomcat.html

walkeros
  • 4,736
  • 4
  • 35
  • 47