Comparison between Servlet and JSP

Source: Internet
Author: User

1.ServletWhat is?

Java provides a complete set of client/server solutions. In this solution, the program can be automatically downloaded to the client and executed. This is the applet. But it is only half of the problem. The other half of the problem is servlet, which can be considered a server-side applet. The servlet is loaded and executed by the Web server, just as the applet is loaded and executed by the browser. The servlet receives a request from a client (through a web server), executes a job, and then returns the result. The basic process for using Servlet is as follows:

· The client initiates a request through HTTP.

· The Web server receives the request and sends it to the servlet. If the servlet has not been loaded, the web server will load it to the Java Virtual Machine and execute it.

· Servlet will receive the HTTP request and perform some processing.

· Servlet will return a response to the web server.

· The Web server sends the response received from the servlet to the client.

Because the servlet is executed on the server, the security issues related to the applet do not need to be implemented. Note that the Web browser does not directly communicate with the servlet. The servlet is loaded and executed by the Web server. Servlets are written in Java, so they are platform-independent at the beginning. In this way, the promise that Java can run (write once, run anywhere) on any platform can also be implemented on the server. Servlet also has some unique advantages that CGI scripts do not possess:

L servlet is persistent. The servlet is loaded only once on the Web server and can maintain services (such as a database connection) between different requests ). In contrast, CGI scripts are transient. Each request to a CGI script causes the Web server to load and execute the script. Once the CGI script stops running, it is cleared from the memory and the result is returned to the client. Every time a CGI script is used, it will cause repeated execution of the program initialization process (such as connecting to the database.

L servlet is irrelevant to the platform. As mentioned above, servlet is written in Java and naturally inherits the platform independence of Java.

L servlet is extensible. Because servlet is written in Java, it has all the advantages of Java. Java is a robust and object-oriented programming language that can be easily expanded to meet your needs. Servlet naturally has these features.

L servlet is safe. The only way to call a servlet from the outside is through the Web server. This provides high-level security protection, especially when your web server is protected by a firewall.

Setvlet can be used on a variety of clients. Since servlets are written in Java, you can easily use them in HTML, just like using an applet.

 

2.JSPAndServlet

JSP is a scripting language that encapsulates the interface of the Java Servlet system, simplifies the use of Java and Servlet, and provides the ability to dynamically execute web pages by extending JSP tags. Despite this, JSP still does not go beyond the scope of Java and Servlet. Not only can Java code be directly written on JSP pages, but JSP runs only after being translated into servlet. JSP is executed on the server and the execution result is output to the client browser. We can say that it is basically irrelevant to the browser. It is different from Javascript. Javascript is executed on the client in the script language and has nothing to do with the server.

So what is JSP? Servlet.

The main difference between JSP and servlet is that JSP provides a set of simple tags that are better integrated with HTML, so that people who do not know servlet can make dynamic web pages. If you are not familiar with the Java language (such as me), you will find JSP development easier. After the JSP is modified, you can see the result immediately without manual compilation. the JSP Engine will do this. However, the servelt lacks the need to compile and restart the servlet engine. However, in JSP, HTML and program code are mixed, but servlet is not like this. Maybe everyone is confused. What is Servlet? Next we will give a brief introduction to JSP running and tell you how to execute a JSP file:
When the Web server (or servlet engine or Application Server) supports the JSP engine, the JSP Engine converts the JSP file to the servlet code source file according to the JSP syntax, then the servlet will be compiled into a Java executable bytecode (bytecode) and loaded and executed in the general servlet mode.

The JSP syntax is simple and can be easily embedded into HTML. It is easy to add dynamic parts to facilitate HTML output. To output HTML missing in servlet, you need to call specific methods. Special processing is also required for characters such as quotation marks, which are added to complicated HTML pages as dynamic parts, it is more difficult than JSP.

Except for the conversion and compilation stages, the difference between JSP and servlet is not big.

Generally, the JSP Engine is built on the servlet engine. It is a servlet. It translates JSP files into Servlet Source Code, calls the Java compiler, and compiles them into servlets. This is also the reason why JSP calls are slower for the first time. After the first compilation, JSP and Servlet are at the same speed. Let's take a look at why they compile at the same speed after the first compilation:

During the entire running process, the JSP engine checks whether the compiled JSP (in servlet form) is newer than the original JSP file. If yes, the JSP engine does not compile. If not, the process of conversion and compilation will be re-executed if the JSP file is newer.

JSP Engine: Generally, JSP engines are based on servlet engines and appear in the form of servlets. At the same time, in the implementation of various free and commercial engines, the servlet engine and the JSP Engine usually appear together. We become the servlet/JSP engine or, in a way, a JSP Engine.

The JSP engine is a system-level entity that provides JSP and Servlet Operation Support and manages its lifecycle.

When the JSP page is requested for the first time, the JSP Engine converts the original JSP file into the Servlet Source Code, calls the Java compiler, compiles the file into a servlet, and runs the file in the servlet engine. When there is another request, the JSP Engine will see whether the JSP compiled by the difference is newer than the original JSP file. If yes, run the servlet. If not, the file has been updated, the conversion and compilation process will be re-executed.

Related Article

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.