Difference between Servlet and JSP: difference between ServletJSP

Source: Internet
Author: User

Difference between Servlet and JSP: difference between ServletJSP
1. Basic Concepts 1.1 Servlet

Servlet is a server-side Java application that has the characteristics of being independent from the platform and protocol and can generate dynamic Web pages. It serves as the intermediate layer between customer requests (Web browsers or other HTTP client programs) and server responses (databases or applications on the HTTP server. Servlet is a Java application on the server inside the Web server. Unlike the traditional Java application started from the command line, Servlet is loaded by the Web server, the Web server must contain a Java virtual machine that supports Servlet.

1.2 JSP

JSP technology uses Java programming language to compile tags and scriptlets like XML to encapsulate the processing logic for generating dynamic web pages. The web page can also access the application logic of resources on the server through tags and scriptlets. JSP separates the Web page logic from the display of the Web page design. It supports Reusable Component-based design, making Web-based application development fast and easy. JSP is a dynamic page technology. Its main purpose is to separate the presentation logic from the Servlet.

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. JSP provides a set of simple tags, which 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, 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. Next, we will give a brief introduction to JSP running and show 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 into the Servlet code source file according to the JSP syntax, and then the Servlet is compiled into a Java executable bytecode (bytecode ), the JSP syntax is simple to load and execute in the general Servlet mode, which 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.

2. Relationships and differences between the two

[1] JSP will be compiled into Servlet during the first run, and will reside in the memory for calling.

[2] JSP is a web development technology, and Servlet is a small program used by the server. When we access a JSP page, the server converts the JSP page into a Servlet applet to get the result, feedback to the browser on the user side.

[3] Servlet is equivalent to a control layer that calls the corresponding JavaBean to process data, and finally returns the result to JSP.

[4] Servlet is mainly used to redirect requests to the corresponding JSP page.

[5] JSP displays pages more, Servlet processes services more, that is, JSP is a page, and Servlet is a method to implement JSP.

[6] Servlet can implement all JSP functions, but it was very difficult for the artist to use Servlet for interface creation. Later, JSP was developed.

[7] Two JSP technology development website modes: JSP + JavaBean; JSP + Servlet + JavaBean (generally in multi-layer applications, JSP is mainly used as the presentation layer, servlet is used as the control layer, because too much code in JSP is not conducive to maintenance, but this is left for Servlet implementation, and a large number of repeated code is written in JavaBean ).

[8] the difference between the two is that the development interface can be directly written in JSP.

For example, write the Table tag in JSP:

[Data]
;

 

Servlet needs to be added: out. println ("

[Data]
").

 

JSP files are compiled into Servlet files after being called by the Application Server (such as Tomcat, Resin, Weblogic, and Websphere. That is to say, the Servlet File is displayed on the webpage. The Servlet File generated after the JSP file compilation in Tomcat is placed in the work folder, and the HTML code in the JSP file is out of the Servlet, JSP code is placed in different positions according to the tag.

[9] JAVA code is embedded in JSP, while HTML code is embedded in Servlet.

[10] in a standard MVC Architecture, Servlet, as the Controller, accepts user requests and forwards them to corresponding actions for processing. JSP, as a View, is mainly used to generate dynamic pages, use EJB as the Model to implement your business code.

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.