problem in applet to servlet communication

hi every one ,

i have a applet code which sends data to the servlet through URL connection.then that servlet takes this data and creates required file(.doc/.rtf/.xls/.pdf) using set headers.

my applet code is as follows.

public class SampleApplet extends JApplet

{

public void init()

{

try{

URL url = new URL("http://localhost:8080/App1/Servlet1");

URLConnection urlconnection =url.openConnection();

urlconnection.setDoOutput(true);

urlconnection.setUseCaches(false);

urlconnection.connect();

OutputStream os=urlconnection.getOutputStream();

OutputStreamWriter osw=new OutputStreamWriter(os);

BufferedWriter bw=new BufferedWriter(osw);

bw.write("place this data in file");

}

}

}

My servlet class is as follows

public class Servlet1 extends HttpServlet

{

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException

{

PrintWriter out=response.getWriter();

InputStream is=request.getInputStream();

BufferedReader br=new BufferedReader(new InputStreamReader(is));

response.setContentType("application/vnd.ms-excel");

response.setHeader ("Content-Disposition", "attachment; filename=\"Sample.xls\"");

response.setHeader("Cache-Control", "no-cache");

out.write(br.readLine());

}

}

when i call this servlet from html file it works fine creates a xls file and opens the file down load dialog .

but when i call this servlet from the applet the file is not creating and file download dialog is not opening.

can anybody solve my problem?

please help me.

[1738 byte] By [raju_namalaa] at [2007-11-27 5:27:42]
# 1

When you call the servlet from an HTML page the request/response is handled by the browser. The browser gets the response and pops up the file downlaod dialog box and handles saving the file.

When you call the sertvlet from the applet the applet has to handle the request/response. That means that you will have to code getting the file from the response from the servlet and opening a file save dialog box and saving the file to disk. You'll need to use a signed applet to get access t the client file system.

An alternative is that instead of sending the request from the Applet is to have the Applet call a JavaScript function on the HTML page where it is embeded and have the JavaScript function submit a form that makes a call to teh servlet. This way the browser will be doingthe request/response and should handle the file download. For Applet-JavaScript communication do a google search for 'LiveConnect"

http://java.sun.com/products/plugin/1.3/docs/jsobject.html

tolmanka at 2007-7-12 14:49:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...