前言
最近在看Servlet中用注解代替web.xml初始化Servlet Context,servlet规范提到:
In a web application, classes using annotations will have their annotations processed
only if they are located in the WEB-INF/classes directory, or if they are packaged
in a jar file located in WEB-INF/lib within the application.
根据这段描述,如果用注解添加自定义Servlet:MyServlet(已经删除web.xml),代码如下:
@WebServlet(name = "MyServlet", urlPatterns = "*.do")
public class MyServlet extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
super.service(req, res);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("<html>Hello World</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
要求MyServlet要在WEB-INF/classes里或者WEB-INF/lib中jar包里。
问题:classpath和这两个路径是什么关系?spring配置里用的classpath*和classpath有什么区别?怎么在servlet要读取classpath里的文件?webapp的classpath是哪个目录?WEB-INF文件夹是classpath吗?webapp里除了WEB-INF文件夹下的文件,其他文件能用servlet读取吗?webapp的文件结构?JSP文件要不要放在WEB-INF/下?
理解classpath
根据官网的描述:ht