The understanding and difference between servlet and JSP

Source: Internet
Author: User

What is a servlet?
Development of client/server computing. Java provides a complete set of client/server solutions in which programs are automatically downloaded to the client and executed, which is the applet. But it is only half the problem. The other half of the problem is the servlet.
A servlet can be considered a server-side applet. The servlet is loaded and executed by the Web server as if the applet were loaded and executed by the browser. The servlet receives the request from the client (through the Web server), executes some kind of job, and returns the result. The basic process for using Servlets is as follows:
• Client requests via HTTP.
· The Web server receives the request and sends it to the servlet. If the servlet has not yet been loaded, the Web server will load it into the Java virtual machine and execute it.
servlet will receive the HTTP request and perform some sort of processing.
Servlet will return an answer to the Web server.
· The Web server sends an answer received from the servlet to the client.
Because Servlets are executed on the server, there is usually no need to implement the security issues associated with applets. Note that the Web browser does not communicate directly with the servlet, and the servlet is loaded and executed by the Web server.
Servlets are written in Java, so they are platform-independent at first. In this way, the commitment of Java to write once to run on any platform (write Once,run anywhere) can also be implemented on the server. The servlet also has some unique advantages that CGI scripts do not have: (I am not very familiar with CGI, so these features can not be fully realized, this is also excerpted from the Forum, please forgive me)
The servlet is persistent. The servlet only needs to be loaded once by the Web server, and it can maintain services (such as a database connection) between different requests. In contrast, CGI scripts are ephemeral and transient. Every request for a CGI script will cause the Web server to load and execute the script. Once the CGI script finishes running, it is purged from memory and returns the results to the client. Each use of a CGI script results in repeated execution of the program initialization process, such as a connected database.
Servlets are platform-independent. As mentioned earlier, the servlet is written in Java and naturally inherits the platform independence of Java.
The servlet is extensible. Since the servlet is written in Java, it has all the advantages that Java can bring. Java is a robust, object-oriented programming language that can easily be scaled to suit your needs. The servlet naturally has these characteristics as well.
The servlet is secure. The only way to invoke a servlet from outside is through a Web server. This provides a high level of security, especially when your Web server has firewall protection.
Setvlet can be used on a wide variety of client computers. Because Servlets are written in Java, you can easily use them in HTML, just as you would with applets.
So, how did the servlet execute? How to write a servlet, what is the basic architecture of it?
These questions will be presented in a later section.
Two. JSP and Servlet
Now that we have a general understanding of the servlet, let's talk about the relationship between JSP and servlet.
JSP is a scripting language that wraps the interface of a Java servlet system, simplifies the use of Java and Servlets, and provides the ability to dynamically execute a Web page by extending the JSP tag (tag). However, JSP is still not beyond the scope of Java and servlet, not only the JSP page can directly write Java code, and JSP is first translated into a servlet before actually running. The JSP executes on the server and outputs the execution results to the client browser, which we can say is basically browser-independent. It is different from JavaScript, JavaScript is in the client's scripting language, executed on the client, not related to the server. So what is JSP? is the servlet.
The main difference between JSP and servlet is that JSP provides a simple set of tags, and HTML fusion is better, so that people who do not understand the servlet can make dynamic Web pages. For people unfamiliar with the Java language (like me, for example), JSP development is more convenient. JSP changes can immediately see the results, do not need to compile manually, the JSP engine will do the work, and Servelt need to compile, restart the servlet engine and a series of actions. But in JSP, the HTML is mixed with the program code, and the servlet is not. Maybe people are more confused, so what is a servlet? Below we do a simple introduction to the operation of JSP, tell you how to execute a JSP file:
When the Web server (or servlet engine, application Server) supports the JSP engine, the JSP engine converts the JSP file into a servlet code source file according to the JSP syntax, and then the servlet is compiled into Java executable bytecode (bytecode). and loaded into execution in a generic servlet manner.
JSP syntax is simple, can be conveniently embedded in the HTML, it is easy to add dynamic parts, easy to output HTML. The output of HTML in the servlet needs to call a specific method, the quotation marks and other characters to do special processing, add to the complex HTML page as a dynamic part, compared to JSP is more difficult.
With the conversion and compilation phase removed, the difference between the JSP and the servlet is not very large.
The JSP engine is usually architected on top of the servlet engine, itself a servlet, translating the JSP file into the servlet source code, then invoking the Java compiler and compiling it into a servlet. This is the reason that the JSP is slow at the first call, and the JSP is the same speed as the servlet after the first compilation. Let's see why they compile the same speed after the first compilation:
Throughout the run, the JSP engine checks that the compiled JSP (in the form of a servlet) is newer than the original JSP file, and if it is, the JSP engine does not compile, and if it is not, the translation and compilation process is re-executed if the JSP file is relatively new.
In order to have a deep understanding, we look at the JSP running and development environment:
Browsers: Common browsers have IE and Netscape two kinds.
Database: The common database has oracle,sql server,informix,db2,sybase,access,mysql and so on.
Operating systems: Common Windows,linux, and a variety of UNIX systems.
Web server: Common with Iis,apache,netscape Enterprise server and so on.
JSP Engine: The general JSP engine is based on the servlet engine and appears in the form of a servlet. At the same time, in the implementation of various free and commercial engines, the servlet engine and the JSP engine often appear together, and we become servlet/jsp engines, or from some kind of JSP engine.
The JSP engine is a system-level entity that can provide JSP and servlet run support and manage their lifetime.
When the JSP page is first requested, the JSP engine translates the JSP raw file into the servlet source code, then calls the Java compiler, compiles it into a servlet, and executes it in the servlet engine. When there is a request again, the JSP engine meets the difference compiled JSP is newer than the original JSP original file, if so, run the servlet, if not, indicates that the file has been updated, the new conversion and compilation process.
Speaking of this, but also basically the JSP and the servlet relationship is clear, from my sense of view with JSP can be, simple and convenient, but also can and beans very good compatible use, the function is very powerful, why there is a servlet, what is the use? Moreover, its writing is relatively complex. In order to make things clearer, I would like to say a few words about history here, and by the way, what are the benefits of using Servlet,servlet?
Summary of History: (Excerpt from a forum with abridged, rewritten)
Simply put, Sun first developed the servlet, its function is relatively strong, the system design is also very advanced, just, it output HTML statement or use the old CGI way, is a sentence output, so, writing and modifying HTML is very inconvenient.
Later, Sun introduced a mosaic type of ASP-like JSP (the product of servlet development), the JSP tag mosaic into the HTML statement, so that greatly simplifies and facilitates the design and modification of the Web page. The new network language, such as asp,php,jsp, is a mosaic script language.
From the point of view of the network three layer structure, a network project is at least three layers: data layer,business layer, presentation layer. Of course, it can be more complicated. Servlets are very powerful for writing business layers, but it is inconvenient to write presentation layers. JSP is mainly for the convenience of writing presentation layer design. Of course, you can write a business layer. Accustomed to asp,php,cgi friends, often unconsciously presentation layer and business layer mixed together. Put the database processing information into the JSP, in fact, it should be placed in the business layer.
According to Sun's own recommendation, the JSP should only store content related to the presentation layer, that is, only the portion of the output HTML page. All data calculation, data analysis, database connection processing, all belong to the business layer, should be placed in the Java beans. Java BEANS is called through JSP to achieve two-tier integration.
In fact, Microsoft's introduction of DNA technology, simply said, is asp+com/dcom technology. Like Jsp+beans, all presentation layers are completed by ASP and all business layers are completed by com/dcom. Through invocation, the integration is realized. Now Microsoft launched the. NET is also through this concept, all the presentation layer is done by ASP, and the business layer is done by C # or vb.net or vc.net.
Why use these component technologies? Because pure asp/jsp language is very inefficient execution, if a large number of user clicks, the pure script language quickly reached his upper limit, and component technology can greatly improve the function limit, speed up the execution speed.
On the other hand, the pure script language mixes the presentation layer with the business layer, causing the changes to be inconvenient, and the code cannot be reused. If you want to change a place, often involves more than 10 pages of code, the use of component technology is only a reorganization of the pieces.
In summary, the servlet is an early imperfect product, write business layer is very good, write presentation layer is very bad, and two layers mixed, appear very chaotic.
So, launch Jsp+baen, write presentation layer with JSP, write business layer with Baen. Sun's own meaning is to replace the servlet with a JSP in the future.
Looking at the above narrative, you may have a better understanding of the coexistence of JSP and servlet. Can see JSP and Bean after the combination of practicality, powerful performance features, ease of use is not the servlet. So is the servlet replaced? No! In the future development, it still has a huge role. Above is just one aspect of the problem, let's look at the features of the servlet itself.
Because it is written by Java, so the relevant features we will not say, the above has been a detailed introduction, we look at the other:
A servlet is a programming model for developing server-side applications that, if it is just a common Java application, can be written without a servlet, but if you want to provide web-based service capabilities, you must write it in this model. And the servlet must also be allowed to run on top of a servlet-compliant Java Web server or app server. Unless you implement a Web server yourself, but its complexity is relatively high, especially in enterprise applications, the stability and robustness of the system requirements are relatively high, so the servlet model is actually simplified to write a robust server-side application development process. Servlets can be used as an access way to provide Web service capabilities
Now it may be understandable what a servlet is, and what the relationship is between them.

The understanding and difference between servlet and JSP

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.