Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet not communicating with Servlet, Help Required.

 
Declan Conlon
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what is wrong with my applet. Originally I had an applet that accessed a file and outputted it to a servlet. I then wanted to add a progress bar but the file access and transfer process were to intensive to make the progress workable. I moved the progress bar to a thread and the file access and transfer also. Now the progress bar works but there seems to be a problem with connecting to the servlet. A case of fix one thing brake another. Has anyone any ideas as to why this may be the case. The code is the same as I just pass datainputstream and a dataoutputstream to the thread and reuse the same codefrom there. The file inputs correctly and I have tried the older version of the code and it still works. Thanks for any help and ideas you can give.
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
well if it run before and now doesnt, then there must be a change.
can you post part of the code ?
karl
 
Declan Conlon
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code
+++++++++++++++NEW CODE++++++++++++++++++
public void postData() throws IOException, FileNotFoundException
{
File outfile = new File(globalfilestr);
URL location = new URL(getCodeBase(), "<URL GOES HERE>");
URLConnection out = location.openConnection();
out.setUseCaches(false);
out.setRequestProperty("Content-Type", "application/x-java-serialized-object");
out.setRequestProperty("Filename", globalfilename);
FileInputStream filein = new FileInputStream(outfile);
DataInputStream datain = new DataInputStream(filein);
DataOutputStream dataout = new DataOutputStream(out.getOutputStream());
filethread = new FileBG();
//Setting thread variables
filethread.setFileSize(outfile.length());
filethread.setInputStream(datain);
filethread.setOutputStream(dataout);
filethread.setPgBar(pgbar);
filethread.start();
}
//PLUS THREAD CODE
float quantumfloat = filesize/100.0f;
int quantum = (int)quantumfloat;
long byteswritten = 0;
float percentup = 0;
barthread = new BarBG();
barthread.pgbar = pgbar;
barthread.start();
while (true)
{
byte readbyte = datain.readByte();
dataout.writeByte(readbyte);
byteswritten++;
if((byteswritten % quantum) == 0 | | (byteswritten % filesize) == 0)
{
float raw = (byteswritten/(float)filesize);
percentup = (raw * 100);
System.out.println(readbyte);
barthread.setPercent(raw);
}
}
+++++++++++++++++OLD CODE++++++++++++++++
private void postData() throws IOException
{
File outfile = new File(globalfilestr);
URL location = new URL(getCodeBase(), "<URL GOES HERE>");
URLConnection out = location.openConnection();
out.setUseCaches(false);
out.setRequestProperty("Content-Type", "application/x-java-serialized-object");
out.setRequestProperty("Filename", globalfilename);
msgarea.append("Filename encoded: " + globalfilename + "\n");
FileInputStream filein = new FileInputStream(outfile);
DataInputStream datain = new DataInputStream(filein);
DataOutputStream dataout = new DataOutputStream(out.getOutputStream());
With same code as thread above for reading and writing the data.
Is there some scoping problem with the DataOutputStream reference? As you see I am essentially doing the same thing in both cases. Hope you can shed some light on the matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic