• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Servlet Not Responding

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic