• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I send XML message in POST body

 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to write a test web page with associated servlet (running in Tomcat) to send a test message to a third part servlet (also running in Tomcat).

The third party servlet expects a request in the following format:-



I have written a HTML page that will allow the tester to put in up to 5 parameters (name and value) put a XML message:-



When the form posts to the servlet, all of the parameters are specified in the body, which isnt what the third party servlet wants. So, I placed my own servlet in the path MBProxyTest which could take the request and rebuild it in the correct format and forward it to the third party servlet.



The problem is that I cant work out how to create a new request so that I can pass the correctly formated message to the third party servlet.

Any suggestions?

Alan
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would do it exactly the same way you would do it if your code weren't in a servlet. In other words, servlets have nothing to help you with the task.

If you want to write your own code, you can use a URLConnection object; see Sun's tutorial for more information. But you might find it easier to use Apache's HttpClient package to take care of the HTTP details for you.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 on the use of HttpClient. I'm using it to send RESTful requests to a web service I'm setting up which expects XML. It gives you a fine level of control over the content and format of the request.
 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.

I had thought about creating a new connection but thought that using forward (servlet to servlet) might be more efficient from a connectivity point of view (not having to go through the tcp/ip, http connectiong 'thing').

I am rapidly coming to the conclusion it is not possible to create a new request object 'on the fly' and that the only way would be to create a brand new connection. Is this a correct conclusion?

Alan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the relationship between the sender and receiver? If they're not in the same web application, a forward is out of the question.

If the receiving server is remote, you need to create a new request to it. How could it possibly have anything to do with the current request on a different server?
 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
What's the relationship between the sender and receiver? If they're not in the same web application, a forward is out of the question.

If the receiving server is remote, you need to create a new request to it. How could it possibly have anything to do with the current request on a different server?


Really? I have to admit, I am new to working with servlets (been doing it 3 days now!), but the code that I posted above (the MBProxyTest servlet) calls the DisplayParameters servlet. Both of these servlets run on the same server and in the same instance of Tomcat, but in different contexts ( had to switch this on in Tomcat, by default it was disabled - crossContext=true). They are seperate web applications. It works in that MBProxyTest can successfully forward to DisplayParameters, which displays the parameters that were entered in the HTML form.
[ November 26, 2008: Message edited by: Alan Campbell ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will it always be true that they apps reside in the same instance? A forward, even a cross-context forward cannot span servers.

In any case, it's moot here because you need to generate a new request to satisfy the expected requirements of the web service.
 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Will it always be true that they apps reside in the same instance? A forward, even a cross-context forward cannot span servers.


Yes, this will always be the case.

In any case, it's moot here because you need to generate a new request to satisfy the expected requirements of the web service.


So, does that mean that its not possible to create a new request object?
[ November 26, 2008: Message edited by: Alan Campbell ]
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alan Campbell:
I am rapidly coming to the conclusion it is not possible to create a new request object 'on the fly' and that the only way would be to create a brand new connection. Is this a correct conclusion?
There's been a lot of conversation since you posted this but I'm surprised you haven't realized that the answer is:

YES.
 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
There's been a lot of conversation since you posted this but I'm surprised you haven't realized that the answer is:

YES.


There may have been a lot of conversation but no one actually said that I could or couldnt do it. I just wanted clarification.

Thank you both for your time and patience.

Alan
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also construct the xml using javascript using the values in the form you want to submit. Then use something like XMLHttpRequest to submit you xml to the servlet (the third party servlet).
or as Paul and Bear suggested, submit the request to your servlet as a normal form. Get the form fields, construct the xml using maybe the DOM Api of java, create a new request using say HttpClient and post the xml generated in this servlet to the third party servlet. You do this exactly the same way you do in a stand alone core java application.
 
Alan Campbell
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to get it to work by creating a wrapper around the request object and modifying the values that way. I got the idea from another post in this forum.

I have posted the code below in case anyone else is looking to do something similar (sorry if its a bit of a long post).

This is my HTML page:-



This is my servlet code:-



And this is my wrapper code:-

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic