The difference between JSP and servlet

Source: Internet
Author: User


The servlet is a server-side program that dynamically generates HTML pages to the client, but this
There are many out.println () in the program, and the Java and HTML languages are confusing. So
The JSP was later launched. In fact, JSP is the servlet, each JSP in the first run of the
Converted to a servlet file and then compiled into a. class to run.
With JSP, the servlet is no longer responsible for generating HTML pages in the MVC pattern, instead serving as
The function of control program logic, controlling the flow between JSP and JavaBean.

The difference between servlet and JSP
* There are no built-in objects in the servlet, the built-in objects in the original JSP must pass through the HttpServletRequest object, or
HttpServletResponse object generation.
* For static HTML tags, the servlet must use the page output stream Kerouac line output.
In short, JSP is a simplification of the servlet, the use of JSP only need to complete the programmer needs to output to the client's content, as in the JSP
How Java scripts are mosaicked into a class is done by a JSP container. The servlet is a complete Java class, and the service of this class
method is used to generate a response to the client.

The nature of the JSP and the servlet is the same, and the JSP is eventually compiled into a servlet

First, the JSP is eventually compiled into a servlet, so the JSP is slower than the servlet.
Second, the JSP is responsible for the front page display, servlet responsible for business control.

1, JSP is evolved from the development of the servlet, JSP in the run time will eventually be translated into a servlet.
2. The stored-value objects that can be used in JSPs can be used in most of the servlet.
3, JSP can achieve the function of servlet can be achieved.
4, in general, we focus on the page display when the use of JSP, in the focus on jump control when using the servlet.

Origin: First Sun proposes a servlet system, which enables Java programmers to develop Web applications based on the B/S architecture, using servlet classes to encapsulate HTTP requests and responses in standard Java classes to implement a variety of Web application scenarios. This step is also one of the most critical steps in Sun's Java EE architecture.
With the development of a large number of B/s architectures, it is found that the writing of servlet classes is very cumbersome, focusing on several issues: first there is a lot of redundant code, which is identical or basic approximation in each servlet class, Next is the development of the servlet programmer rarely proficient in art, resulting in the use of servlet development can not easily achieve a variety of page effects and colorful style, this time sun borrowed from Microsoft's ASP, formally proposed JSP (that is, servlet 1.1), JSP launched, A Java programmer can also add server code to a static page that has been designed by the designer as an ASP programmer, and then automatically parse the JSP file into a servlet class to run through a JSP container. This has greatly improved the efficiency of the work.
People's desire is always endless ~ ~, with the wide application of JSP and the prevalence of various design patterns, people find that JSP also exposes a large number of problems: First, the JSP file with the service-side code to the post-maintenance and page style redesign brought a lot of obstacles, When modifying the page, the artist has to face a lot of the server code that can not understand, the programmer in the change of logic is often confused by complex client code. The cross-workflow makes JSP face a lot of difficulties. This directly led to the introduction of the servlet1.2, Sun in this version of the full advocacy of the concept of MVC, a large number of page tags used to make cross-work a little easier, the compatibility of the service-side label so that the artist can also directly move these labels to get the corresponding effect. However, some problems have been revealed: poor design of the label makes the program error is not detected, immature code can not really make the service-side tag and the client tag as freely as possible without causing a logic error. All these things we need to solve


Development of-servlet
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 a 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.