Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet to handle XML Requests and Responses!

Status
Not open for further replies.

FOR111

Programmer
Sep 29, 2005
103
MT
HI All,

I have a problem...i have to design a servlet which handles xml requests and responses. The requests are received via http, they are parsed and a function should handle the request (ie Add User etc) then a response should be created (xml) and sent via the same http request.

Is there anyone who can give me some examples or maybe a way to go so i can get started!? thanks for your help..i'm quite new to java per se

Regards
Nick
 
Something to start you off ...

In your doPost() method :

Code:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	// read the data from the stream which should be your XML
	// and save it somewhere (in memory or to disk)
	DataInputStream is = new DataInputStream(request.getInputStream());
	int inlen = request.getContentLength();
	byte[] xmlRequestData = new byte[inlen];
	is.readFully(xmlRequestData);
	is.close();	
	is.close();
	
	

	// Then parse your XML which is held in memory in 'xmlRequestData'

	// Perform your required action

	// Create the response XML and convert it into a byte[]

	// Write the data
	response.setContentType("text/xml");

	OutputStream os = response.getOutputStream();
	os.write(xmlResponseData);
	os.flush();
	os.close();
	
}


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks sedj...thats already a big start thanks!

What i was after was how i could test, and parse the xml then the equivelent xml will do a certain function for itself!

Thanks again
Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top