The key of this architecture is the applet on the client and server side? Communication between ervlet. However, because applet is limited by browser security mode, accessing data and information in applet is not as simple as it seems. In this article, we will explain the limitations faced by developers in the Applet-Servlet structure, and discuss several different communication strategies that can transfer data between Applets and Servlets. If you are already familiar with Applet and Servlet, it will certainly help you read this article. If you are not so familiar, it doesn't matter. Let's make a brief introduction.
Introduction to APPLET and SERVLET
applet
Java applets is actually a Java program running on a web page. It is a Java class inherited from java.applet.applet, which is embedded into an HTML page by reference, just like a picture. The combination of Applet and HTML can build a more powerful dynamic interface. For some Applet that are only used for scrolling text and playing animation, we can use it in enterprise applications to display and process data from resources on the server. For example, Applet can be used to browse and modify records in a database, or to control other applications running on a server.
In addition to its own defined class files, Java applet can also use other classes, whether they exist independently or packaged into a JAR file. Applets and their class files are distributed through standard HTTP requests, so applets can be sent through the firewall of the platform where the webpage data is located. Unless it involves the problem of maintaining the integrity of the application, every time a user visits the web host again, the applet will always refresh automatically and stay at the client for a period of time.
Thanks to the platform independence of Java operating system, Applet can run on any browser with Java Virtual Machine (JVM). Sun's Java plug-in can even use the latest version of the JVM to compile pages without worrying about being limited by the JVM version in your user browser.
Because Applet is an extension of Java platform, existing Java components can be reused when building the interface of web application with Applet. As we can see in the following example, you can use complex Java objects in the components of an Applet to develop work that was originally done by server-side applications. In fact, you can write such Java code, which can be executed either on an Applet or in an application.
Applet have all the functions of traditional Java applications, including using JFC/Swing components of Sun Company. Applets can also be used to make graphics and user interfaces in applications (although some auxiliary windows will be labeled "Warning, Java applet window"). But no matter how similar they are, there are some key differences between applications and Applet. For example, we must take into account that our applet is limited by security mode.
Security constraints of Applet
The Applet code comes from the web host and runs in the browser of the end user's machine. Harmful small programs containing viruses may have destructive effects. In order to prevent such applets, applets are subject to security constraints, that is, applets can only communicate with the host providing this applet, and applets cannot operate the end user's machine. They can't read and write the user's file system, can't execute the above programs, and can't check some sensitive environmental parameters. In fact, there is a way to avoid this restriction, that is, developers can use digital signature technology to mark applets, which will ask users if they can give applets some special treatment. But this is beyond the scope of this article. In addition, Applet cannot establish or accept external socket connections. The so-called foreign, that is, this connection is beyond the host that provides the applet class file (not the host that provides the HTML that references this applet).
Because of this security restriction, we must adopt special strategies in communication with Applet. The only communication way is the network connection before the host provides the applet and the host provides the corresponding HTML.
Servlets
Java servlet is a server-side component, which is similar to CGI in many ways. It can process web requests and return data or HTML. Servlet can access databases, perform calculations, and communicate with components such as Enterprise JavaBean. Unlike CGI programs, Servlet are persistent, that is, they can continue to process requests as long as they are instantiated once (these requests are likely to occur at the same time). Therefore, Servlet are more efficient than CGI.
Servlets run in Servlet engines, usually on web servers or application servers. Both Netscape Enterprise Server 4.0 and Netscape Application Server support the latest version of Java servlet specification. Unlike Applet, Servlet are not restricted by security constraints. Because the Servlet runs entirely on the server, it has the performance allowed by all operating systems.
Servlet can be used to easily establish connections between clients such as Applet and Web browsers and enterprise application cores. For clients, requests for Servlet are no different from other web requests. The client accepts the returned information through URL. We can see that the returned information is not necessarily HTML. In fact, we can send and receive any kind of data through HTML protocol.
Construction method
There are several ways for enterprise applications to construct the use of Applet and Servlet. Let me introduce three different construction methods and compare their advantages and disadvantages.
The first method actually only uses applets instead of Servlet. Although the applet is limited by its security mode, it can still access back-end information such as database, LDAP directory and Enterprise JavaBeans components by using JDBC and RMI protocols. This construction method is shown in figure 1. Although this method seems simple, it is not a good method and will bring many problems. First, this arrangement requires you to embed all access information directly into the Applet code. Database user name, password, server identification, all of which must be included in the Applet code so that end users can collect this information from the class file. In addition, the database or any other system you access must be located on the same server that provides Applet. This means that your server will have to bear a double burden. It is both a web server and a database server. Usually, your back-end resources may be protected by a firewall, but in this case, this is impossible because the Applet running on the client must directly access your machine. Finally, using this method, it is difficult, if not impossible, to use web server clusters.
Figure 1. Construction of two-tier application program
A better way is to encapsulate the transactions that communicate with the back-end resources into Servlet, while Applet are only used to handle the front-end work. In this construction method, as shown in Figure 2, Servlet overcomes the inherent security constraints of applets and is used to control applets to access enterprise information systems and transaction logic. When the Servlet receives the request, it will query the information in the back-end database, perform calculations, handle the acquisition of information representing applets, and operate on the information from applets. A great progress of this method is that Applet/Servlet pairs can be distributed on the cluster of back-end web servers, and all communication with a database exists in the back-end. In addition, the design using Servlet is helpful to modular design, abstract the back end of application to deal with business logic, and improve the flexibility of design.
Figure 2. Three-tier application structure.
If you build applications around Enterprise JavaBeans, Servlet become middleware. EJB components help to separate business logic from Servlet and make it more abstract. In this case, the Applet communicates with its Servlet, which in turn communicates with the EJB component. As we can see in Figure 3. Introducing a hierarchical structure composed of EJB components, Servlet and front-end applet/HTML in application construction can provide us with the greatest flexibility and performance. Although you have to add complexity and cost.
Figure 3. Construction of an application program with multi-layer structure.