The difference between JSP and servlet

Source: Internet
Author: User

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.
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]

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.