Solved

Sending a xml file/String to remote servlet.

Posted on 2004-04-14
7
332 Views
Last Modified: 2010-04-01
Hi All
I am running a query and getting the resultset and using that i am formulating a string in xml format.My query here is i need to send that string/xmlfile to a remote servlet.
help me out how to send that to remote servlet(I need to send in post method only)
0
Comment
Question by:krishnamukuntha
  • 4
  • 3
7 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10823001
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20665134.html

Only thing that you might want to change in the code is the method that I am setting to GET... you will have to convert it to POST
0
 
LVL 2

Author Comment

by:krishnamukuntha
ID: 10823177
I want to send it from my servlet to remote
i hope your code is doing on other way...
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10823185
nopes its sending to the remote URL and reading the response back
0
Industry Leaders: 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!

 
LVL 2

Author Comment

by:krishnamukuntha
ID: 10823224
let me rephrase my query:
On Click of a button i am hittin the DB and creating report in xml format by looping the Resultset.
Now i ned to send the created xml file to repmote servlet.
I also need to know how the remort server can get my file in its doPost Method
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 250 total points
ID: 10823509
BufferedReader in=null;
                        
//                  String clientUrl = POINTProperties.getProperty("CFWURL");
                  String clientUrl = CFWURL;
                  clientUrl +="&setSystemProperties=AS400ConnectUserIdTREE("+usr+"),AS400ConnectUserPasswordTREE("+pwd+")";
//                  clientUrl +="&setOverrideProperties=AS400ConnectUserId("+usr+"),AS400ConnectUserPassword("+pwd+")";
                  URL cfwUrl = new URL(clientUrl);
                  
                  java.net.HttpURLConnection con = null;
                  //Open a connection to the specified url
                  try
                  {  
                        con =(java.net.HttpURLConnection) cfwUrl.openConnection();
                        con.setRequestProperty("Content-Type","text/xml");
                        con.setDoOutput(true);
                        con.setDoInput(true);
                        con.setRequestMethod("POST");
                        //Post the XML transaction to the system.
                        con.getOutputStream().write(xml.getBytes());//this code will post your XML to
whatever url you have in cfwURL...
                        in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                  }
                  //Catch any errors that may happen is this process.
                  catch(ProtocolException pe)
                  {
                        System.out.println("Received a Protocol Exception in doRequest"+pe);
                        return null;
                  }
                  
                  catch (FileNotFoundException exception)
                  {
                        System.out.println("Exception in PM-doRequest"+exception);
                        InputStream err = con.getErrorStream();
                        in = new BufferedReader(new InputStreamReader(err));
                  }
                  catch(IOException ioe)
                  {
                        System.out.println("Received an Exception in PM-doRequest"+ioe);
                        ioe.printStackTrace();
                        return null;
                  
                  }
            //Try reading the response from the system. catch any errors that may happen in the process.      
                  try
                  {
                  StringBuffer response = new StringBuffer();
                  String line;
      
                  while ((line = in.readLine()) != null)
                        response.append(line);
                  in.close();
                  return response.toString();
                  }
                  catch(IOException ioex)
                  {
                  System.out.println("ioex in PM-doRequest"+ioex);
                  ioex.printStackTrace();
                  return null;
                  }
                  catch(Exception e1)
                  {
                        System.out.println("Error in PM-doRequest"+e1);
                        e1.printStackTrace();
                        return null;
                  }
            }
            catch(Exception e2)
            {
                  return null;
            }
      }


in the remote servlet all you have to do in doPost is
request.getInputStream(); //this stream will have the XML that you have posted
0
 
LVL 2

Author Comment

by:krishnamukuntha
ID: 10823856
thanks kuldeep
whole nation will proud of you :)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10823869
:-) glad to be of help
thanks for the 'A' grade...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read our guide on how to survive being on-call.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question