aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing - servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing - servlet communication" Watch "Swing - servlet communication" New topic
Author

Swing - servlet communication

satyam vizag narayana
Greenhorn

Joined: Jul 07, 2006
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
Check this. It works for Swing apps just like it does for applets.


Ping & DNS - my free Android networking tools app
kalyan chowdary
Greenhorn

Joined: Jun 22, 2006
Posts: 24
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
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

Joined: Jun 22, 2006
Posts: 24
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
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
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

Joined: Jun 22, 2006
Posts: 24
hi,

I mean servlet is not called
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing - servlet communication