Servlet and Applet Communication Method

Source: Internet
Author: User

The relationship between Servlet and Applet is generally indirect, that is, the page request is sent to the Servlet by the browser. As a response, the Servlet sends the HTML document generated by the result to the browser. In many cases, it is unnecessary to establish a direct connection between Servlet and Applet. However, if you want to display real-time dynamic data, such as chat room windows, news displays, and stock market quotations, it is useful to establish a direct connection between the two. In particular, such communication is essential in more complex distributed Java applications. This complex interaction method between the client Java program and the Server Java program is not enough to use Servlet and Applet APIs. This article introduces and compares four communication methods: passing parameters through the HTML page, using the network functions of the java.net package to establish a direct network connection, remote method call RMI) and CORBA.

1. Passing Applet parameters through HTML pages

Passing parameters through HTML pages is the most common method to establish a connection between Servlet and Applet. Servlet only needs to write the parameters passed to the Applet into the HTML page. This type of communication is unidirectional and is used to implement Servlet control over the Applet.

2. Use the java.net package to establish two-way communication

The second method is to use the network capabilities provided by the java.net package. Taking the connected stream communication method as an example, the operations on the server side are generally:

Create a ServerSocket object to listen to requests sent from the client on the specified port. When a request is received, the accept () method returns a Socket object.

Use the preceding Socket object to create input and output stream objects. Interacts with customers through input and output streams. After the interaction is complete, close the input and output streams and Socket. When the service program ends, disable ServerSocket. The implementation code class is as follows:

 
 
  1. Try {
  2. BooleanFlag=True;
  3. SocketClientSocket=Null;
  4. ServerSocketServerSocket=NewServerSocket (0 );
  5. System. out. println ("Server listen on:" + serverSocket. getLocalPort ());
  6. While (flag ){
  7. ClientSocket=ServerSocket. Accept ();
  8. DataInputStreamIs=NewDataInputStream (new bufferedInputStream (clientSocket
  9. . GetInputStream ()));
  10. PrintStreamOS=NewPrintStream (new bufferedOutputStream (clientSocket. getOut
  11. PutStream ()));
  12. // Process the Applet request
  13. OS. close ();
  14. Is. close ();
  15. ClientSocket. close ();
  16. }
  17. ServerSocket. close ();
  18. } Catch (IOException e ){
  19. System. err. println ("Exception:" + e );
  20. }
  1. Analysis of Servlet Web Applications
  2. Install Servlets and JSP
  3. Running Environment of Servlets and JSP-JRun
  4. Getting started with the Servlets Engine
  5. Introduction to Java Servlets and CGI programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.