路径

URI和URL的区别

案例演示:
工程案例目录结构

web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Servlet03</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>web.AbcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/abc</url-pattern>
</servlet-mapping>
</web-app>
AbcServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AbcServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//项目名
System.out.println(req.getContextPath());
//Servlet网名
System.out.println(req.getServletPath());
//URI:Servlet绝对路径
System.out.println(req.getRequestURI());
//URL:Servlet完整路径
System.out.println(req.getRequestURL());
}
}
控制台输出信息与Tomcat部署工程目录结构:


本文通过一个具体的工程案例,深入解析了URI(统一资源标识符)和URL(统一资源定位符)的区别。通过web.xml配置文件及AbcServlet.java源代码展示了如何获取请求的URI和URL,并解释了它们在Web应用中的作用。
876

被折叠的 条评论
为什么被折叠?



