HTTP proxy servlet ver. 1.6

This servlet lets you transparently pass all the incoming requests to the some predefined host. Servlet simply performs a role of HTTP proxy for your requests.

Normally you can hide the actual location for your servers with this proxy servlet. One interesting usage belongs to Ajax programming. You browser may impose a security restriction on calls to XMLHttpRequest. This restriction prevents a script or application from making a connection to any web server other than your web page originally came from. But what if you are going to obtain XML data from the Net? In our example we will show how you can request web services from Yahoo in your Ajax applications.

How to use proxy servlet:

a) download httpProxyPackage.jar and save it in WEB-INF/lib

b) describe this servlet in web.xml file. Servlet accepts the following initial parameters:
host - describes a host for the redirection
proxyHost - Optional parameter. Describes a proxy host for your internet connection
proxyPort - Optional parameter. Describes a proxy port for your internet connection
rewriteHost - Optional parameter. If this value is true than HTTP header Host will be changed. Default value is false
uri - Optional parameter. If this value is not empty, than proxy will use the request's URI exclude the provided value in the final request. Default value is empty (do not rewrite URI).
encoding - Optional parameter. Describes a target encoding

For example in this case servlet will redirect requests to Yahoo search:
 


<servlet>
  <servlet-name>HttpProxy</servlet-name>
  <servlet-class>com.jsos.httpproxy.HttpProxyServlet</servlet-class>
<init-param>
  <param-name>host</param-name>
  <param-value>http://api.search.yahoo.com/WebSearchService/V1/webSearch</param-value>
</init-param>
</servlet>

c) describe a mapping for this servlet in web.xml file. E.g.:
 


<servlet-mapping>
  <servlet-name>HttpProxy</servlet-name>
  <url-pattern>/servlet/yahoo</url-pattern>
</servlet-mapping>

So in the above-mentioned example any local request like this /servlet/yahoo?appid=Your_Yahoo_ID&query=Coldtags&results=10 (search for the word Coldtags on Yahoo) will be actually redirected (and processed there) to Yahoo site. It is also an example how easily you can use Yahoo search API in your Ajax applications. Your XMLHttpRequest Calls could be addressed to the local servlet and transparently redirected to the actual Yahoo site. And all this chain lets you avoid the violation of browser cross-domain security policy.

Note that you do not need proxy of course for the direct (not Ajax based) request to Yahoo. But HTTP proxy may help again when/if you will need hide that requests or create a facade etc.

Also you can describe the proxy settings for the servlet itself. Parameters are proxyHost and proxyPort. E.g.:
 


<servlet>
  <servlet-name>HttpProxy</servlet-name>
  <servlet-class>com.jsos.httpproxy.HttpProxyServlet</servlet-class>
<init-param>
  <param-name>host</param-name>
  <param-value>http://api.search.yahoo.com/WebSearchService/V1/webSearch</param-value>
</init-param>

<init-param>
  <param-name>proxyHost</param-name>
  <param-value>10.114.7.95</param-value>
</init-param>

<init-param>
  <param-name>proxyPort</param-name>
  <param-value>80</param-value>
</init-param>
</servlet>

   For downloading:

    HTTP proxy package:  httpProxyPackage.jar
 

 ©  Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: