Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes applet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

applet to servlet

ray bond
Ranch Hand

Joined: Oct 11, 2000
Posts: 111
hello there ,
I have an applet , this applet does some downloading of content work at end user side , applet has to download 3 to 4 contents from server , when it successfully download all the content at
end user I want to have report of applets execution activity at
end user side , so this applet will pass notification in string format to given servlet and then that servlet will take this string which has been passed by applet and then servlet writes in file on server's disk , so this is how I can keep track of every time applet is executed successfully or not at end user ,
currently my applet is downloading contents very nice , but
how this applet can pass string to servlet , and how can servlet
capture this string , I know how to write to a file on local disk, it would be very nice of you if you can tell me how to pass string from applet to servlet ,
thank you in advance.
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
Hi Ray,
Applet to servlet communication is dealt very nicely in the Orielly's "Servlet Programming" book by Jason Hunter.
I have the following solution to your problem.
1. Open a Http connection to the Servlet using the URL and URLConnection classes in java.io package.
2. Get an outputstream from the URL connection object and pass it to a high level data stream.
3. Write the desired String to the outputstream.
Overall u are passing the String to the Servlet using The HTTP POST method.
Let me know if you need any more info.
Rajesh
ray bond
Ranch Hand

Joined: Oct 11, 2000
Posts: 111
hi rajesh thanks for your reply,
I know this method ,
URL url = new URL("http://server/servlet/TestServlet");
URLConnection urlcon = url.openConnection();
PrintStream pt = new PrintStream(urlcon.getOutputStream());
pt.print(string);
upto this I have coded in my applet , but after this how can I
send this string to servlet and in servlet what code should I write to obtain this posted string by applet , as this is not parameter that I can code "request.getParameter("string");" in servlet ,
please can you tell me with code how can I get string in servlet
and what should I write in servlet
thanks
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
Okay, make sure that u have called the setDoOutput(true) for the urlconnection object. After that i think the string should be posted to the servlet.
On the servlet side, you can use the getReader()method or the getInputStream() method of the request object. You can then chain the streams apprppriately to read the transmitted string on the server side.
Let me know if you have any more queries. If you are interested i can mail to the code for the same.
Rajesh
ray bond
Ranch Hand

Joined: Oct 11, 2000
Posts: 111
hello there ,
now I am giving code of my applets and servlets here ,
DisplayerApplet.java
import java.io.*;
import java.util.*;
import java.net.*;
import java.applet.*;
public class DisplayerApplet extends Applet
{
public void init()
{
try{
URL upurl = new URL("http://comp7:8080/servlet/DisplayerServlet");

URLConnection urlcon = upurl.openConnection();
urlcon.setUseCaches(false);
urlcon.setDoOutput(true);
PrintStream pt = new PrintStream(urlcon.getOutputStream());
String str = URLEncoder.encode("string to be printed in servlet string to be printed in servlet string to be ...");
pt.print(str);
pt.flush();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

and
DisplayerServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class DisplayerServlet extends HttpServlet
{

String str1 = "c:/p/app.txt";
public void init(ServletConfig config) throws ServletException
{

super.init(config);
}
public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException , IOException
{

response.setContentType("text/html");
PrintWriter pt = response.getWriter();

try{

pt.println("<html><body>");
String str1 = null;
InputStream ins = request.getInputStream();
DataInputStream dis = new DataInputStream(ins);
File f1 = new File(str1);
FileWriter fwr = new FileWriter(f1);
BufferedWriter buff = new BufferedWriter(fwr);
PrintWriter output = new PrintWriter(buff);
pt.println("given string");
while((str1=dis.readLine())!=null)
{
pt.println(str1);
output.println(str1);
System.out.println(str1);
}

output.close();
}

catch(Exception e)
{
System.out.println(e.getMessage());
}
pt.println("</body></html>");
pt.close();
}
public void doPost(HttpServletRequest request , HttpServletResponse response) throws
ServletException , IOException
{
doGet(request , response);
}
}

so , this is the code of applet and servlet , I am using tomcat as servlet container . after starting tomcat , I open DisplayerApplet.html and then in onther browser window I start DisplayerServlet , I am trying to see string which is passed from applet to servlet on servlet , in console it shows null.
whats wrong with my code , what should be added in my code to do this. please can you tell me with some code , thank you.


[This message has been edited by ray bond (edited December 29, 2000).]
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
Hi Ray,
I tried some sample code for the applet and servlet. I am using the ObjectStream method , and not the PrintStream. I will try to look into why the printstream method is falling. I am sending the code for client and servlet :
Client :
/*
<html>
<body>
<applet code="DisplayerApplet.class" width=200 height= 100></applet>
</body>
</html>
*/
import java.io.*;
import java.util.*;
import java.net.*;
import java.applet.*;
public class DisplayerApplet1 extends Applet
{
public void start()
{
try{
URL upurl = new URL("http://localhost:8080/servlet/Hello");
URLConnection urlcon = upurl.openConnection();
urlcon.setDoOutput(true);
urlcon.setUseCaches(false);
PrintStream pt = new PrintStream(urlcon.getOutputStream());
String str = URLEncoder.encode("string to be printed in servlet string to be printed in servlet string to be ...");
pt.print(str);
pt.flush();
urlcon.getInputStream();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Hello extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException
{
try
{
System.out.println("Enterin the servlet now- current one");
ServletInputStream is=req.getInputStream();
ObjectInputStream os=new ObjectInputStream(is);
String str=(String) os.readObject();
System.out.println(str);
res.setContentType("text/html");
ServletOutputStream os1=res.getOutputStream();
ObjectOutputStream os2=new ObjectOutputStream(os1);
os2.writeObject("Response from Servlet");
os1.close();
os2.close();
}
catch(Exception e)
{
}
}
public void doPost(HttpServletRequest request , HttpServletResponse response) throws
ServletException , IOException
{
System.out.println("Here is the post method");
doGet(request , response);
}
}
Its pretty strange to note that the servlet will not be invoked untill a call to urlcon.getInputStream() is made. Let me know if you have any more queries. Let me mention that i am using the Resin servlet engine.
Rajesh
Madhu Sudhan B T
Greenhorn

Joined: Nov 18, 2009
Posts: 21
Hi Ray

I am trying to write an applet to download multiple files from Unix server to user machine(windows).I am able to call the servlet and get the content of the files but unable to download the contents from applet to local machine. However if I run the same code from RAD, the documents gets downloaded but not if i run from JSP where my applet is embedded.

Can you please share some information/code. This would be of great help.

Thanks,
Madhu
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
Please take a look at the date when this topic was last posted to; it'sunlikely anyone of the original participants is still around.


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

Closed as a duplicate.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
jQuery in Action, 2nd edition
 
subject: applet to servlet
 
Similar Threads
Looking for an order-of-magnitude speed-up
Applet upload/download image to/from database via Servlet
applet to servlet
Update applet text runtime
Download file from Servlet via Applet