Jsp/servlet Beginner Tutorial--servlet Getting Started with

Source: Internet
Author: User

  today 's 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. Personally feel that another kind of relatively good, as for the reasons we can learn to understand! 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 is performed on the server side to handle client requests
  2. Introduction to Servlet related packages
    --javax.servlet.*: Stores generic servlet classes that are not related to the HTTP protocol.
    --javax.servlet.http.*: In addition to inheriting javax.servlet.*. It also adds features related to the HTTP protocol.


    (Note: It is necessary to learn the HTTP protocol.) Because 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 handleClient-issued GET Request
      Public void DoPost () method: used to handlePOSTRequest
    There are several ways to check the API Help files yourself.
    --The interface of the Javax.servlet package:
    ServletConfig interface:
    used by the servlet container during initialization
    ServletContext Interface: defines a method that the servlet uses to obtain information from its container
    ServletRequest Interface: requesting information from the server
    Servletresponse Interface: Responding to client requests
    Filter Interface:
    Class of--javax.servlet Package:
    ServletInputStream class
    : Used to read binary data from the client
      Servletoutputstream class : Used to send binary data to the client
    --javax.servlet.http the interface of the package:
    HttpServletRequest interface:
    provides 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 : Suppose 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 package for importing the corresponding

    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 a sample of the first servlet

    * @author Cn.dragon

    */

    public class Servletdemofirst extends HttpServlet {

    For handling get requests sent by the 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. "); Send data to client using the PrintWriter object method

    Out.close ();

    }

    For processing post requests sent by the client

    public void DoPost (HttpServletRequest request, httpservletresponse response)

    Throws Servletexception, IOException {

    Doget (request, response); The function of this statement is. Call the Doget () method for processing when the client sends a POST request

    }

    }

  5. Deployment of Servlets
      

    Intercept section below

    <servlet>
    <description> Casual </description>
    <display-name> Casual </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> is followed by the corresponding class. Tip: You can copy it directly from your servlet class to avoid mistakes!
    ③<url-pattern> must be the /servlet and the servlet name. That's what everyone is saying 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 the 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 Beginner 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.