Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Swing - servlet communication

 
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
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this. It works for Swing apps just like it does for applets.
 
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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: 43016
76
  • 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: 43016
76
  • 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: 43016
76
  • 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.
    Bookmark Topic Watch Topic
  • New Topic