Servlet采用了接口的设计思想,由Tomcat提供
HttpServlet是⼀个与HTTP协议相关的Servlet,专门用来处理HTTP协议的请求响应。
在HttpServlet类的service方法内部,根据HTTP协议请求方式不同,执行不同的doXXX的方法(get请求执行doGet方法,如果是post请求就会执行doPost方法)继承了HttpServlet之后不需要重写service方法,只需要重写doGet和doPost方法即可。浏览器客户端只响应了doGet的输出: servlet接口下的方法service()方法 在tomcat运行的时候就会自动完成(HttpServletRequest req, HttpServletResponse resp)中req、resp对象的创建
String method = req.getMethod();//获取定义的请求方式是post请求还是get请求 /** post请求只有在表单中存在,get请求则是显示在地址栏中 */在HttpServlet中定义doGet方法
/** * Called by the server (via the <code>service</code> method) to * allow a servlet to handle a GET request. * * <p>Overriding this method to support a GET request also * automatically supports an HTTP HEAD request. A HEAD * request is a GET request that returns no body in the * response, only the request header fields. * * <p>When overriding this method, read the request data, * write the response headers, get the response's writer or * output stream object, and finally, write the response data. * It's best to include content type and encoding. When using * a <code>PrintWriter</code> object to return the response, * set the content type before accessing the * <code>PrintWriter</code> object. * * <p>The servlet container must write the headers before * committing the response, because in HTTP the headers must be sent * before the response body. * * <p>Where possible, set the Content-Length header (with the * {@link javax.servlet.ServletResponse#setContentLength} method), * to allow the servlet container to use a persistent connection * to return its response to the client, improving performance. * The content length is automatically set if the entire response fits * inside the response buffer. * * <p>When using HTTP 1.1 chunked encoding (which means that the response * has a Transfer-Encoding header), do not set the Content-Length header. * * <p>The GET method should be safe, that is, without * any side effects for which users are held responsible. * For example, most form queries have no side effects. * If a client request is intended to change stored data, * the request should use some other HTTP method. * * <p>The GET method should also be idempotent, meaning * that it can be safely repeated. Sometimes making a * method safe also makes it idempotent. For example, * repeating queries is both safe and idempotent, but * buying a product online or modifying data is neither * safe nor idempotent. * * <p>If the request is incorrectly formatted, <code>doGet</code> * returns an HTTP "Bad Request" message. * * @param req an {@link HttpServletRequest} object that * contains the request the client has made * of the servlet * * @param resp an {@link HttpServletResponse} object that * contains the response the servlet sends * to the client * * @exception IOException if an input or output error is * detected when the servlet handles * the GET request * * @exception ServletException if the request for the GET * could not be handled * * @see javax.servlet.ServletResponse#setContentType */ 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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg); } else { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg); } }构建的实体类继承HttpServlet,其中doGet方法作为父类,再在实体类中重写doGet方法,在客户端浏览器中调用子类的doGet方法,由于子类service方法没有重写,所以就直接调用父类的service方法