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
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's 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> Parameter uri lets you support RESTful requests. Let us provide an example: Suppose you are describing this parameter so:
<init-param> <param-name>uri</param-name> <param-value>/c/delete*</param-value> </init-param> and your mapping for servlet looks so:
<servlet-mapping> <servlet-name>HttpProxy</servlet-name> <url-pattern>/c/delete/*</url-pattern> </servlet-mapping> then for your original request that looks so: http://original_host/c/delete/1 proxy will cut a part of the request that corresponds uri parameter (/c/delete – everything before *) and use the rest (/1 in this case) for adding to the target request. So the real request finally looks so: http://real_host/1 In the simplest case you can set an intial parameter path to true and translate path info (the extra path information follows the servlet path but precedes the query string and will start with a "/" character) from the original request to the final one. At this version proxy supports the following HTTP (HTTPS) requests: GET
For downloading: HTTP proxy package: httpProxyPackage.jar
See also JSOS - the largest collection of servlets and filters.
|
Also in JSOS:
|