如何使用注解配置Servlet

版权声明:《==study hard and make progress every day==》 https://blog.csdn.net/qq_38225558/article/details/82959949

从JavaEE6(Tomcat7,Servlet3.0)开始,可以使用注解来取代部分web.xml配置

第一步:修改web.xml文件

metadata-complete="false"    ==>    true:不扫描类上的WebServlet注解      false:要扫描

ex:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" metadata-complete="false">

</web-app>

第二步:  配置 @WebServlet 注解

ex:

//@WebServlet(value={"/HelloServlet"},loadOnStartup=1)
@WebServlet("/HelloServlet")  //常用
public class HelloServlet extends HttpServlet {
	@Override
	public void init() throws ServletException {
		System.out.println("===初始化===");
	}
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("===Servlet程序===");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_38225558/article/details/82959949