Winter
Winter

Reputation: 1916

How to send a XML file under servlets on tomcat?

I have two web services running on my tomcat lets call them X and Y.

when Y is requested by X in the end Y have a String with a XML inside and now i want to return this string of XML to X.

How can i do this ?

is there anyway to make a special request that X servlet waits for a response ? or do i really need to redirect ?

thx for your time.

Upvotes: 0

Views: 2799

Answers (2)

Steven Feldman
Steven Feldman

Reputation: 841

Since you have a java tag, I will assume you are using java servlets.

X gets a XML, that should go to y? Well the easiest way is to for X to create a request to y and forward the response back to the person who made the request.

You can use the HTTPClient class for that.

so once you send the request from X to Y, X will wait until Y has responded.

If your unsure how to do this checkout the java docs http://download.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html

Make sure u check variable sets and parameters, i pulled this from some of my code

Code:

try {
        URL url = new URL(server);

        HttpURLConnection con;
        con=(HttpURLConnection) url.openConnection();
        con.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setDoInput(true);


        OutputStream out = con.getOutputStream();
        Writer writer = new OutputStreamWriter(out, "UTF-8");

        writer.write(xml);



        writer.flush();
        writer.close();

        InputStream is= con.getInputStream();

//This gets sent to the client
            return set_courses(is);


    } catch (Exception e){
        e.printStackTrace();
        status_message= "Custom 1: "+e.getMessage();
        return false;
    }

Upvotes: 2

Joseph Ottinger
Joseph Ottinger

Reputation: 4951

Why wouldn't you have X just call Y, via URL or commons-http, and block until the response is returned? Then X would have the XML and could do whatever it needed with it.

Upvotes: 0

Related Questions