不废话,上代码
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;
@WebServlet(
name="commonServlet",
description="公共测试Servlet",
urlPatterns={"/index.html"},
asyncSupported=true)
public class CommonController extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
resp.getWriter().append("hello,this is a servlet").close();
}
}
配置了注解之后web.xml里面基本不需要写东西,但要注意不要在web.xml里面禁用注解(怎么禁用暂时不讨论),否则导致注解失效。
访问地址:
http://localhost/index.html
参考内容: