javascript thread: Not allowed to access documents in another domain using javascript.
Hello Joeri Theelen,
Thank you for this recent reply. I have read the technique, you just told me what you are also doing with the help of a proxy
servlet. But I think i had missed something to inform u with, in the previous mail. Here I should tell u that I CANNOT
change/upload/install anything on the host machines where the forms are hosted. Only include a javascript code in the FORMs is all
that i can do. Ofcourse, it is great technique to route a FORM submission request to TWO different places at the same time one that
is direct to the server and the other which is further directed towards APPLET server from FORM server (according to your
suggestion). But unfortunately I have no chance to apply this in my case. :(
Anyhow I am still looking for the help and also researching on my part. Any suggestions regarding this problem are welcome.
Thanks.
>From: "Joeri Theelen"
>Reply-To: "javascript"
>To: "javascript"
>Subject: [javascript] Re: Not allowed to access documents in another domain using javascript.
>Date: Wed, 20 Nov 2002 06:40:58
>
>Hello Fahad Bin Rahat,
>
>If you are using Servlets/JSP on the server with the form, then you can
>write what I have called a ProxyServlet. When opening the child-window
>with the applet, in stead of calling the URL directly on the other domain,
>you should call the ProxyServlet on the same machine the form is hosted
>and supply an extra POST or GET parameter (I have called this
>parameter "url" but it is configurable) in which you put the real URL (to
>forward the request to). Thus, in stead of
>calling "http://www.myAppletMachine.com/..." it should
>be "http://www.myFormMachine.com/proxy/ProxyServlet?
>url=http://www.myAppletMachine.com/...". I do not like this solution, but
>it works until we come up with something better.
>
>Here is the code for the Servlet:
>---
>package be.gim.utils;
>
>import java.io.*;
>import java.net.*;
>import java.util.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>
>public class ProxyServlet extends HttpServlet
>{
> private String urlKey;
>
> public void init()
> {
> urlKey = getInitParameter("urlKey");
> }
>
> public void doGet(HttpServletRequest request, HttpServletResponse
>response) throws IOException, ServletException
> {
> doRequest(request, response);
> }
>
> public void doPost(HttpServletRequest request, HttpServletResponse
>response) throws IOException, ServletException
> {
> doRequest(request, response);
> }
>
> public void doRequest(HttpServletRequest request,
>HttpServletResponse response) throws IOException, ServletException
> {
> URL url = null;
>
> // Get the request-parameters
> String parameters = "";
> Enumeration parameterNames = request.getParameterNames();
> while (parameterNames.hasMoreElements())
> {
> String parameterName = (String)
>parameterNames.nextElement();
> if (!parameterName.equalsIgnoreCase(urlKey)) //
>Skip the urlKey-parameter
> {
> String[] parameterValues =
>request.getParameterValues(parameterName);
> if (parameterValues != null)
> for (int i = 0; i <
>parameterValues.length; i++)
> parameters += (parameters
>== "" ? "" : "&") + parameterName + "=" + parameterValues[i];
> }
> }
> // String parameters now looks like
>key1=value1&key2=value2&key3=value3&...
>
> HttpURLConnection con = null;
>
> // Open connection and send parameters in a method
>specific way
> if (request.getMethod().equals("GET"))
> {
> // Append parameters to the URL
> url = new URL(request.getParameter(urlKey) + "?" +
>parameters);
> con = (HttpURLConnection) url.openConnection();
> con.setRequestMethod("GET");
> }
> else // POST
> {
> url = new URL(request.getParameter(urlKey));
> con = (HttpURLConnection) url.openConnection();
> con.setRequestMethod("POST");
> con.setDoOutput(true);
> OutputStreamWriter osw = new OutputStreamWriter
>(con.getOutputStream());
> osw.write(parameters);
> osw.flush();
> osw.close();
> }
>
> // Make the connection
> con.connect();
>
> int responseCode = con.getResponseCode();
> String responseMessage = con.getResponseMessage();
>
> InputStream is = con.getErrorStream();
> if (is != null) // Error
> {
> response.sendError(responseCode, responseMessage);
> }
> else // No error
> {
> is = con.getInputStream();
> response.setStatus(responseCode);
> }
>
> PrintWriter out = response.getWriter();
> for (int c; (c = is.read()) != -1; )
> out.write(c);
>
> is.close();
>
> con.disconnect();
>
> return;
> }
>}
>---
>
>Here the content of our web.xml file:
>---
>
>
>> PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
> "http://java.sun.com/dtd/web-app_2_3.dtd">
>
>
>
>
> ProxyServlet
> be.gim.utils.ProxyServlet
>
> urlKey
> url
>
>
>
>
> ProxyServlet
> /ProxyServlet
>
>
>
>---
>---
MSN 8 helps ELIMINATE E-MAIL VIRUSES. Get 2 months FREE*.