@WebServlet("/cdiservlet") public class NewServlet extends HttpServlet { private Message message; @Override ||将doget重写 public void init() { message = new MessageB(); ||初始化 } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().write(message.get()); ||捕获可能发生的异常 }}
public interface Message { ||servlet实现Message接口的一个对象实例 public String get();}
public class MessageB implements Message { ||servlet创建以下对象的实例 public MessageB() { } @Override public String get() { return "message B"; }}
@WebServlet("/cdiservlet") ||使用CDI,servlet声明它对Message实例的依赖性,并让它由CDI运行时自动注入public class NewServlet extends HttpServlet { @Inject private Message message; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().write(message.get()); ||捕获可能发生的异常 } }