有没有Applet和Servlet通信的例子

wlfjck 2001-07-18 09:35:57
有没有Applet和Servlet通信的例子
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2002-01-28
  • 打赏
  • 举报
回复
Talk to a CGI/Servlet
From the client point of view, there is no difference talking to CGI or Servlet. There is two ways to send a request to a CGI. The GET method contains encoded parameters in the URL. A typical URL talking to CGI using the GET method would be: new URL("http://www.server.com/cgi-bin/aCGI.pl?name=Real&site=JAVA+HowTo");



Here we calling a script called aCGI.pl (a PERL script) passing the parameters name and site. Parameters are encoded, spaces are changed to "+" and special character to hexadecimal using a 3-letter escape sequence. Each parameter is delimited by the character "&". Habitually the encoding is done through the static method encode of the java.net.URLencoder class. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl?";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI + encoded);



Once the URL is constructed, you call the CGI using the showDocument method. getAppletContext().showDocument(CGIurl);



The CGI will process the result and produce a page to be displayed.
The POST method allows the programmer to manipulate the data received from the CGI. First a connection is made to the CGI, an OutputStream is open to send the parameters (if any). Then InputStream is created to receive the result. String theCGI = "http://www.server.com/cgi-bin/aCGI.pl";
String encoded = "name=" + URLencoder.encode("Real Gagnon");
URL CGIurl = new URL(theCGI);

URLConnection c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);
c.setRequestProperty("content-type","application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(c.getOutputStream());
out.writeBytes(encoded);
out.flush(); out.close();

BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream());

String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}


Netix 2002-01-28
  • 打赏
  • 举报
回复
Applet:
URL u = new URL("http://localhost:8080/examples/servlet/SomeServlet");
URLConnection conn = u.getConnection();
// 在conn上发送请求。
Servlet:
in = requset.getInputStream();
// 读取请求。
out = response.getOutputStream();
// 发送响应。
别忘了close()。
packy_li 2001-07-19
  • 打赏
  • 举报
回复
清华大学出版的<<java服务器>>书上有现成的
htouch 2001-07-18
  • 打赏
  • 举报
回复
先读一下这个帖子吧:
http://www.csdn.net/expert/topic/58/58576.shtm

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧