Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Applet Servlet Communication

Dear friends,
when i call a Servlet from a Applet . it give IOException .
it showing this message also " Server returned HTTP response code: 405 for
the URL : url of Servlet "
when I call the Servlet in a normal way it works.
please help me
[287 byte] By [thennam] at [2007-11-11 7:39:16]
# 1 Re: Applet Servlet Communication
This method loads a file (named listName) from the server directory "system" containing a list of image file names
and then loads these images. The class used for connecting (SiteConn) is at the end of this reply.

Note: this method executes in a thread.

private void loadImageSequence() throws Exception {
String hostName=applet.getDocumentBase().getHost();
String p=applet.getDocumentBase().getPath();
int ix=p.lastIndexOf("/");
String pth="http://"+hostName+"/"+p.substring(0,ix)+"/system/"+listName;

URL url = new URL(pth);
System.out.println("url: "+url.toString());
SiteConn sc = new SiteConn(url);
String s = sc.getContents().toString();
System.out.println("content: "+s);
String [] ss=s.split("\n");
MediaTracker mt=new MediaTracker(applet);
int n=0;
imgList.clear();
for (int i=0; i<ss.length && this.running; i++) {
url=new URL("http://"+hostName+"/"+p.substring(0,ix)+"/images/"+ss[i]);
Image img=applet.getImage(url);
mt.addImage(img,n++);
imgList.add(img);
}
// prepare for double buffering
memImg=new BufferedImage(applet.getWidth(), applet.getHeight(),BufferedImage.TYPE_INT_RGB);
mt.addImage(memImg,n++);
try {
mt.waitForAll();
} catch (Exception ex) {
msg="Image load fail: "+ex.getMessage();
}
memG=memImg.getGraphics();
repaint();
}

The SiteConn class

/**
Usage example:
try {
URL url=new URL("http://www.whatever.com/servlets/store?text=storeThisTExt");
SiteConn aConnection=new SiteConn(url);
StringBuffer sb=aConnection.getContens();
System.out.println(sb.toString());
} catch (MalFormedURLException mfe) {
System.err.println(mfe.getMessage());
}
*
*/

public class SiteConn {
URL url=null;
public SiteConn(URL url) {
this.url=url;
}
public StringBuffer getContents() throws Exception {
StringBuffer buffer;
String line;
int responseCode;
HttpURLConnection connection;

BufferedReader dataInput;
connection = (HttpURLConnection)url.openConnection();
responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP response code: " +
String.valueOf(responseCode));
}
try {
buffer = new StringBuffer();
InputStream input = connection.getInputStream();
dataInput = new BufferedReader(new InputStreamReader(input));

while ( (line = dataInput.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
//System.out.println("line: " + line);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}
return buffer;
}
public byte [] getByteBuffer () throws Exception {

String line;
int responseCode;
HttpURLConnection connection;

connection = (HttpURLConnection)url.openConnection();
InputStream input=connection.getInputStream();

responseCode = connection.getResponseCode();

if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("HTTP response code: " +
String.valueOf(responseCode));
}

try {
byte [] b=new byte[1420];
BufferedInputStream in=new BufferedInputStream(input);
ByteArrayOutputStream out=new ByteArrayOutputStream();
System.out.println("starting");
int n=in.read(b);
System.out.println("start:"+n);
while (n > 0) {
out.write(b,0,n);
n=in.read(b);
System.out.println("buf:"+n);
}
out.close();
input.close();
return out.toByteArray();
} catch (Exception ex) {
ex.printStackTrace(System.err);
return null;
}

}

}
sjalle at 2007-11-11 22:37:47 >