GeeCON Prague 2014*
The moose likes Applets 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 » Applets
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
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Do any exceptions get generated when you try to run the applet?
One thing that you may consider is in the url use your real ip address instead of localhost. I had a problem with jdbc, and when I used my real ip address instead, everything worked fine.
- Sean
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet --> Servlet... PLEASE HELP!