有关于servlet,大部分人配置的时候,都是惯性在web.xml中生成的

这样的配置将会造成web.xml极其臃肿,不便修改

因此,在servlet2.5之后,servlet的配置允许直接在java代码中进行注解配置(注解如同标签,具体操作如下)

1.创建对应的class文件,并且继承HttpServlet

public class annotationServlet extends HttpServlet{

}

2.在定义类上方添加注解(/记得加,这个不可缺少)

@WebServlet("/testServlet")

3.添加对应的业务即可.

接下来添加较为完整的代码

@WebServlet("/testServlet")
public class annotationServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("我已经运行成功了");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}

以上便是注解式Servlet的用法

posted on 2018-10-15 09:41  千重山  阅读(365)  评论(0)    收藏  举报