• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call servlet do post method from a pojo  RSS feed

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
i have one simple java class representing a service which should call a servlet from another server.How can i invoke that servlet and call its doPost() method?

Thanks
Raj
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what data you want to send to the servlet, and what data you get back ... basically, check out java.net.URL for connecting to the servlet, and then play around time to figure out how to communicate with a web server from Java code.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may find the open source Apache HTTPClient toolkit to be helpful for more complex interactions.

For simple requests you will need the methods in HttpURLConnection to do a POST request - see the java.net package.

Bill
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guido and William for your reply.This community always responds in the quickest and most helpful way.Thats why i like javaranch very much.

regards
Rajvinder
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i have tried the given below code. But my standalone class is not able fetch the data from the server.
My java class is


and my servlet code is



i am getting output as : Bytes available 0
Can anyone tell me where am i doing wrong?

thanks
Raj
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Do NOT use available() call - instead read characters until you get a -1
or use the ServletInputStream readLine() method.

2. Your request is doing a GET not a POST - see the HttpURLConnection methods like I said.

Bill
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William
thanks for your reply.Now my code is working once i removed the is.available() method.Now only problem left is to convert byte array to String in servlet.There may be many ways .Can any body tell me the simplest way to do this?

regards
Raj
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use request.getReader() instead and write it to a java.io.StringWriter; then use that writer's toString() method to get the full string.
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob
Its a good solution. Thanks

regards
Raj
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Is it possible that a simple java class can get user defined java object from a servlet?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!