首页 > 学院 > 开发设计 > 正文

Applet与Servlet通讯 (片段源码)

2019-11-17 06:24:34
字体:
来源:转载
供稿:网友

  Applet与Servlet通讯 (源码) 作者:jdeveloper

Applet source (partly)
...



showStatus(textField1.getText());
URL servlet = new URL(this.getDocumentBase(),"servlet/GetParameterServlet");
URLConnection connection = servlet.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
PRintWriter out = new PrintWriter(byteStream, true);
String postData = "Name=" + URLEncoder.encode(textField1.getText());
out.print(postData);
out.flush();
String lengthString = String.valueOf(byteStream.size());
connection.setRequestProperty("Content-Length", lengthString);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byteStream.writeTo(connection.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
textArea1.setText("");
String s;
for(; (s = br.readLine()) != null && s.length() > 0; textArea1.appendText(s + ""));
...

Servlet source




// GetParameterServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class GetParameterServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/Html");

PrintWriter out = response.getWriter();
String title = "Reading Request Parameters";
String name = request.getParameter("Name");
out.println(title);
out.println("Hello: " + name );

}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}







--------------------------------------------------------------------------------

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表