Jsp/servlet Getting Started tutorial--servlet Getting started with

Source: Internet
Author: User

  now JSP books are some of the direct use of the JSP, and then explain the use of serverlet, there are books on the use of Serverlet, and then explain the use of JSP. I think the second kind of relatively good, as for the reasons we can learn to experience! So today we continue to learn the use of Serverlet!

Okay, cut the crap, start now!

  1. What is a servlet?
    ①servlet is the Java class
    ②servlet is a class that inherits the HttpServlet class
    ③ This server-side operation to process client requests
  2. Introduction to Servlet related packages
    --javax.servlet.*: Store generic servlet classes unrelated to the HTTP protocol;
    --javax.servlet.http.*: In addition to inheriting javax.servlet.*, it also adds functionality related to the HTTP protocol.
    (Note: It is necessary to learn the HTTP protocol, as Web development will involve)
    All servlets must implement the Javax.servlet.Servlet interface (Interface).
    If the servlet program is not related to the HTTP protocol, then the Javax.servlet.GenericServlet class must be inherited;
    If the servlet program is related to the HTTP protocol, then the Javax.servlet.http.HttpServlet class must be inherited.
    --httpservlet: An abstract class was provided to create an HTTP Servlet.
       Public void Doget () method:Used to handleGET requests made by the client
      Public void DoPost () method: used to handlePOST request
    There are several ways to check the API Help file yourself
    -- Interface for the Javax.servlet package:
    ServletConfig Interface:
    used by the servlet container to use the
    ServletContext interface during initialization: Defines the method that the servlet uses to obtain information from its container
    ServletRequest Interface: request information from the server
    Servletresponse interface: Response Client Request
    Filter Interface:
    --javax.servlet class for package:
    ServletInputStream class
    : For reading binary data from client
    Servletoutputstream class : interface for sending binary data to the client
    --javax.servlet.http package:
    HttpServletRequest Interface:
    provide HTTP request Information
    HttpServletResponse Interface: provides HTTP response
  3. Servlet life cycle
    The--servlet life cycle refers to the time that exists and when the servlet instance was created, and when the entire process was destroyed.
    The--servlet life cycle has three methods
      init () method :
      Service () method : Dispatches client requests to the protected method service
      Destroy () method : Called by the servlet container to indicate to a servlet, the servlet is being taken out of servi Ce.
    --servlet stages of the life cycle
    ---- instantiation : servlet container creation servlet instance
    ---- initialization : Calling the init () method
    ---- Service : If there is a request, call the service () method
    ---- Destroy : Call the Destroy () method before destroying the instance
    ---- garbage collection : destroying instances
  4. The basic structure of the servlet
      

    Package cn.dragon.servlet;

    The following is the import of the appropriate 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.HttpServletRe Sponse;

    /**

    * This is the first example of a servlet

    * @author Cn.dragon

    */

    public class Servletdemofirst extends HttpServlet {

    Used to process a GET request sent by a client

    public void doget (HttpServletRequest request, httpservletresponse response)

    Throws Servletexception, IOException {

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

    PrintWriter out = Response.getwriter ();

    OUT.PRINTLN ("Hello! "); To send data to the client using the method of the PrintWriter object

    Out.close ();

    }

    For handling post requests sent by clients

    public void DoPost (HttpServletRequest request, httpservletresponse response)

    Throws Servletexception, IOException {

    Doget (request, response); The purpose of this statement is to call the Doget () method for processing when the client sends a POST request

    }

    }

  5. Deployment of Servlets
      

    The following interception section

    <servlet>
    <description> any </description>
    <display-name> any </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

    ① above two <servlet-name> must be the same
    ②<servlet-class> refers to the corresponding class above. Tip: You can copy it directly from your servlet class to avoid mistakes!
    ③<url-pattern> must be a /servlet and a servlet name. Everyone is going to remember that now.

  6. Servlet Instance Demo

    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 Am the init () Method!") Used for initialization work ");
    }
    Handling GET Requests
    public void doget (HttpServletRequest request, httpservletresponse response)
    Throws Servletexception, IOException {
    System.out.println ("I Am the Doget () Method! Used to process a GET request ");
    Response.setcontenttype ("text/html;charset=gb2312");
    PrintWriter out = Response.getwriter ();
    Out.println ("<HTML>");
    Out.println ("<BODY>");
    Out.println ("This is an example of a servlet");
    Out.println ("</BODY>");
    Out.println ("</HTML>");
    }
    Processing Post Requests
    public void DoPost (HttpServletRequest request, httpservletresponse response)
    Throws Servletexception, IOException {
    Doget (request, response);
    }
    Destroying instances
    public void Destroy () {
    Super.destroy ();
    System.out.println ("I Am the Destroy () Method! Used for the destruction of instances ");
    }
    }

    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>

    </web-app>

Jsp/servlet Getting Started tutorial--servlet Getting started with

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.