第一种 实现 Servlet 接口,需要覆写 Servlet 的5个方法,并将ServletConfig对象保存到类级变量中
package app01a; import java.io.IOException; import java.io.PrintWriter; 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; @WebServlet(name = "MyServlet", urlPatterns = {"/my"}) public class MyServlet implements Servlet { private transient ServletConfig servletConfig; @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return servletConfig; } @Override public String getServletInfo() { return "My Servlet"; } @Override public void init(ServletConfig servletConfig) throws ServletException { this.servletConfig = servletConfig; } @Override public void service(ServletRequest resuest, ServletResponse response) throws ServletException, IOException { String servletName = servletConfig.getServletName(); response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.print("<html><head></head>" + "<body>Hello form " + servletName + "</body></html>"); } }
第二种 继承抽象类 GenericServlet覆写service()方法
package app01a; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; @WebServlet(name = "GenericServletDemoServlet", urlPatterns = { "/generic" }, initParams = { @WebInitParam(name = "admin", value = "Harry taciak"), @WebInitParam(name = "email", value = "admin@example.com") } ) public class GenericServletDemoServlet extends GenericServlet { private static final long serialVersionUID = 1L; @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { ServletConfig servletConfig = getServletConfig(); String admin = servletConfig.getInitParameter("admin"); String email = servletConfig.getInitParameter("email"); response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.print("<html><head></head><body>" + "Admin: " + admin + "<br/>Email: " + email + "</body></html>"); } }
第三种 继承类 HttpServlet 覆写doGet()和doPost()方法 public abstract class HttpServlet extends GenericServlet
package app01c; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "SimleServlet", urlPatterns = {"/simple"}) public class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SimpleServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<html>"); writer.println("<head>"); writer.println("</head>"); writer.println("<body>"); writer.println("This is the Simple Servlet"); writer.println("</body>"); writer.println("</html>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
GenericServlet、HttpServlet是Servlet接口有两个默认的实现类。
HttpServlet在原有的Servlet接口上添加了一些HTTP协议的处理方法,是能够处理HTTP请求的Servlet。其功能比Servlet更加强大,因此开发人员在编写Servlet时,通常应继承HttpServlet类,而不是直接实现Servlet接口。
HttpServlet在实现Servlet接口时覆写了service()方法,该方法体内的代码会自动判断用户的请求,如果是Get请求,则自动调用HttpServlet的doGet()方法;如果是Post请求,则自动调用doPost()方法。通常开发人员在编写Servlet时,只需要覆写doGet()或doPost()方法,而不用再去覆写service()方法。