<repo_name>qinlinsen/servlet-indepth<file_sep>/src/main/java/com/timo/servlet/ExtendsGenericServlet.java
package com.timo.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * 这是写Servlet的第二种方法继承GenericServlet
 * @author 秦林森
 */
public class ExtendsGenericServlet extends GenericServlet{
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println(config);
        String servletName = config.getServletName();
        System.out.println("servletName="+servletName);
        Enumeration<String> initParameterNames = config.getInitParameterNames();
        while(initParameterNames.hasMoreElements()){
            String s = initParameterNames.nextElement();
            System.out.println("s="+s);
        }
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("contextPath="+servletRequest.getServletContext().getContextPath());
        Enumeration<String> attributeNames = servletRequest.getAttributeNames();
        while (attributeNames.hasMoreElements()){
            String element = attributeNames.nextElement();
            System.out.println("element="+element);
        }
        servletResponse.setContentType("text/html;charset=utf-8");
        PrintWriter writer = servletResponse.getWriter();
        writer.print("ExtendsGenericServlet");
    }
}
<file_sep>/src/main/java/com/timo/servlet/ImplementServlet.java
package com.timo.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 这是写Servlet的第一种方法实现Servlet接口，所以我的类名取ImplementServlet
 * 从这里可以看出servlet的生命周期（所谓的生命周期就是servlet）
 * @author 秦林森
 */
public class ImplementServlet implements Servlet {
    /**
     * 在服务器tomcat启动时调用该方法，该方法只会被调用一次
     * @param servletConfig
     * @throws ServletException
     */
    public void init(ServletConfig servletConfig) throws ServletException {
        Object renmin = servletConfig.getServletContext().getAttribute("renmin");
        String name = servletConfig.getInitParameter("name");
        ServletContext servletContext = servletConfig.getServletContext();
        System.out.println("name="+name);
        String age = servletConfig.getInitParameter("age");
        System.out.println("age="+age);
        System.out.println("servletContext="+servletContext);
        System.out.println("renmin="+renmin);
        System.out.println("*****************************************************");
        Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
        while (initParameterNames.hasMoreElements()){
            String element = initParameterNames.nextElement();
            String value = servletConfig.getInitParameter(element);
            System.out.println(element+"="+value);
        }
    }

    public ServletConfig getServletConfig() {
        return getServletConfig();
    }

    /**
     * servlet中处理业务逻辑的方法
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        servletResponse.setContentType("text/html;charset=utf-8");
        String contextPath = servletRequest.getServletContext().getContextPath();
        System.out.println("contextPath="+contextPath);
        String renmin = servletRequest.getServletContext().getInitParameter("renmin");
        System.out.println("renmin="+renmin);
        ServletOutputStream outputStream = servletResponse.getOutputStream();
        outputStream.print("hello world");
    }

    public String getServletInfo() {
        return null;
    }

    /**
     * 服务器关闭时调用该方法
     */
    public void destroy() {
        System.out.println("服务器关闭时调用该方法");
    }
}
<file_sep>/src/main/java/com/timo/servlet/ExtendsHttpServlet.java
package com.timo.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 第三种写servlet方式继承HttpServlet
 * @author 秦林森
 */
public class ExtendsHttpServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("第三种写servlet方式");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write(1);
        out.write(3);
        out.print(resp);
    }
}
