Servlet Development Detailed

Source: Internet
Author: User

I. Introduction of the Servlet

Servlet is a technology provided by Sun for dynamic Web resources

Sun provides a servlet interface in its API that requires two steps to develop a dynamic Web resource:

    1. Write a Java class to implement the Servlet interface
    2. Deploy the well-developed Java class to the Web server.

Second, the operation of the servlet process

The servlet is called by the Web server after the Web server receives a servlet access request from the client:

    1. The Web server first checks to see if the instance object for the servlet has been loaded and created, and if so, executes step 4th directly, or 2nd step
    2. Mount and create an instance object of the servlet
    3. Invoking the Init () method of the Servlet instance object
    4. Creates a HttpServletRequest object that encapsulates the HTTP request message and a HttpServletResponse object that represents the HTTP response message, and then invokes the Servlet's service method. and the request and response objects are passed in as parameters.
    5. Before the Web application is stopped or restarted, the servlet engine uninstalls the servlet and calls the Servlet's Destory method before uninstalling

Third, servlet run diagram

Iv. Eclipse Development Create servlet

V. Development details of the servlet

    • servlet Access URL Mapping settings

Because the client accesses the resources in the Web server through the URL address, the servlet must map the Serlvet program to a URL address if it wants to be accessed by the outside world, and this is done in the XML file.

  <servlet>
<!-- Servlet-name refers to the servlet name-- <servlet-name>ServletDemo1</servlet-name>
<!-- the full class name of the servlet corresponding class--- <servlet-class >com.atguigu.servlet.ServletDemo1< /servlet-class> </servlet>

<!-- mapping an external access path to a registered servlet-- <servlet-mapping> <servlet-name> servletdemo1</servlet-name> <url-pattern>/servletDemo1</url-pattern> </ Servlet-mapping>
    • servlet access URL using * wildcard mapping
    • The difference between a servlet and a common Java class

A servlet is a Java class that is called by other Java programs and is not run on its own, and it is run entirely by the servlet engine to control and dispatch.

Multiple servlet requests for the client, typically, the server creates only one Servlet instance object, that is, once the Servlet instance object is created, it resides in memory and serves subsequent requests until the Web container exits and the instance object is destroyed.

The init () method is called only once throughout the servlet's lifecycle, and each access request to a servlet causes the servlet engine to invoke the Servlet's service method. For each access request, the Servlet engine creates a new httpservletrequest and a new HttpServletResponse response object, and then passes the two objects as arguments to the service method it invokes. The service method is then called separately according to the request mode ... Method.

    • Default servlet

If the mapping path for a servlet is only one/, the servlet becomes the default servlet for the current web application

  <servlet>    <servlet-name>ServletDemo2</servlet-name>    <servlet-class> com.atguigu.servlet.servletdemo2</servlet-class>  </servlet>  <servlet-mapping >    <servlet-name>ServletDemo2</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>

In the Tomcat installation directory \conf\web.xml file, register a servlet called Org.apache.catalina.servlets.DefaultServlet, which is a default servlet

    <servlet>        <Servlet-name>Default</Servlet-name>        <Servlet-class>Org.apache.catalina.servlets.DefaultServlet</Servlet-class>        <Init-param>            <Param-name>Debug</Param-name>            <Param-value>0</Param-value>        </Init-param>        <Init-param>            <Param-name>Listings</Param-name>            <Param-value>False</Param-value>        </Init-param>        <Load-on-startup>1</Load-on-startup>    </servlet>

When you access a static HTML and picture in Tomcat, you are actually accessing the default servlet.

    • Thread safety issues for Servlets

Servlet Development Detailed

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.