The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Appln-Servlet Communication.

Posted by Rajendra kadam on November 27, 2001 at 5:44 PM

Hey,
Here is peice of code that i used in my application to do communication with servlet.
It may be useful to you.

//URL url = new URL(ezPricingURL +"/SAJsp/ezOfflineLoanApplication.jsp?login="+ userFld.getText()+"&data="+ record);
URL url = new URL(ezPricingURL +"/loanApplication?login="+userFld.getText());
URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(con.getOutputStream())));

String actualData = record.substring(0,lastDelimiterIndex + 2);
//System.out.println(actualData);
out.println(actualData);
out.flush();

//con.setDoInput(true);
BufferedReader jspReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String lineFromJSP = jspReader.readLine();

boolean success = false;

//Wait for signal from Servlet.
while(lineFromJSP != null){

if(lineFromJSP.indexOf("UPLOADSUCCESS") != -1){
//System.out.println("record has been uploaded successfully");
//now delete this record from the file and table.
//Get the String Object ie with "W" character to overwrite the complete record.
StringBuffer buffer = new StringBuffer("");
for(int k=0; k < recordLength; ++k){
buffer.append(appendString);
}

loansFile.seek(addressPtr);
loansFile.writeBytes(buffer.toString());

//remove row and corresponding from hashTable
tableModel.removeRow(rowIndex);
hashAddresses.remove(loanId);
--totalRecordsCounter;

//Update the statusLabel to indicate the upload Status.
++uploadCount;
String txt = new String(uploadCount + " of "+ totalCount + " Loan Applications are uploaded.");
updateStatus(txt);

try{
Thread.sleep(200);
}
catch(Exception e){}

//if totalRecordsCounter = 0; it means no more records,
//so disable all buttons and upload menu option.
if(totalRecordsCounter == 0){
changeStateOfButtons(false);
}

success = true;
//break;
}

Hope this will help you.
raju

> People,
> I need to upload a file from a workstation to a server. It should be asynchronous(ie, not based on user interaction).Hence, there is no form (html etc) involved. So i use a java client at the workstation and a servlet. Here is the code snippet.
> //Client
> URL url = new URL("http:m/c:port/servlet/FileuploadServlet");
> URLConnection con = url.openConnection();
> con.setDoInput(true);
> con.setDoOutput(true);
> con.setUseCaches (false);
> con.setRequestProperty("Content-Type","multipart;application/octet-stream");
> System.out.println("before opening output stream");
> ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); //
> System.out.println("after opening output stream");
> out.writeObject("Success!");
> out.flush();

> //Servlet, in a method performTask() which is called by both //doget and dopost

> ObjectInputStream oin = new ObjectInputStream(request.getInputStream()); //Even though the client is opening an output stream , the control never comes to this line
> Object obj = oin.readObject();
> System.out.println("Servlet: " + obj);

> If the reverse happens, ie, servlet opens output stream and writes to client, then it works, but not in this case.
> Any thoughts, anyone? It's very urgent.

> Raging bull






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us