Applets, Scriptlet, and Servlets

Source: Internet
Author: User
Tags connection pooling html form microsoft iis microsoft c

Applets, Scriptlet and Servlets-youth-Shao's blog-Blog Channel-csdn.net
http://blog.csdn.net/mo_fan_qing_wa/article/details/50016575

Introduction to Small application Appletapplet

An applet is a special Java program that cannot run on its own (because it does not have a main ()) and needs to be embedded in an HTML file to explain execution with a browser or appletviewer. Applets cannot be run directly with Java commands and need to be compiled into bytecode files first

When a user accesses such a Web page, the applet is downloaded to the user's computer for execution, but only if the user is using a Java-enabled web browser. Because the applet is executed on the user's computer, its execution speed is not limited by the network bandwidth or modem access speed, the user can better appreciate the multimedia effect produced by the applet on the webpage.

The implementation of applet applets relies primarily on the applet classes in the Java.applet package. Unlike a typical application, an applet application must be embedded in an HTML page to be interpreted and executed, while an applet can get parameters from a Web page and interact with the Web page.

The HTML file code for the Web page containing the applet must have a pair of tags such as and , and when the Java-enabled web browser encounters this pair of tags, it will download the appropriate applet code and execute the Applet applet on the local computer.

Procedures for the execution of applets

1. Create an applet file. Like a file named Shao.java.

2. Enter Doc. (assuming that the Shao.java is stored in the E-drive applet\src), start-and-run->cmd-> e:->cd Applet\src->javac Shao.java

Javac.exe will compile the Shao.java file into a byte-code file.

3. At this time, E:\applet\src in the new Shao.class file. Then create the 1.html file in the E:\APPLET\SRC.

Where the code in the 1.html file is:

<applet Code=shao.class height=300width=300></applet>

4. Enter Appletviewer 1.html in CMD to see your applet or simply drag this 1.html into the browser to run

The evolution of applets

Java's early days in the web is the only selling point is the applet, for some time very popular applet animation, chat rooms and games, then the browser, including IE, Netscape are included in the JVM. Sun was and Microsoft pushed the applet, and later for some reason, sun company and Microsoft fell out, IE also no longer embedded JVM, but the applet down to the plug-in level, and since then sun no longer put the mind on the desktop design, more factors consider enterprise design, the JRE is also bigger, Desktop users are also reluctant to install this plugin (for example: Install the JRE, download the class of the applet itself, run the other classes required to download, the download process is becoming more and more slow directly caused the applet to lose the user base) (then Microsoft based on that virtual machine technology, the J + +, then J + + No, from Borland dug to Anders Hejlsberg, directly led to the birth of Microsoft C # and the Decline of Delphi). Just at this point, Flash appeared.

You click on a Web page to prompt you to install a few m of ActiveX control, you can accept Flash and Java applets are controlled by a company, although he basically support all browsers, but after all, but also to install a set of runtime. After Ajax technology was introduced, the ability of JavaScript to make apps was re-discovered, and it was the biggest benefit to run well without having to install a separate set of runtimes. (especially the rise of some front-end frameworks such as: JQUERY,EXTJS, etc.) soon Flash was eliminated.

Running applets requires the installation of a Java environment, which is expensive for users. But Flash also need plug-ins, but because of the success of the promotion of flash, basically has a browser installed Flash plugin. HTML5 is completely free of plugins, browser support!

On the Internet, the ActiveX plug-in software is characterized by: the general software requires users to download and then perform the installation, and the ActiveX plugin is when users browse to a specific webpage, ie browser can automatically download and prompt users to install. An installation of the ActiveX plugin is subject to the user's consent and confirmation.

The basic method of running state control for applets

The four basic methods in the Applet class are used to control its running state: Init (), Start (), Stop (), Destroy ()
Init () method

This method is mainly for the normal operation of the applet to do some initialization work. When an applet is called by the system, it is the method that the system calls first. You can usually do this in this method by passing parameters from the Web page to the applet, adding the basic components of the user interface, and so on.
Start () method

The start () method is called automatically after the system finishes calling the Init () method. Also, the system executes the start () method again every time the user leaves the home page that contains the applet and then returns. This means that the start () method can be executed more than once, unlike the Init () method. Therefore, you can put only the code that you wish to execute once in the Init () method. You can start a thread in the start () method, such as continue an animation, sound, and so on.
Stop () method

This method executes when the user leaves the page where the applet is located, so it can be executed more than once. It allows you to stop some of the work that consumes the system resources while the user is not paying attention to the applet to avoid affecting the speed of the system and does not require an artificial call to the method. If the applet does not contain programs such as animations, sounds, and so on, it is often not necessary to implement the method.

Destroy () method

Unlike the object's Finalize () method, Java calls the method when the browser is closed. The applet is embedded in the HTML file, so the Destroty () method does not care when the applet is closed and it executes automatically when the browser is closed. In the Destroy () method, it is generally possible to require the recovery of a non-memory independent resource. (If the browser is closed when the applet is still running, the system will first execute the Stop () method and then the Destroy () method.)

Small Script Program (scriptlet = script + Applet)

Live Connect provides Java and JavaScript interfaces that allow JavaScript to be used in Java applet applets. The steps are as follows:
1) Use the Mayscript attribute of the 〈applet〉 flag in an HTML script to allow Java APPLET applets to access the script;
2) will be Netscape. JavaScript package import Java applets applet;
3) Create a handle to the JavaScript window in the Java applet applet using the GetWindow () method of the Jsobject class;
4) Access the JavaScript object using the GetMember () method of the Jsobject class in the Java applet applet;
5) Call the JavaScript method in the Java applet applet using the Eval () method of the Jsobject class. "This is not a detailed explanation"

Small service Program (Servlet = Server + Applet)

A servlet is a server-side Java application that has platform-and protocol-independent features that can generate dynamic Web pages. It serves as the middle tier of a customer request (a Web browser or other HTTP client program) and a server response (a database or application on an HTTP server). A servlet is a Java application located on the server side of a Web server, unlike a traditional Java application launched from the command line, which is loaded by a Web server that must contain a Java virtual machine that supports Servlets.

A servlet is a small program that runs on a server. This word is created in the context of Java applets, which is a small program that is sent as a separate file to a Web page, which is typically used to run on the server side, resulting in services such as user operations or mapping based on user interaction.

Some programs are required on the server, often the ones that access the database based on user input. These are usually done using a public Gateway Interface (CGI (Common gatewayinterface)) application. However, Java is run on the server, which can be implemented using the Java programming language. On servers with large traffic volumes, the advantage of Javaservlet is that they execute faster than CGI programs. Individual user requests are activated as one thread in a single program without creating a separate process, which means that the overhead of server-side processing requests is significantly reduced.

The main function of a servlet is to interactively browse and modify data to generate dynamic Web content. This process is:

The client sends the request to the server side;

The server sends the request information to Servlet,servlet to generate the response content and pass it to the server. Response content is dynamically generated, usually depending on the client's request server returning the response to the client

The servlet looks like a common Java program. The servlet imports a specific package that belongs to the Java Servletapi. Because it is the object bytecode that can be dynamically loaded from the network, it can be said that the servlet is the same as the applet to the client, but because the servlet is running in server, they do not need a graphical user interface. In this sense, servlets are also known as Facelessobject.

A servlet is a class in the Java programming language that is used to extend the performance of the server, which resides in applications that can be accessed through the request-response programming model. Although the servlet can respond to any type of request, it is usually used only to extend the application of the Web server.

A detailed comparison between Java Servlets and applets-yasi_xi's column-Blog channel-csdn.net
http://blog.csdn.net/yasi_xi/article/details/22071099

Java Servlets are similar to applets:

They are not standalone applications and do not have a main () method.

They are not called by the user or programmer, but by another application (container).

They all have a life cycle that includes the Init () and Destroy () methods.

Java servlet differs from applets:

Applets have a good graphical interface (AWT), along with the browser, running on the client side.

The Servlet does not have a graphical interface and runs on the server side.

Comparison of Java Servlet with CGI (Common Gateway Interface):

Compared to traditional CGI and many other CGI-like technologies, Java servlets are more efficient, easier to use, more powerful, better portable, and less expensive to invest. In the future of technology development, the servlet is likely to replace CGI completely.

Efficient

In traditional CGI, each request starts a new process, and if the CGI program itself executes a short time, the overhead required to start the process is likely to exceed the actual execution time. In the servlet, each request is handled by a lightweight Java thread (rather than a heavyweight operating system process).

In traditional CGI, if there are n concurrent requests for the same CGI program, the Code of the CGI program is repeatedly loaded n times in memory, whereas for a servlet, the request is processed by n threads, requiring only one copy of the Servlet class code. In terms of performance optimization, Servlets also have more options than CGI.

Convenient

Servlets provide a number of utility routines, such as parsing and decoding HTML form data automatically, reading and setting HTTP headers, processing cookies, tracking session state, and so on.

Powerful features

In Servlets, many tasks that are difficult to accomplish with traditional CGI programs can be done easily. For example, a servlet can interact directly with a Web server, while a normal CGI program cannot. Servlets can also share data across programs, making it easy to implement features like database connection pooling.

Good portability

The servlet is written in Java and the Servlet API has a well-established standard. Therefore, a servlet written for iplanet Enterprise server can be ported to Apache, Microsoft IIS, or WebSTAR without any substantial changes. Almost all mainstream servers support servlets either directly or through plugins.

Save Investment

Java Servlets and applets not only have many inexpensive and even free Web servers available for personal or small-scale web sites, but for existing servers, if it does not support Servlets, this part of the functionality is often free (or requires minimal investment).

Comparison of Java Servlet and JSP (JavaServer Pages):

JavaServer Pages (JSP) is a technology that implements common static HTML and Dynamic HTML mixed coding, and JSP does not add any functionality that is inherently not possible with servlets. However, it is more convenient to write static HTML in the JSP without having to use the PRINTLN statement to output each line of HTML code. More importantly, with the separation of content and appearance, tasks of different natures in page authoring can easily be separated: for example, HTML design by the page designer, while leaving room for the servlet programmer to insert dynamic content.

Introduction to Java Servlet API 2.2

The Java Servlet API 2.2 classes and interfaces comprise two Java packages, namely: Javax.servlet and Javax.servlet.http (also including javax.servlet.jsp packages, which are not covered in this article).

The Javax.servlet package provides the servlet interfaces necessary to control the servlet lifecycle, which must be implemented when writing a servlet.

The Javax.servlet.http package provides abstract classes and generic tool classes that are specifically designed to handle HTTP requests derived from the Servlet interface. All Servlet objects implement the Servlet interface, in most cases as Javax.servlet.GenericServlet and javax.servlet.http.HttpServlet that have implemented the Servlet interface Subclass of these two abstract classes to indirectly implement the Servlet interface.

Applets, Scriptlet, and Servlets

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.