[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

passing info in the header to different app

I have a servlet and would like to pass some info in the header to a different app (it's actually a perl application).

I can't use something like this, since this I have a fully qualified url for where I want to send it to - it's not prefixed by "/" since it's not in the same app:

RequestDispatcher rd=request.getRequestDispatcher("/JSPLogin.jsp");
                  rd.forward(request,response);      

What should I do?
0
aturetsky
Asked:
aturetsky
  • 6
  • 4
1 Solution
 
koppchaCommented:
you mean you want to call a perl application from the servlet ?
use getRunTime();
0
 
aturetskyAuthor Commented:
No... I don't want to run a perl application I would like to completely transfer from a java servlet to a perl cgi app.  But I need to pass some info to the PERL app.  Cookies and Gets are not an option.  I would like to add header info or set header info but then when I call sendRedirect("http://someDomain/somePerl.cgi"); the header info does not seem to be there that I added or set.

thanks.
0
 
bloodredsunCommented:
If it's a totally different webapp then the best thing would be to pass information in the request, using request parameters

e.g.
String perlURL = "http://www.myhost.com/cgi-bin/test.pl?param1=hello¶m2=example" ;
response.sendRedirect( perlURL );

RequestDispatcher is used for forwarding requests inside your own webapp, as soon as you go outside of the app, you can't use it (because it's an internal server call).

You could use request headers, but they are a lot more convoluted to call than a simple redirect with request parameters.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bloodredsunCommented:
To use headers you would have to create an HttpUrlConnection inside your servlet and I wouldn't recommend that.
0
 
aturetskyAuthor Commented:
As of right now, adding params in plain view in the url is not an option.

Thanks.
0
 
bloodredsunCommented:
do you want to keep them in your own webapp? Then creating an internal HttpUrlConnection is probably your best best. I can show you how if you like.

I'm wondering about your design/architecture if you have to go outside of your owb webapp. How come?
0
 
aturetskyAuthor Commented:
Yes, I am going outside my own WebApp.  This is just a temporary solution until we can completely convert to Java.  I have some legacy forms in PERL.  Once in PERL they will never need to come back to that java servlet.   BTW, will I need to create a HttpsUrlConnection, assuming I am connecting to a https://?

Thanks.  
0
 
bloodredsunCommented:
Yes the easiest thing to do is create a HttpsUrlConnection. It's that or create a HttpUrlConnection with an over-ridden x509 manager and that's not fun (I had to do this and it was such a pain!).

Somehting along the lines of this but adding your request headers (which if I remember is addRequestProperty())

  StringBuffer sb = new StringBuffer();
          try {
               URL u = new URL( pURL );
               HttpsURLConnection huc = (HttpsURLConnection) u.openConnection();
               huc.setRequestMethod( GET );
               huc.connect();
               BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));

               int b = 0;
               while((b = br.read()) != -1) {
                    sb.append((char)b);
               }
               br.close();
               // disconnect HttpsURLConnection
               huc.disconnect() ;
          }
          catch (IOException e){
               return "Unable to open connection: " + e.getMessage();
          }
          return sb.toString();
0
 
aturetskyAuthor Commented:
All,
    Thanks for all your help.  As it turns out the headers were getting to my browser.  However the sendRedirect was then sends a 302 "temp moved" and forwards to another website WITHOUT my request headers sent on.  So back to the drawing board.

0
 
bloodredsunCommented:
Yes, a response.sendRedirect makes the browser make a new request, which is why you have to use an internal https call to get the response from the external webapp from inside a servlet doGet/doPost method
0
 
bloodredsunCommented:
By the way, you know that you can call HttpsURLConnection.setFollowRedirects(true) ; which should enable the instance to follow redirects. It should work for all 3XX responses.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now