File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes applet-servlet communication. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "applet-servlet communication." Watch "applet-servlet communication." New topic
Author

applet-servlet communication.

Gauri Deshmukh
Greenhorn

Joined: Dec 25, 2004
Posts: 12
HI,

I am trying to send some data ( a string) from an applet to a servlet. I can see that the data is getting sent to the servlet ( by using print stmts) but the servlet doesn't seem to get the data. I cannot see any prin stmts I have put in the servlet in my logs. Here is my code:

Applet code:


Here is what I am doing in my servlet:


It may be worth mentioning here that my applet has another URL connection opened to another servlet ( the applet gets some data from this servlet and sends some modified data to another servlet) . The first servlet connection works fine but the second one doesn't seem to get started from the applet.

CAn somebody tell me what I can do to fix this?
Thanks,
G.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

I think we can rule out a bad URL and bad servlet mapping since you are not getting an exception on the client. Since you aren't getting ANY output on the servlet, I'm suspicious that you may be sending a GET rather than a POST (though I think the line servletConnection2.setDoInput(true); sets the request to be POST). Throw a simple doGet in your servlet for a test.
What I'd bet is the problem is that you don't actually do a request on the URL. Your applet writes to it, but it doesn't read from it. HTTP is a request-response protocol. If you don't follow the protocol, I'd not be surprised when it doesn't work.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

A quick glance at the Java Documentation for URLConnection confirms my suspicions:

# The connection object is created by invoking the openConnection method on a URL.
# The setup parameters and general request properties are manipulated.
# The actual connection to the remote object is made, using the connect method.
# The remote object becomes available. The header fields and the contents of the remote object can be accessed.

You are not actually making the request.
Gauri Deshmukh
Greenhorn

Joined: Dec 25, 2004
Posts: 12
HI Joe,

After this post yesterday I started getting an error :
java.lang.IllegalException:Already Connected. I don't understand what tihs means or what could be causing this and why it wasn't there earlier.
Also could you pls explain what you mean by :
"You are not actually making the request"
Sorry I am new to Java ...this may be trivial....but right now it has me stumped.
THanks,
G.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Gauri Deshmukh:

Also could you pls explain what you mean by :
"You are not actually making the request"


You are constructing an HTTP POST request but you are not actually sending the request across the network. The actual request is made when either you invoke connect() explicity or you invoke a method which depends on the connection (i.e. getInputStream()) which will call connect() if not already invoked.
As for your exception, you obviously made code changes but did not update us so we can only guess. I'd say you are invoking a method which must be called before the connection is made (i.e. setRequestProperty()) after the connection is made. Remember, you are working with a network protocol. There's a specific order in which things must occur. See the link I gave you to the documentation for more on what can be called when.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: applet-servlet communication.