aspose file tools*
The moose likes Servlets and the fly likes Applet --> Servlet... PLEASE HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Applet --> Servlet... PLEASE HELP!" Watch "Applet --> Servlet... PLEASE HELP!" New topic
Author

Applet --> Servlet... PLEASE HELP!

Timothy Thompson
Greenhorn

Joined: Jul 05, 2001
Posts: 2
hello all-
before i start i'd like to prematurely thank anyone who responds to the message.
I am running apache1.3.20, tomcat3.2.1, jsdk2.0, jdk1.3.1:
i have been trying to get this to work or some time now and i am very frustrated. i have already scoured the net for a solution and would appreciate any input i could get.
i am trying (unsuccessfully) to send data from a java app to a servlet to be saved in a file on the server. i can successfully invoke the servlet from the browser's location bar and everything runs okay, but when i try to invoke the servlet from the java app, the servlet never get invoked. the code for both the applet and server follows:
********************************************************************************
//FileSaveServlet: receives data from applet and saves to file
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileSaveServlet extends HttpServlet {

public void doPut(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

//open the input stream from applet
DataInputStream inStream = new DataInputStream(req.getInputStream());

//read in the data
int inInt = inStream.read();

//create new dat file and write file
File fout = new File("test.dat");
FileWriter wout = new FileWriter(fout);
wout.write(inInt);
wout.close();
}
}
********************************************************************************
//connTest applet: cpnnects and sends data to applet

//on button clicked...
void cmdConnect_MouseClicked(java.awt.event.MouseEvent event) {
URL thisURL = null;
URLConnection thisConnection = null;

txtOut.appendText("Connect clicked and mouseClicked thrown... \n Attempting to construct URL... \n");
//construct servlet's url
try {
thisURL = new URL("http://127.0.0.0:8080/examples/servlet/HelloServlet");
txtOut.appendText("URL construction successful! \n Attmpting to connect to servlet... \n");
}
catch(MalformedURLException me) {
txtOut.appendText("Exception caught! " + me.toString() + ". \n");
}

//open the connection
try {
thisConnection = thisURL.openConnection();
thisConnection.setDoOutput(true);
thisConnection.setDoInput(true);
thisConnection.setUseCaches(false);
thisConnection.setRequestProperty("Content-type","text/plain");
txtOut.appendText("Connection successful!\n Attempting to send data to Servlet...\n");
}
catch(IOException ie) {
txtOut.appendText("Exception caught! " + ie.toString() + ".\n");
}
// send the data and close the connection
try {
String reqString = URLEncoder.encode("0123456789");
DataOutputStream outStream = new DataOutputStream(thisConnection.getOutputStream());
outStream.writeBytes(reqString);
outStream.flush();
outStream.close();
txtOut.appendText("Data sent successfully!\n");
}
catch(IOException ie) {
txtOut.appendText("Exception Caught! " + ie.toString() + "\n");
}
}
********************************************************************************
again, i am very grateful for any insight you can give me.
thanks, tsquared
johan swanepoel
Greenhorn

Joined: Jul 12, 2001
Posts: 1
Try the thisConnection.getContent() command.
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
Hi,
To send data from an applet to servlet, the best way is creare a socket connection use that socket for sending data.
Hope you know this mechanism
Thanks
Binu

Sarath Mohan
Prithvi Raj
Greenhorn

Joined: Jul 13, 2001
Posts: 9
The servlet is not invoked because the most important thing u have to understand in client/server communication any request from a client( Applet side) request needs a response) that is why when u r client side makes a request to u r servlet, (whether u send something or not to the servlet) the servlet has to send something back. just open an object outputstream similar to input u have done and output some dummy (say string ) to applet. the applet has to have an inputstream which will read the response. So just read the response that is enough. u r servlet will surely be invoked. dont forget to set content type in the servlet before writing to output steam.

prithvi
Jens Voss
Greenhorn

Joined: Jun 20, 2001
Posts: 14
Hello Timothy,
if you want to send an http request to a servlet from an applet (or, for that matter, from any other Java application), you might want to use the com.oreilly.servlet.HttpMessage class; that is doing a lot of what you programmed in your applet yourself.
Gru�,
Jens


Jens Voß
SCJP 5.0 (97%), SCJD (98%), SCWCD (92%)
Tero Ahonen
Greenhorn

Joined: May 24, 2001
Posts: 26
Try to use JavaRanch's own com.javaranch.common package and expecially classess HTTP and ObjectServlet

-Tero
Wayne Hefner
Greenhorn

Joined: Jul 13, 2001
Posts: 13
I have attached some of our code that does exactly what your looking for.
// server - server where servlet is running
// page - servelet name
// data - additional parameters to servlet
URL url = new URL(server + page + data);
//System.out.println("Opening URL "+url);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("content-type","application/x-www-form-urlencoded");
(connection.getOutputStream());

// parseResults processes the return value in InputStream
int retval = parseResults connection.getInputStream(), dataType, continueOnError);
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi timothy,
i have been trying to do exactly what u were trying to do and faced the same problem,ie,the doPost method of the servlet never executed.if u could solve ur problem then please mail the solution to me at mystiquestranger@yahoo.com..i would greatly appreciate ur help in this regards..btw i tried to email u but it seems ur email addr in the profile is not working bcoz the mail bounced back..
regards
karan
Matt Gregory
Greenhorn

Joined: Jul 24, 2001
Posts: 8
You might try something which I found worked very well.
I invoked an RMI Server instead of a servlet and then connected using the java.rmi.Naming class back over to the server from my applet. This allowed my applet to call methods on the server which invoded the database calls directly from the applet. It was seemlessly client/server and much less static feeling than an applet/servlet based application. I even had dynamic content for the applet being sent back from the servelt, which is always cool.
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi matt,
i have emailed u at the email id provided in ur profile in the ranch..please read it and reply me back..i would appreciate it..
regards
karan
Matt Gregory
Greenhorn

Joined: Jul 24, 2001
Posts: 8
Originally posted by karan, chopra:
hi matt,
i have emailed u at the email id provided in ur profile in the ranch..please read it and reply me back..i would appreciate it..
regards
karan

Aiight here's how it's done:
1.) First, you need to create a class and an interface to that class.
2.)Your interface must extend java.rmi.Remote
3.)Your class must extend java.rmi.server.UnicastRemoteObject and, althought it isn't required, should implement Your interface mentioned above.
4.)You must either create an application which regsiters your server for you, or create a main method in your server to register itself. I personally make the server check for a registry and/or create a registry and then register itself as shown in this code snippet.

5.) Now for the client. In order for the client to connect with the server you need to use somethign like the following code to get the interface to that remote object.

6.) From this point on, you can treat the interface class as though it's a class on the server (which it is) making database calls, filesystem calls, and any other kind of call you wish to add inside the server code. The reason is because the function you are calling in the interface is really being executed by the class on the server, inside the jvm on the server, using the server's classes. (I love RMI )
Matt Gregory
Greenhorn

Joined: Jul 24, 2001
Posts: 8
I just wanted to add that if you weren't aware you must run rmic on your server class. This will generate a server _stub (and if you want to be backwards compatable, a server _skel) class from your compiled classes. These classes are required by the RMI registry to function.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Applet --> Servlet... PLEASE HELP!