(1)几种Servlet

Servlet

  • 需要实现很多方法init、getServletConfig、service、getServletInfo、destroy
  • 我们一般只需要service方法 ``` package com.code.web;

import javax.servlet.*; import javax.servlet.annotation.WebServlet; import java.io.IOException;

@WebServlet(urlPatterns = “/demo”) public class HelloServlet implements Servlet {

  1. /**
  2. * 被创建的时候,调用1次
  3. * @param servletConfig
  4. * @throws ServletException
  5. */
  6. @Override
  7. public void init(ServletConfig servletConfig) throws ServletException {
  8. System.out.println("init");
  9. }
  10. /**
  11. * 获取ServletConfig配置对象
  12. */
  13. @Override
  14. public ServletConfig getServletConfig() {
  15. System.out.println("getServletConfig");
  16. return null;
  17. }
  18. /**
  19. * 调用的时候,多次调用多次执行
  20. */
  21. @Override
  22. public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
  23. System.out.println("service");
  24. }
  25. @Override
  26. public String getServletInfo() {
  27. System.out.println("getServletInfo");
  28. return null;
  29. }
  30. /**
  31. * 销毁的时候,执行1次
  32. */
  33. @Override
  34. public void destroy() {
  35. System.out.println("destroy");
  36. }

}

  1. <a name="BMEVr"></a>
  2. #### GenericServlet
  3. - GenericServlet只暴露service()

package com.code.web;

import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import java.io.IOException;

@WebServlet(“/demo2”) public class Servlet2 extends GenericServlet {

  1. @Override
  2. public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
  3. System.out.println("demo2");
  4. }

}

  1. <a name="zycfk"></a>
  2. #### HttpServlet
  3. - 对http协议的一种封装,简化操作

package com.code.web;

import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;

@WebServlet(“/demo3”) public class Servlet3 extends HttpServlet {

  1. @Override
  2. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  3. super.doGet(req, resp);
  4. System.out.println("doGet");
  5. }
  6. @Override
  7. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  8. super.doPost(req, resp);
  9. System.out.println("doPost");
  10. }

}

  1. - service方法封装成getpost
  1. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2. String method = req.getMethod();
  3. long lastModified;
  4. if (method.equals("GET")) {
  5. lastModified = this.getLastModified(req);
  6. if (lastModified == -1L) {
  7. this.doGet(req, resp);
  8. } else {
  9. long ifModifiedSince;
  10. try {
  11. ifModifiedSince = req.getDateHeader("If-Modified-Since");
  12. } catch (IllegalArgumentException var9) {
  13. ifModifiedSince = -1L;
  14. }
  15. if (ifModifiedSince < lastModified / 1000L * 1000L) {
  16. this.maybeSetLastModified(resp, lastModified);
  17. this.doGet(req, resp);
  18. } else {
  19. resp.setStatus(304);
  20. }
  21. }
  22. } else if (method.equals("HEAD")) {
  23. lastModified = this.getLastModified(req);
  24. this.maybeSetLastModified(resp, lastModified);
  25. this.doHead(req, resp);
  26. } else if (method.equals("POST")) {
  27. this.doPost(req, resp);
  28. } else if (method.equals("PUT")) {
  29. this.doPut(req, resp);
  30. } else if (method.equals("DELETE")) {
  31. this.doDelete(req, resp);
  32. } else if (method.equals("OPTIONS")) {
  33. this.doOptions(req, resp);
  34. } else if (method.equals("TRACE")) {
  35. this.doTrace(req, resp);
  36. } else {
  37. String errMsg = lStrings.getString("http.method_not_implemented");
  38. Object[] errArgs = new Object[]{method};
  39. errMsg = MessageFormat.format(errMsg, errArgs);
  40. resp.sendError(501, errMsg);
  41. }
  42. }

```