实现servlet的三种方式

目录

    • 实现Servlet接口
    • 继承GenericServlet类
    • 继承HttpServlet类

实现Servlet接口

实现Serclet接口并重写五个方法:

  • void init(ServletConfig config)
  • void destroy()
  • void service(ServletRequest request, ServletResponse response)
  • String getServletInfo()
  • ServletConfig getServletConfig()

示例代码:

package com.hitmt.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;

/**
 * 

Description: 通过实现Servlet接口,从而实现Servlet的演示

* @author HiTMT * @date 2019年10月12日上午12:30:36 */
@WebServlet("/MyServlet") public class MyServlet implements Servlet { @Override public void destroy() { System.out.println("MyServlet被销毁了"); } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig config) throws ServletException { System.out.println("配置信息:"+config); } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("Hello World"); } }

继承GenericServlet类

继承GenericServlet类并重写一个方法:

  • void service(ServletRequest request, ServletResponse response)

示例代码:

package com.hitmt.servlet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

/**
 * 

Description: 通过继承GenericServlet实现Servlet的演示

* @author HiTMT * @date 2019年10月12日上午12:30:24 */
@WebServlet("/MyGenericServlet") public class MyGenericServlet extends GenericServlet { @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write("Hello World"); } }

继承HttpServlet类

继承GenericServlet类并可重写两个方法:

  • void doGet(HttpServletRequest req, HttpServletResponse resp)
  • void doPost(HttpServletRequest req, HttpServletResponse resp)

示例代码:

package com.hitmt.servlet;

import java.io.IOException;

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

/**
 * 

Description: 通过继承HttpServlet实现Servlet的演示

* @author HiTMT * @date 2019年10月12日上午12:29:45 */
@WebServlet("/MyHttpServlet") public class MyHttpServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("Hello World"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

你可能感兴趣的:(java,javaEE,javaWeb,Servlet,JavaEE练习,Java复习的作业)