在servlet 3.0之前,需要通过在web.xml中提供url模式和servlet类来配置servlet。每当发送servlet的请求时,Tomcat会在web.xml中搜索servlet类,并根据请求类型调用doget或dopost。
在servlet 3.0中,我们可以使用注释(如@WebServlet)在servlet类中自行配置servlet。
我的问题是,在这种情况下Tomcat如何识别servlet类,因为在web.xml中没有为servlet指定映射。
提前感谢您的帮助。
classes
目录下的所有jar文件和类文件,使用类似于clazz = Class.forName(theClassName)
的代码获取类,然后调用clazz.getDeclaredAnnotations()
或clazz.getAnnotation(WebServlet.class)
。然后它读取注释的属性以提取servlet映射。Class.forName()
,而是将类作为字节数组加载,并将其传递给类加载器。web.xml
中提取的相同(或类似)信息。 - AlexR
Processor
接口。 - Luiggi Mendoza@WebServlet
或 JS - Luiggi MendozaProcessor
接口仅在编译时使用,而在运行时使用其他反射策略。您是否有关于在运行时使用它进行处理的文章? - chrylis -cautiouslyoptimistic-