aspose file tools*
The moose likes Servlets and the fly likes Servlet Not Responding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Not Responding" Watch "Servlet Not Responding" New topic
Author

Servlet Not Responding

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
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);
}
}
ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Not Responding