Forums Register Login

Servlet Not Responding

+Pie Number of slices to send: Send
Hello Friends,
What I actually want to do is to send a file to a servlet through an applet and that servlet will deliver it to another applet. [ Sending file from one user to another in my chat program if user selects, send file button ]. Before doing this I want to write a program where an applets sends a file to a servlet which in turn sends the same file to the applet itself. I incorporated a TextArea in applet which logs every stage of its execution and telling that it has sent file successfully to servlet and there is no response from sertvlet. When I executed servlet which loads a textfile and delivers it to a client, it worked fine but I cannot figure out what is the case with this applet and servlet communication. For the time being inorder to access a file thru an applet I have given permission for applets in Local Intranet to access file in thru settings and its working fine in accesing a file. Below is My Applet and servlet part. Kindly help me in overcoming this problem. It's posing problems to me since two days.
/* Applet Code */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class FileClient extends Applet {
TextArea ta;
public void init() {
ta = new TextArea();
add(ta);
ta.append("--- STARTING ----");
String servletURL = "http://localhost:8080/examples/servlet/FileServer";
URL url;
URLConnection con;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
url = new URL(servletURL);
ta.append(": pening Connection");
con = url.openConnection();
ta.append("::Getting Output Stream");
bos = new BufferedOutputStream(con.getOutputStream());
ta.append(": pening File");
bis = new BufferedInputStream(new FileInputStream("c:\\give.txt"));
byte[] buff = new byte[2048];
int bytesread;
ta.append("::Reading File And Writing To Stream");
while(-1 != (bytesread=bis.read(buff,0,buff.length)) ) {
bos.write(buff,0,bytesread);
}
ta.append("::Finished Reading File And Writing To Stream");
ta.append("--- OVER ----");
}
catch(final Exception e) {
ta.append("::Caught Exception " + e);
}
finally {
try {
if(bis!=null)
bis.close();
if(bos!=null)
bos.close();
}
catch(Exception e) {
ta.append("::Caught Exception During Closing " + e);
}
}
}
}

/* Servlet Code */
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileServer extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletInputStream in = req.getInputStream();
ServletOutputStream out = res.getOutputStream();
res.setContentType("application/octet-stream");
res.setHeader("content-disposition","attachment; filename=give");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread=bis.read(buff,0,buff.length)) ) {
bos.write(buff,0,bytesread);
}
}
catch(final MalformedURLException e) {
System.out.println("Caught Malformed URL Exception");
throw e;
}
catch(final IOException e) {
System.out.println("Caught IO Exception");
throw e;
}
finally {
if(bis!=null)
bis.close();
if(bos!=null)
bos.close();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req,res);
}
}
+Pie Number of slices to send: Send
Hello Friends,
The Above Code Is Working If I could read binary stream from applet and could deliver it to servlet which then simply returns it to the respective applet. Hence the statements such as out.write("Hello Client")where out is an instance of Printwriter pertaining to client is not making respective changes in the client's browser window when an applet is loaded. i.e messages sent by the servlet are not displayed in the browsers window.
what I want to do is to send data from servlet as a downloading content so that browser could popup a saveas dialog box and client could select the desired location and name.
Also I hope that when all this transfer is done thru program , time for transfer willbe long. So, kindly suggest me a way to avoid this delay and to accomplish filetransfer b/n server and its clients.
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1037 times.
Similar Threads
Sending A File From An Applet To A Servlet
Servlet downloads Excel file ok but also attempts Servlet & JSP files !!
How to Return a File using Servlet
Download file from Servlet via Applet
how to download file from server through servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:09:47.