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

Swing - servlet communication

 
satyam vizag narayana
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i communicate with servlets from swings. I want to send and recieve the data from the servlets to the swings application using JFrame.

Please specify any resources which will give the info.

If u can please give the code for swing servlet communication.

thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this. It works for Swing apps just like it does for applets.
 
kalyan chowdary
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I know about swing(JApplet) to servlet communication.

But its not working in JFrame,

Why it is i dont know,

Here is the code for JApplet to servlet communication
private URLConnection getServletConnection(String servletName) throws MalformedURLException, IOException
{
URL urlServlet = new URL(getCodeBase(), servletName);
con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
return con;
}


URLConnection con = getServletConnection("db");
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(fields);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
details = (ArrayList) inputFromServlet.readObject();

tell me any one how it is in Frame

Thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's going to work except for the "getCodeBase()", which is an applet method. You're going to need to get the servlet URL some other way -- i.e., letting the user type it in, or from a configuration file or something.

But surely if you tried this, the compiler error message already made this clear enough?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But its not working in JFrame,

What does this mean? Is the code being called? Are any exceptions thrown? Is the server accessed at all? Are there exceptions on the server?

It doesn't matter in which class this is implemented, whether it's a subclass of JFrame or not.
 
kalyan chowdary
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to get the servlet using this this URL(from JFrame) but the servlet is not responding.

URL url=new URL("http://localhost:8080/ItemDetails/dbmodify");
URLConnection con=url.openConnection();

ItemDetails is my application name,
dbmodify is the urlpattern which is written in web.xml for that particular servlet.

Help me,

Thanks in advance
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalyan, you need to provide more detail if you want us to help you: TellTheDetails.

E.g., what does this mean:
the servlet is not responding.

Does the request ever get to the server? Does the request ever get to the servlet? Is any of the code of the servlet run? Are there exception, either on the client or the server?
 
kalyan chowdary
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I mean servlet is not called
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's impossible to determine what might be going wrong if you don't tell us what's happening in detail. Make sure the URL you're accessing is correct (e.g., at first you seem to call parts of it "db", and then later "dbmodify"), and that the code that calls it is actually being run. Good luck.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic