Web应用配置文件和目录

web部署描述符


    
    HelloServlet
    
    isgod.niezhic.servlet.HelloServlet
    
    1


    
    HelloServlet
    
    /hello

web部署描述符中定义URL
一个请求的URL实际上由三个部分组成:contextPath + servletPath + pathInfo,可以使用HttpServletRequest的getRequestURL()来获取
contextPath可以使用HttpServletRequest的getContextPath()来获取
servletPath可以使用HttpServletRequest的getServletPath()来获取,但是当URL模式为 /* 和 "" 的时候,getServletPath()取得路径为空字符串
pathInfo可以使用HttpServletRequest的getPathInfo()来获取,没有额外路径信息时返回null

常见的URL模式:

1. 以 / 开头以 /* 结尾的模式,如 /account/* 表示访问帐户目录下中的资源文件: /account/open、/account/close
2. 以 *. 开头的模式,如 *.view 表示处理所有以view结尾请求
3. 完全匹配模式,如 /tester/test 表示URL中除了请求参数部分必须是 /tester/test

示例Servlet代码,如下:
/**
 * 请求URL路径解析
 */
public class URLPathServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        // 完整的请求URL路径,输出:http://localhost:8080/learnServlet/url/path/233
        out.println(request.getRequestURL() + "
"); // 当前环境根路径,输出:/learnServlet out.println(request.getContextPath() + "
"); // 处理Servlet路径,输出:/url/path out.println(request.getServletPath() + "
"); // 额外路径信息,输出:/233 out.println(request.getPathInfo() + "
"); } }

WEB-INF目录

1. 该目录名称固定,放置在该目录中的文件对外界是封闭的,客户端无法直接获取这些资源,只有通过处理相应Servlet的请求来获取
2. web.xml:web应用的部署表述文件,名称固定,路径也一定是在WEB-INF中

web-fragment.xml

在Servlet3.0中,如果一个JAR文件中有使用标注的Servlet,Web容器也可以加载该类
在Servlet3.0中,Web应用可以使用JAR来进行模块化部署,不仅是Servlet,监听器、过滤器等都可以定义在JAR文件中
在JAR文件中的Serlvet、监听器、过滤器等也拥有自己的部署表述符,那就是web-fragment.xml:




web-fragment.xml的根标签是

你可能感兴趣的:(Web应用配置文件和目录)