程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Applet與Servlet通訊 (片段源碼)

Applet與Servlet通訊 (片段源碼)

編輯:JAVA編程入門知識

  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);
   }
  }
  

  
  
  
  
  
  
  --------------------------------------------------------------------------------
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved