javaWeb-Servlet(一)

One: What is Servlet :

  Servlet javaWeb is one of the three components, it is the dynamic resources. Action is the Servlet to process the request, the server sends a request to the Servlet receives processed, usually required in Servlet:

    1. Receive request data

    2. Request Processing

    3. Complete response

    For example, a client sent a login request, obtain output registration requests, which should be completed handled by the Servlet! Servlet need to write our own, each Servlet must implement                       javax.servlet.Servlet Interface                            

Two: the way to achieve Servlet    

  There are three ways to achieve Servlet:

  1. To achieve javax.servlet.Servlet Interface

  2. class inherits javax.servlet.GenericServlet

  3. javax.servlet.http.HttpServlet class inheritance

  Normally we would inherit HttpServlet class to complete our Servlet, but also learn Servlet interface to start learning from java.servlet.Servlet.

  

Achieve Servlet.java

/ ** 
 * Check method Servlet interface 
 * / 
public  class aServlet the implements Servlet {
     / * 
     * It is also a life-cycle approach 
     * it will be called before Servlet is destroyed, and it will only be called once! 
     * / 
    @Override 
    public  void the destroy () { 
        System.out.println ( "Destory () ....." ); 
    } 
    
    / * 
     * may be used to obtain configuration information the Servlet 
     * / 
    @Override 
    public the ServletConfig getServletConfig () { 
        System.out.println ( "getServletConfig () ........." );
         return  null ; 
    } 
    
    / * 
     * Get Servlet information
     * / 
    @Override 
    public String getServletInfo () { 
        System.out.println ( "getServletInfo () ......" );
         return  null ; 
    } 
    
    / * 
     * It is the life-cycle approach 
     * it immediately after the Servlet object is created execute and perform only once! (After the call sound) 
     * / 
    @Override 
    public  void the init (the ServletConfig the arg0) throws ServletException { 
        System.out.println ( "the init () ...." ); 
    } 

    / * 
     * It is the lifecycle methods 
     * It is called multiple times! 
     * Each processing requests are calling this method 
     * / 
    @Override 
    public  void service(ServletRequest request, ServletResponse response) 
            throws ServletException, IOException {
        System.out.println("service()......");
    }

 

When you have finished Servlet Servlet classes need to be configured in the web.xml file;

 <servlet>
    <servlet-name>AServlet</servlet-name>
    <servlet-class>cn.itcast.web.servlet.AServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AServlet</servlet-name>
    <url-pattern>/AServlet</url-pattern>
  </servlet-mapping>

 

Three: Servlet life cycle

   The so-called life cycle xxx, xxx that is born, service, and death. Servlet life cycle, too! Servlet life cycle and related methods are:

1. public void init(ServletConfig config) throws ServletException  
2. public abstract void service(ServletRequest req,ServletResponse res) throws ServletException, java.io.IOException
3. public void destroy()
 

 

1.Servlet birth

  Servlet server will be the first visit is to create Servlet, Servlet or created in the server Qidong. Create a Servlet If the server starts, you need to configure web.xm file. That default, Servlet is the first time the server is accessed created.

  Just a Servlet type, the server creates only one instance of an object, for example, in our first visit to http: // localhost: 8080 / when AServlet, through server "/ AServlet" bound to find a name for the Servlet cn.itcast.servlet.AServlet, the server then see whether this type of Servlet has been created, if not created, the server will create an instance of AServlt by reflection. When we visit again

cn.itcast.servlet.AServlet, the server will not be created again AServlet instance, but rather use the last instance created directly.

  After Servlet is created, the server calls the Servlet void init (ServletConfig config) method immediately. Remember, Servlet instance of the object created soon after the call init () method, and a Servlt of

This method is only called once a lifetime. This method will only be called once.

  We can father a number of initialization of the Servlet in init () method!

 

2.Servlet Service

  Each time when the server receives the request, invokes the Servlet service () method to process the request. The server receives a request, it will call seviet () method once used service () method will be called multiple times.

It is precisely because of this, so we need to write code to process the request in service () method!

 

3.Servlet destruction

  Servlet usually only destroyed when the server shuts down. When the server is shut down, the server will go Servlet destroyed, before destroying the Servlet calls destroy Servlet () method, we can Servlet last words

Into destroy () method, for example the release of certain resources like code into destroy () method.

   

@Override
    public void destroy() {
        // NOOP by default
    }

    /**
     * Returns a <code>String</code> containing the value of the named
     * initialization parameter, or <code>null</code> if the parameter does not
     * exist. See {@link ServletConfig#getInitParameter}.
     * <p>
     * This method is supplied for convenience. It gets the value of the named
     * parameter from the servlet's <code>ServletConfig</code> object.
     * 
     * @param name
     *            a <code>String</code> specifying the name of the
     *            initialization parameter
     * @return String a <code>String</code> containing the value of the
     *         initialization parameter
     */
    @Override
    public String getInitParameter(String name) {
        return getServletConfig().getInitParameter(name);
    }

    /**
     * Returns the names of the servlet's initialization parameters as an
     * <code>Enumeration</code> of <code>String</code> objects, or an empty
     * <code>Enumeration</code> if the servlet has no initialization parameters.
     * See {@link ServletConfig#getInitParameterNames}.
     * <p>
     * This method is supplied for convenience. It gets the parameter names from
     * the servlet's <code>ServletConfig</code> object.
     * 
     * @return Enumeration an enumeration of <code>String</code> objects
     *         containing the names of the servlet's initialization parameters
     */
    @Override
    public Enumeration<String> getInitParameterNames() {
        return getServletConfig().getInitParameterNames();
    }

    /**
     * Returns this servlet's {@link ServletConfig} object.
     * 
     * @return ServletConfig the <code>ServletConfig</code> object that
     *         initialized this servlet
     */
    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    /**
     * Returns a reference to the {@link ServletContext} in which this servlet
     * is running. See {@link ServletConfig#getServletContext}.
     * <p>
     * This method is supplied for convenience. It gets the context from the
     * servlet's <code>ServletConfig</code> object.
     * 
     * @return ServletContext the <code>ServletContext</code> object passed to
     *         this servlet by the <code>init</code> method
     */
    @Override
    public ServletContext getServletContext() {
        return getServletConfig().getServletContext();
    }

    /**
     * Returns information about the servlet, such as author, version, and
     * copyright. By default, this method returns an empty string. Override this
     * method to have it return a meaningful value. See
     * {@link Servlet#getServletInfo}.
     * 
     * @return String information about this servlet, by default an empty string
     */
    @Override
    public String getServletInfo() {
        return "";
    }

 

 

 

 

 

 

 

    

   

 

   

 

 

 

 

 

 

    

    

    

   

    

Guess you like

Origin www.cnblogs.com/sacai/p/11618604.html