• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass an XML to a servlet.

 
Marc Taylor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,

I am into a new project and i have to write a servlet which takes input as XML thru HTTPS, parse the XML, perform database operations and need to send the response as XML.

Can anyone help me out in this.
Thanks in advance.
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a typical webservice type of problem where you take an xml input and produce an xml out put. Is it necessary for you to do this via a servlet only. I mean the way I see this problem is you can invoke a webservice from your client ( any java program ) and pass an XML input. Inside the webservice you can perform you database stuff and then the returned value can be sent back to the java client. If you are ok with this I can send you the sample code of calling the webservice if you want.
 
Marc Taylor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai nitin,

Firstly, thanks for ur reply.
Plz send sample code of calling the webservice. that would be great.

and is it not possible to use servlets, bcoz the design here says that they r using servlets inorder to receive the XML thru https.
My doubt is, a servlet has to extend either generic servlet ot Http servlet. and if it want to parse the XML it has to extend a SAX Handler, but anyway servlet cant extend both these classes at a time.
I am not sure whether its correct to use servlets in this case.
Plz let me know if i am right.

Thanks once again.
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the sample test client to call the webservice. You would need the axis jars for this to work. You can download the axis jar from the apache site..it is a freeware.

public class TestClient {

public static void main(String [] args) {
try {
String endpoint = "<put the URL of ur webservice here";

Service service = new Service();
Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName("<the method name which you intend to call");

String ret = (String) call.invoke( new Object[] { <put here the list of ur inputs> } );
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

I think for what you want a webservice is ideal. If you know how to create a webservice then it is fine otherwise just have a look at the below given link.. this tells you how to build a webservice while using a WSAD client.

http://www-106.ibm.com/developerworks/websphere/library/techarticles/0310_flurry/flurry1.html

I hope this is helpful.
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry missed the following import statements at the beginning of the test stub.

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
 
Marc Taylor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks nitin !!

Marc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic