• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Applet --> Servlet... PLEASE HELP!

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the thisConnection.getContent() command.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to use JavaRanch's own com.javaranch.common package and expecially classess HTTP and ObjectServlet
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 [email protected] 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic