The difference between JSP and servlet

Source: Internet
Author: User

The full name of 1.JSP is Java Server Pages, the Chinese name is Java Servers page, it is a simplified servlet design, JSP technology is somewhat similar to ASP technology, it is in the traditional Web page HTML (Standard Universal Markup Language subset) file (*.htm,*. HTML), insert the Java program Segment (Scriptlet) and the JSP tag (tag) to form a JSP file with the suffix (*.jsp).

Web applications developed with JSP are cross-platform and can run on Linux and other operating systems. (The essence of JSP is that SERVLET,JVM can only recognize Java classes, not the code of the JSP, the Web container compiles the JSP code into the Java class that the JVM can recognize)


2.jsp is better at displaying pages, and Servlets are better at logic control.


There are no built-in objects in 3.Servlet, the built-in objects in the JSP must be obtained by HttpServletRequest objects, HttpServletResponse objects, and HttpServlet objects.


4.JSP is a simplification of servlets, and using JSP requires only the content that the programmer needs to output to the client, how the Java script in the JSP is mosaicked into a class, and is done by the JSP container.

The servlet is a complete Java class, and the service method of this class is used to generate a response to the client.

when did the JSP page compile into a servlet?

When executing a JSP Web page, it can usually be divided into two periods: the translation period (translation time) and the requested period (request time).
Translation period: The JSP Web page is transferred into a servlet class.
Request period: After the Servlet class executes, the response results to the client.
Two things were done during the translation:
Translation period: The JSP Web page is transferred to the servlet source code. java.
Compile time: The servlet source code. Java is compiled into a servlet class. class.


Contact: JSP is an extension of servlet technology, which is essentially a simple way for servlets. 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. The case for JSP is that Java and HTML can be combined into a file with a. jsp extension. JSPs focus on Views, and Servlets are used primarily for control logic.

Development of Servlets
A servlet is often called a server-side applet for processing and responding to client requests.
The servlet is a special Java class that must inherit HttpServlet. Each servlet can respond to requests from the client.
The servlet provides different methods for responding to client requests.
* Doget: A GET request to respond to a client
* DoPost: A POST request to respond to a client
* DoPut: A put request to respond to a client
* DoDelete: Delete request in response to client
* Service (HttpServletRequest request, [can respond to all types of requests from clients]
HttpServletResponse response)
Throws Servletexception,java.io.ioexception
Other methods
* Init (servletconfig config): The initialization method that is called when the servlet instance is created.
* Destory (): The resource reclamation method that is called automatically when the servlet instance is destroyed.
configuration of the servlet
The compiled servlet source file does not respond to user requests and must be compiled into a class file. The compiled. Class
The file is placed under the web-inf/classes path, and if the servlet has a package, the class file should also be placed under the corresponding package path.
In order for the servlet to respond to user requests, the servlet must also be configured in a Web App. When configuring a servlet, you need to modify
Web. xml file.
Configuring a servlet requires configuring two sections:
* Configure the name of the servlet: corresponds to the <servlet/> element in Web. xml
* Configure the URL of the servlet: corresponds to the <servlet-mapping/> element in Web. xml
The life cycle of-servlet
The servlet is run in a container, and the creation and destruction of its instances are not determined by the programmer, but are controlled by the container.
There are two options for creating a servlet:
* The servlet instance is created when the client requests the corresponding servlet: Most servlets are this servlet
*web when the app starts, create a servlet instance immediately: the Load-on-startup servlet
the life cycle of the servlet
Begin
---> Create instance Completion
---> initialization [init]
---> Respond to client requests [Doget,dopost,service]
---> Destroyed [destroy]--->
End
-Use servlet as Controller
Using the servlet as a presentation layer is too much work, and all HTML tags need to be generated using the page output stream.
So using servlets as a presentation layer has the following three disadvantages:
* Development efficiency is low, all HTML tags need to use the page output stream to complete
* Not conducive to team collaboration, artists can not participate in the development of the Servlet interface
* Program maintainability is poor, even if you modify the title of a button, read the need to re-edit the Java code, and recompile

The schema implementation of MVC using the servlet:
JSP page--servlet program--The servlet forwards new JSP pages based on criteria
-load-on-startup Servlet
The time for servlet instantiation is also when the Web app starts, that is, the Load-on-startup servlet
A servlet that is launched when the app is started, usually a servlet for some background service, or a servlet that intercepts many requests;
This servlet is often used as the underlying servlet, providing important back-office services.
Configuration in Web. xml:
Adding elements to the <servlet/> element
<load-on-startup>1</load-on-startup>
-Access the configuration parameters of the servlet
When configuring a servlet, additional configuration parameters can be added, and by using configuration parameters, better decoupling can be achieved.
Avoid writing all the parameters in the program in hard-coded ways.
To access servlet configuration parameters through an instance of the ServletConfig class, ServletConfig provides the following methods:
Java.lang.String Getinitparameter (java.lang.String name) [Get initialization parameters]

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.