zoukankan      html  css  js  c++  java
  • servlet和jsp学习指南(一)servlet

    近期接触了些纯servlet与jsp的编程项目,顺便把《servlet和jsp学习指南》这本书拿出来看看,感悟良多。记下随笔,以便会议。

    要编写一个servlet首先要实现Servlet或者继承HttpServlet

    package test.com.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebServlet;
    
    //Servlet类的名称要以Servlet作为后缀
    @WebServlet(name = "MyServlet", urlPatterns = { "/my" })
    public class MyServlet implements Servlet {
    
        private transient ServletConfig servletConfig;
        
        public void destroy() {

        }

        public ServletConfig getServletConfig() {
            return null;
        }

        public String getServletInfo() {
            return "My Servlet";
        }

        public void init(ServletConfig servletConfig) throws ServletException {

            this.servletConfig = servletConfig;
        }

        public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

            String admin = servletConfig.getInitParameter("admin");
            String servletName = servletConfig.getServletName();
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html><head></head>" + "<body>Hello from " + servletName + admin + servletConfig+ "</body></html>");
        } }

    实现Servlet接口则需实现它的五个方法,当第一次访问MyServlet时先调用init(ServletConfig servletConfig)方法,其中的ServletConfig是servlet容器传进来的,可以用它获得ServletContext。

    init()方法供servlet初始化使用,有且只调用一次。一般会利用它将servletConfig传给类属性,或者利用servletConfig获取ServletContext传给类属性。

    之后就用访问到service()方法。service()方法是每次访问都会调用的方法。所以用它来实现业务方法。

    destroy()方法是在容器关闭时调用的方法。

    由此可见servlet的存活时间了,也就是面试中命中率超高的servlet生命周期问题了。

    servlet生命周期

    1,初始化阶段  调用init()方法

    2,响应请求阶段  调用service()方法

    3,终止阶段  调用destroy()方法

    如果是实现Servlet接口,那么就意味着每次都要实现那么多方法,即使你放着空方法看着也很冗余。那么写个通用类实现Servlet供其他功能servlet继承就只需要关注业务实现不用,写无关的方法了。

    这时候出现了抽象类javax.servlet.GenericServlet

    /*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
    package javax.servlet;
    
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.Enumeration;
    
    public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
        private static final long serialVersionUID = 1L;
        private transient ServletConfig config;
    
        public void destroy() {
        }
    
        public String getInitParameter(String name) {
            return getServletConfig().getInitParameter(name);
        }
    
        public Enumeration<String> getInitParameterNames() {
            return getServletConfig().getInitParameterNames();
        }
    
        public ServletConfig getServletConfig() {
            return this.config;
        }
    
        public ServletContext getServletContext() {
            return getServletConfig().getServletContext();
        }
    
        public String getServletInfo() {
            return "";
        }
    
        public void init(ServletConfig config) throws ServletException {
            this.config = config;
            init();
        }
    
        public void init() throws ServletException {
        }
    
        public void log(String msg) {
            getServletContext().log(getServletName() + ": " + msg);
        }
    
        public void log(String message, Throwable t) {
            getServletContext().log(getServletName() + ": " + message, t);
        }
    
        public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)
                throws ServletException, IOException;
    
        public String getServletName() {
            return this.config.getServletName();
        }
    }
    抽象类GenericServlet 实现了Servlet接口的绝大部分方法除了service()方法。
    后来又出现现在我们平时写servlet时常继承的HttpServlet,其实HttpServlet是继承于GenericServlet,并封装了doGet、doPost等我们常用的方法。
    /*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
    package javax.servlet.http;
    
    import java.io.IOException;
    import java.lang.reflect.Method;
    import java.text.MessageFormat;
    import java.util.Enumeration;
    import java.util.ResourceBundle;
    import javax.servlet.DispatcherType;
    import javax.servlet.GenericServlet;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public abstract class HttpServlet extends GenericServlet {
        private static final long serialVersionUID = 1L;
        private static final String METHOD_DELETE = "DELETE";
        private static final String METHOD_HEAD = "HEAD";
        private static final String METHOD_GET = "GET";
        private static final String METHOD_OPTIONS = "OPTIONS";
        private static final String METHOD_POST = "POST";
        private static final String METHOD_PUT = "PUT";
        private static final String METHOD_TRACE = "TRACE";
        private static final String HEADER_IFMODSINCE = "If-Modified-Since";
        private static final String HEADER_LASTMOD = "Last-Modified";
        private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
        private static final ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");
    
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_get_not_supported");
            if (protocol.endsWith("1.1"))
                resp.sendError(405, msg);
            else
                resp.sendError(400, msg);
        }
    
        protected long getLastModified(HttpServletRequest req) {
            return -1L;
        }
    
        protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (DispatcherType.INCLUDE.equals(req.getDispatcherType())) {
                doGet(req, resp);
            } else {
                NoBodyResponse response = new NoBodyResponse(resp);
                doGet(req, response);
                response.setContentLength();
            }
        }
    
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_post_not_supported");
            if (protocol.endsWith("1.1"))
                resp.sendError(405, msg);
            else
                resp.sendError(400, msg);
        }
    
        protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_put_not_supported");
            if (protocol.endsWith("1.1"))
                resp.sendError(405, msg);
            else
                resp.sendError(400, msg);
        }
    
        protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_delete_not_supported");
            if (protocol.endsWith("1.1"))
                resp.sendError(405, msg);
            else
                resp.sendError(400, msg);
        }
    
        private static Method[] getAllDeclaredMethods(Class<?> c) {
            if (c.equals(HttpServlet.class)) {
                return null;
            }
    
            Method[] parentMethods = getAllDeclaredMethods(c.getSuperclass());
            Method[] thisMethods = c.getDeclaredMethods();
    
            if ((parentMethods != null) && (parentMethods.length > 0)) {
                Method[] allMethods = new Method[parentMethods.length + thisMethods.length];
    
                System.arraycopy(parentMethods, 0, allMethods, 0, parentMethods.length);
    
                System.arraycopy(thisMethods, 0, allMethods, parentMethods.length, thisMethods.length);
    
                thisMethods = allMethods;
            }
    
            return thisMethods;
        }
    
        protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Method[] methods = getAllDeclaredMethods(super.getClass());
    
            boolean ALLOW_GET = false;
            boolean ALLOW_HEAD = false;
            boolean ALLOW_POST = false;
            boolean ALLOW_PUT = false;
            boolean ALLOW_DELETE = false;
            boolean ALLOW_TRACE = true;
            boolean ALLOW_OPTIONS = true;
    
            for (int i = 0; i < methods.length; ++i) {
                Method m = methods[i];
    
                if (m.getName().equals("doGet")) {
                    ALLOW_GET = true;
                    ALLOW_HEAD = true;
                }
                if (m.getName().equals("doPost"))
                    ALLOW_POST = true;
                if (m.getName().equals("doPut"))
                    ALLOW_PUT = true;
                if (m.getName().equals("doDelete")) {
                    ALLOW_DELETE = true;
                }
            }
            String allow = null;
            if (ALLOW_GET)
                allow = "GET";
            if (ALLOW_HEAD)
                if (allow == null)
                    allow = "HEAD";
                else
                    allow = new StringBuilder().append(allow).append(", HEAD").toString();
            if (ALLOW_POST)
                if (allow == null)
                    allow = "POST";
                else
                    allow = new StringBuilder().append(allow).append(", POST").toString();
            if (ALLOW_PUT)
                if (allow == null)
                    allow = "PUT";
                else
                    allow = new StringBuilder().append(allow).append(", PUT").toString();
            if (ALLOW_DELETE)
                if (allow == null)
                    allow = "DELETE";
                else
                    allow = new StringBuilder().append(allow).append(", DELETE").toString();
            if (ALLOW_TRACE)
                if (allow == null)
                    allow = "TRACE";
                else
                    allow = new StringBuilder().append(allow).append(", TRACE").toString();
            if (ALLOW_OPTIONS) {
                if (allow == null)
                    allow = "OPTIONS";
                else
                    allow = new StringBuilder().append(allow).append(", OPTIONS").toString();
            }
            resp.setHeader("Allow", allow);
        }
    
        protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String CRLF = "
    ";
            StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI()).append(" ")
                    .append(req.getProtocol());
    
            Enumeration reqHeaderEnum = req.getHeaderNames();
    
            while (reqHeaderEnum.hasMoreElements()) {
                String headerName = (String) reqHeaderEnum.nextElement();
                buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));
            }
    
            buffer.append(CRLF);
    
            int responseLength = buffer.length();
    
            resp.setContentType("message/http");
            resp.setContentLength(responseLength);
            ServletOutputStream out = resp.getOutputStream();
            out.print(buffer.toString());
            out.close();
        }
    
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String method = req.getMethod();
    
            if (method.equals("GET")) {
                long lastModified = getLastModified(req);
                if (lastModified == -1L) {
                    doGet(req, resp);
                } else {
                    long ifModifiedSince;
                    long ifModifiedSince;
                    try {
                        ifModifiedSince = req.getDateHeader("If-Modified-Since");
                    } catch (IllegalArgumentException iae) {
                        ifModifiedSince = -1L;
                    }
                    if (ifModifiedSince < lastModified / 1000L * 1000L) {
                        maybeSetLastModified(resp, lastModified);
                        doGet(req, resp);
                    } else {
                        resp.setStatus(304);
                    }
                }
            } else if (method.equals("HEAD")) {
                long lastModified = getLastModified(req);
                maybeSetLastModified(resp, lastModified);
                doHead(req, resp);
            } else if (method.equals("POST")) {
                doPost(req, resp);
            } else if (method.equals("PUT")) {
                doPut(req, resp);
            } else if (method.equals("DELETE")) {
                doDelete(req, resp);
            } else if (method.equals("OPTIONS")) {
                doOptions(req, resp);
            } else if (method.equals("TRACE")) {
                doTrace(req, resp);
            } else {
                String errMsg = lStrings.getString("http.method_not_implemented");
                Object[] errArgs = new Object[1];
                errArgs[0] = method;
                errMsg = MessageFormat.format(errMsg, errArgs);
    
                resp.sendError(501, errMsg);
            }
        }
    
        private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {
            if (resp.containsHeader("Last-Modified"))
                return;
            if (lastModified >= 0L)
                resp.setDateHeader("Last-Modified", lastModified);
        }
    
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            HttpServletResponse response;
            try {
                HttpServletRequest request = (HttpServletRequest) req;
                response = (HttpServletResponse) res;
            } catch (ClassCastException e) {
                throw new ServletException("non-HTTP request or response");
            }
            HttpServletResponse response;
            HttpServletRequest request;
            service(request, response);
        }
    }

    虽然我们没有再写service方法,写的是doGet、doPost方法。其实真正访问一个继承HttpServlet的servlet时先调用的是HttpServlet的service方法,再根据你的请求选择使用doGet方法处理,还是用doPost方法处理。

    其实还可以不走寻常路,不复写他的doGet等方法。而是直接复写他的service方法

    package com.httpservlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    
    @WebServlet(name = "MyServlet", urlPatterns = { "/formServlet" })
    public class FormServlet extends HttpServlet{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        @Override
        public void service(ServletRequest req, ServletResponse response) throws ServletException, IOException {
            String servletName = getServletConfig().getServletName();
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html><head></head>" + "<body>Hello from " + servletName +"</body></html>");
        }
    
    }

    访问 http://localhost:8080/servlet-jsp/formServlet

    页面输出 Hello from FormServlet

    
    
    
  • 相关阅读:
    Linux 中国镜像
    VMWare 安装 Debian 9
    Linux 安装Docker compose 快速方法
    Nginx on Docker 配置
    Docker 中配置Nginx
    Nginx IPV4 问题
    VMWare中CentOS 7 配置 XShell连接
    在CentOS 7 中安装Docker
    Docker for Windows 和虚拟机VMWare共存方案
    Nginx 配置多个站点
  • 原文地址:https://www.cnblogs.com/vincentren/p/5778170.html
Copyright © 2011-2022 走看看