The difference between JSP and servlet

Source: Internet
Author: User

Basic concepts

Servlet

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.

Jsp

JSP technology uses the Java programming language to write tags and scriptlets of class XML to encapsulate the processing logic that produces dynamic Web pages. Web pages can also access the application logic of resources that exist on the server through tags and scriptlets. JSP separates the Web page logic from the display of the web design, supports reusable component-based design, and makes the development of Web-based applications quick and easy. JSP (JavaServer pages) is a dynamic page technology whose main purpose is to separate the presentation logic from the 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). 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 who are unfamiliar with the Java language, they will find JSP development 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. 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) support JSP engine, JSP engine will follow the syntax of JSP, the JSP file into the servlet code source file, Then the servlet will be compiled into Java executable bytecode (bytecode), and loaded into the normal servlet execution JSP syntax is simple, can be easily 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.

Ii. the connection and difference between the two

The "1" JSP is compiled into a servlet the first time it is run and resides in memory for invocation.

"2" JSP is a Web development technology, Servlet is a server-side application of small programs, when we visit a JSP page, the server will turn this JSP page into a servlet applet run results, feedback to the client browser.

The "3" servlet corresponds to a control layer to invoke the corresponding JavaBean processing data, and finally returns the result to the JSP.

The "4" servlet is primarily used for steering, turning requests to the appropriate JSP pages.

"5" JSP more is the page display, servlet is more processing business, that is, JSP is a page, servlet is the way to implement JSP.

The "6" servlet can implement all the functions of the JSP, but since the artist uses the servlet to make the interface very difficult, the JSP was later developed.

"7" JSP technology development Site two modes: JSP + javabean;jsp + Servlet + JavaBean (generally in multi-tier applications, JSP is mainly used as the presentation layer, and the Servlet is used as the control layer, because too much code in the JSP is not conducive to maintenance, This is left to the servlet, and a lot of repeating code is written in JavaBean.

"8" The difference between the two is that the development interface is JSP directly can be written.

For example, write the table tag in the JSP: <table>[data]</table>;

The servlet needs to join: Out.println ("<table>[data]</table>").

JSP files are compiled into servlet files after they have been called by the application server (for example: Tomcat, Resin, WebLogic, and WebSphere). This means that the servlet file is actually displayed on the Web page. Tomcat the resulting servlet file after compiling the JSP file is placed under the work folder, and the HTML code in the JSP is out of the servlet, and the JSP code is placed in a different location depending on the label.

Java code is embedded in the "9" JSP, and HTML code is embedded in the servlet.

"10" in a standard MVC architecture, the servlet takes a user request as a controller and forwards it to the corresponding action handler, which is primarily used to generate dynamic pages as a view, and EJB implements your business code as a model.

JSPs are essentially servlets, but they are created differently. Servlet is completely Java program code composition good at process control and transaction processing and through Servlet
To generate dynamic Web pages; JSP is made up of HTML code and JSP tags, which makes it easy to write dynamic Web pages
So the servlet is used to control the business process in practical application, and the JSP is used to generate the Dynamic Web page. In the struts framework, the JSP is in the view layer of the MVC design pattern, and the servlet is in the control layer.



JSP is the extension of servlet technology, which is essentially a simple way of servlet. The JSP is compiled with a "class servlet". The main difference between Servlets and JSPs is that

The application logic of the servlet is in the Java file and is completely detached from the HTML in the presentation layer. In the case of JSP, Java and HTML can be combined into a

A file with the extension. jsp. JSP focuses on views, and Servlets are used primarily for control logic

The difference between JSP and servlet

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.