JSP/servlet getting started-servlet getting started

Source: Internet
Author: User

Some of the current JSP books directly describe the use of JSP, and then explain the use of serverlet; some books first describe the use of serverlet, and then explain the use of JSP. I personally think the second one is better. You can learn the reason! So today we will continue to learn how to use serverlet!

Okay, let's talk about it. Start now!

  1. What is Servlet?
    ① Servlet is a Java class
    ② Servlet is a class that inherits the httpservlet class
    ③ This operation is run on the server to process client requests.
  2. Introduction to servlet-related packages
    -- Javax. servlet .* : Stores general Servlet classes unrelated to HTTP;
    -- Javax. servlet. http .* : In addition to inheriting javax. servlet. *, It also adds http-related functions.
    (Note: It is necessary to learn about the HTTP protocol, because Web development will involve it)
    All servlets must implement the javax. servlet. servlet interface ).
    If the Servlet Program It has nothing to do with HTTP, so it must inherit the javax. servlet. genericservlet class;
    If the servlet program is related to the HTTP protocol, it must inherit the javax. servlet. http. httpservlet class.
    -- Httpservlet : Provides an abstract class to create an HTTP servlet.
    Public void doget () method: Used for processing The client sends Get Request
    Public void dopost () method: Used for processing Post request
    there are several ways for you to view the API help file
    -- javax. servlet package interface:
    servletconfig interface
    used by the servlet container during initialization
    servletcontext interface: define the servlet method for obtaining information from its container
    servletrequest interface: request information from the server
    servletresponse interface: responds to client requests
    filter interface:
    -- javax. servlet package class:
    servletinputstream class
    : used to read binary data from the client
    servletoutputstream class : used to send binary data to the client
    -- javax. servlet. HTTP packet interface:
    httpservletrequest interface:
    provides HTTP Request Information
    httpservletresponse interface: provide HTTP Response
  3. Servlet Lifecycle
    -- The servlet Life Cycle refers to the whole process of the Existence time and destruction after the servlet instance is created.
    -- Servlet has three methods in its lifecycle.
    Init () method:
    Service () method: Dispatches client requests to the protectedServiceMethod
    Destroy () method: Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
    -- Each stage of the servlet Lifecycle
    ----Instantiation: Servlet container create servlet instance
    ----Initialization: Call the init () method
    ----Service: If there is a request, call the service () method
    ----Destroy: Call the destroy () method before destroying an instance.
    ----Garbage Collection: Destroy an instance
  4. Basic servlet Structure

    Package CN. Dragon. servlet;

    // Import the corresponding package

    Import java. Io. ioexception;

    Import java. Io. printwriter;

    Import javax. servlet. servletexception;

    Import javax. servlet. http. httpservlet;

    Import javax. servlet. http. httpservletrequest;

    Import javax. servlet. http. httpservletresponse;

    /**

    * This is the first servlet example.

    * @ Author CN. Dragon

    */

    Public class servletdemofirst extends httpservlet {

    // Process get requests sent by the client

    Public void doget (httpservletrequest request, httpservletresponse response)

    Throws servletexception, ioexception {

    Response. setcontenttype ("text/html; charset = gb2312"); // This statement specifies the content format and character encoding used to send to the client.

    Printwriter out = response. getwriter ();

    Out. println ("Hello! "); // Use the printwriter object method to send data to the client

    Out. Close ();

    }

    // Process the POST request sent by the client

    Public void dopost (httpservletrequest request, httpservletresponse response)

    Throws servletexception, ioexception {

    Doget (request, response); // This statement is used to call the doget () method for processing when the client sends a POST request.

    }

    }

  5. Servlet deployment

    The following screenshot
     

    <Servlet>
    <Description> arbitrary </description>
    <Display-Name> arbitrary </display-Name>
    <Servlet-Name>Servletdemofirst</Servlet-Name>
    <Servlet-class>CN. Dragon. servlet. servletdemofirst</Servlet-class>
    </Servlet>

    <Servlet-mapping>
    <Servlet-Name>Servletdemofirst</Servlet-Name>
    <URL-pattern>/Servlet/servletdemofirst</Url-pattern>
    </Servlet-mapping>

     

    Note]

    ① The two above<Servlet-Name>Must be the same
    ② <Servlet-class> next refers to the corresponding class. Tip: You can copy it directly in your servlet class to avoid errors!
    ③ <URL-pattern> must be/ServletAdd the servlet name. Let's record it now.

     

  6. Servlet instance demonstration

    Package CN. Dragon. servlet;

    Import java. Io. ioexception;
    Import java. Io. printwriter;

    Import javax. servlet. servletexception;
    Import javax. servlet. http. httpservlet;
    Import javax. servlet. http. httpservletrequest;
    Import javax. servlet. http. httpservletresponse;

    Public class servletdemosecond extends httpservlet {
    // Initialization
    Public void Init () throws servletexception {
    System. Out. println ("I'm the init () method! Used for initialization ");
    }
    // Process get requests
    Public void doget (httpservletrequest request, httpservletresponse response)
    Throws servletexception, ioexception {
    System. Out. println ("I'm the doget () method! Used to process get requests ");
    Response. setcontenttype ("text/html; charset = gb2312 ");
    Printwriter out = response. getwriter ();
    Out. println ("<HTML> ");
    Out. println ("<body> ");
    Out. println ("this is an example of servlet ");
    Out. println ("</body> ");
    Out. println ("}
    // Process post requests
    Public void dopost (httpservletrequest request, httpservletresponse response)
    Throws servletexception, ioexception {
    Doget (request, response );
    }
    // Destroy the instance
    Public void destroy (){
    Super. Destroy ();
    System. Out. println ("I'm the destroy () method! Used to destroy an instance ");
    }
    }

     

    Web. xml file

    <? XML version = "1.0" encoding = "UTF-8"?>
    <Web-app version = "2.4"
    Xmlns = "http://java.sun.com/xml/ns/j2ee"
    Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
    Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
    Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

    <Servlet>
    <Servlet-Name> servletdemosecond </servlet-Name>
    <Servlet-class> CN. Dragon. servlet. servletdemosecond </servlet-class>
    </Servlet>

    <Servlet-mapping>
    <Servlet-Name> servletdemosecond </servlet-Name>
    <URL-pattern>/servlet/servletdemosecond </url-pattern>
    </Servlet-mapping>

Related Article

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.