Originally posted by Marya Doery:
[B]I've never done this; I'd be interested to know why you do this.
<snip>
Hi Marya,
The reason I am writing this servlet is to act as a redirection for incoming requests. The request is made to the servlet with enough information for me to figure out where (which machine, port etc), to re-direct the incoming request to. The reason I am re-directing via a servlet, is because for certain requests, I need to do some suthentication to make sure they are allowed to view the data they are requesting. Therefore my servlet examines an incoming request, if it is of a certain type, then it will be re-directed to an authorisation servlet before coming being directed to the intended destination.
With regards to my problem, if a URL is accessed directly (ie without going through my Redirection Servlet), then everything will work fine. Things still don't go quite right when we try to access the same URL through my servlet. Therefore we are getting in a contractor to look at the problem. I have already spent 3 weeks looking at it and have run out of things to try. I'm not even sure if the problem lies in my servlet or in the CGI program.
Thanks for your reply,
Chris
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Originally posted by Chris Mooring:
Hi,
I am having a problem connecting to a URL from within a servlet.
The URL I am trying to connect to is a CGI Program that has some parameters passed as part of the URL (as in a GET) and a few FORM parameters (as in a POST). If I access the URL directly through a browser, everything works perfectly every time. However, when I try to access the URL from a servlet, it doesn't work the first time. Then when I hit the browsers refresh button, it runs the same code, but this time it works !
The CGI program is called using a POST method.
Here is what I am doing in my servlet ;
- Create a URL object with the parameter I got from the form
- Create a URLConnection object by opening a connection on my URL object
- setDoOutput(true) and setDoInput(true) on my URLConnection object
- Create a DataOutputStream (dos) to my URLconnection
- While my requestParams.hasMoreElements(), dos.writeBytes
(URLEncoder.encode(Param)) + "=" + URLEncoder.encode(req.getParameter(Param))
- I then flush and close the DataOutPutStream (dos)
- I then connect to the input stream of the URLConnection object and recieve the HTML output.
- I set res.setContentType("text/html");
- I print the HTML output to the output stream of my servlets
HTTPServletResponse
Does any of this seem incorrect or incomplete ? Why would it work fine in a browser and not in a servlet ? Should I have some sort of delay before connecting to the URL and passing through the form parameters ? Does anyone know exacly how a browser would handle reuests ?
I have been working on this problem for a while now and I am completely stumped.
I would appreciate any help I can get on this one.
Thanks,
Chris