Servlet knowledge point (1), servlet knowledge point

Source: Internet
Author: User

Servlet knowledge point (1), servlet knowledge point

Why servlet?

Jsp can be used to develop some websites, but it is only a small website. Because jsp pages are too redundant and hardly well maintained, servlet should be learned to separate display from logic, therefore, we need another technology servlet.

 

There is web1.0 (there is also a java code in the page to control the output) and web2, 0 (completely separated, with js for control ).

 

Servlet is a java program that processes client requests and responds to requests on the server.

 

Sun has developed a set of j2ee standards in the same way as jdbc (database access interface). For server vendors, they must comply with these standards. For development programmers, they must also develop these standards. Therefore, we can cooperate with each other.

Therefore, servlet must comply with this set of standards to run on j2ee.

 

Servlet Interface

GenericServlet: General Servlet

Httpservlet: supports http

Commonservlet

 

Servlet is single-instance, that is, multiple accesses only have one Servlet (So avoid writing global variables)

Strus is multi-threaded, and a request for a new action is more efficient.

 

Servlet lifecycle: initialization-> call-> destruction

 

Servlet Configuration

(Servlet must be placed in the package, not in the default path)

1. inherit from httpservlet. You must override a method, such as doGet () or doPost ();


2. web. xml configuration:

(1) <servlet>

<Servlet-name> </servlet-name>

<Servlet-class> </servlet-class>

</Servlet>

(2) <servlet-mapping>

<Servlet-mapping>


(Note that the Servlet-name must be consistent during configuration. Servle-class must have a package name ).



A general Servlet lifecycle: (also demonstrated in the form of applets)

First time: first construct-> init ()-> service-> doGet ()/doPost ()



Configure the web. xml file to print the call sequence in the background (the first time) for webpage access)



Second: service-"doGet ()/doPost ()()


Therefore, the Servlet is single-threaded, And the thread is not secure, so global variables are not used.

 

 

Destroy: Close the container-"destroy


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.