Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about swing & servlet

 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
can we call servlet on swing component as action listener event ?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what you mean by "call". A servlet will run in a different JVM than the Swing app, so it's not just a plain old method call. What you can do is make an HTTP request to a URL that's handled by the servlet, and receive its response. Here's an example of how to do that; note that it's possible to pass parameters in the URL if you need to (of course, you need to do proper exception handling as well).
[ July 26, 2008: Message edited by: Ulf Dittmer ]
 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
But i did not get different JVMs.swing is in one jvm & servlet in oter.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you do have different JVMs. Then you need to use the method I outlined above.
 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to ceeate one swing component & on action event of that component , servlet needs to be invoked.
just as INBOX button in any mail site .
please guide me for this .
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the example code that Ulf gave you?
Merely repeating your requirements isn't going to get you a better answer.

Servlets don't get 'called'.
They respond to HTTP requests.
If you want to invoke a servlet from a Swing application, you'll need to generate an HTTP request from the Swing application.

See Ulf's example.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sunanda kadam:
I want to ceeate one swing component & on action event of that component , servlet needs to be invoked.
just as INBOX button in any mail site .
please guide me for this .


Just to follow up on what other people are saying - A Servlet runs on a Server, and Swing runs on a Client. They are, for all intents and purposes different Machines (even if on the same computer, they are on different Java Virtual Machines) so they can't directly talk.

What you can do is make an HTTP Request from the Swing Client to the Server's Servlet and read the response from the Server. This is the only way to communicate with the Servlet.

Read that link to learn the details of how to do said HTTP Request.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic