Web工程下URL地址,有斜杠与没有斜杠路径的区别及使用

对于Web工程下URL地址,有两种写法: ① 绝对路径(“以’/ ‘开头的路径”) ② 相对路径(”不以’/ ‘开头的路径“)

对于绝对路径的URL(“以’/ ‘开头的路径”)需要根据这个URL是给服务器用的,还是客户端浏览器用的。

  • ① 如果是给服务器用的,则”/”代表当前应用(如这里的RequestAndResponse或者是你的web项目路径WebContent、WebRoot);
  • ② 如果是给客户端浏览器用的,则”/”代表当前站点(如这里的http://localhost:8080/或者是tomcat下的> webapps);

例子:

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.转发类型:(给服务器用的地址)
        //request.getRequestDispatcher("/page/test1.jsp").forward(request, response);//正确
        //request.getRequestDispatcher("page/test1.jsp").forward(request, response);//相对于当前web项目 正确
        //request.getRequestDispatcher("/RequestAndResponse/page/test1.jsp").forward(request, response); //HTTP Status 404 - /RequestAndResponse/RequestAndResponse/page/test1.jsp
    //2.重定向类型:(给浏览器用的地址)
    //response.sendRedirect("/page/test1.jsp");//error: http://localhost:8080/page/test1.jsp
    //response.sendRedirect("page/test1.jsp");//正确
    //response.sendRedirect("/RequestAndResponse/page/test1.jsp");//正确
    //3.获取资源真实路径:(给服务器用的)
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    OutputStream out = response.getOutputStream();
    String path = this.getServletContext().getRealPath("/page/test1.jsp");//(1)正确: 
    String path = this.getServletContext().getRealPath("page/test1.jsp"); //(2)正确: 
    String path = this.getServletContext().getRealPath("/RequestAndResponse/page/test1.jsp");//(3)错误地址得到的是:E:\workspace\jsp\.metadata\.me_tcat\webapps\RequestAndResponse\RequestAndResponse\page\test1.jsp
            //4.获取资源文件输入流对象:(给服务器用的)
     InputStream in = this.getServletContext().getResourceAsStream("/page/test1.jsp");//正确:正确输出test1.jsp页面
        InputStream in = this.getServletContext().getResourceAsStream("page/test1.jsp");//正确:正确输出test1.jsp页面
    InputStream in = this.getServletContext().getResourceAsStream("/RequestAndResponse/page/test1.jsp");//错误java.lang.NullPointerException;
    }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
<base href="<%=basePath%>">
<title>My JSP 'test2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
 </head>
<body>
 5. 给客户端浏览器用的地址-form action=""&gt;类型<br>
表单1:
<form action="/page/test1.jsp">
 <input type="text" name="username" value="中国">
 <input type="submit" value="提交" name="button">
 (action="/page/test1.jsp" 地址错误=localhost:8080/page/test1.jsp)
 </form>
 <br>
 表单2:
 <form action="page/test1.jsp">
 <input type="text" name="username" value="中国">
 <input type="submit" value="提交" name="button">
 (action="page/test1.jsp" 地址正确)
 </form>
 <br>
 表单3:
 <form action="/RequestAndResponse/page/test1.jsp">
 <input type="text" name="username" value="中国">
 <input type="submit" value="提交" name="button">
 (action="/RequestAndResponse/page/test1.jsp" 地址正确)
 </form>
 <br>
 表单4:
 <form action="../page/test1.jsp">
 <input type="text" name="username" value="中国">
 <input type="submit" value="提交" name="button">
 (action="../page/test1.jsp" 地址错误,跳转至http://localhost:8080/page/test1.jsp)
 </form>
 <br>
 6. 给客户端浏览器用的地址 a标签
 <a href="/page/test1.jsp">连接1(href="/page/test1.jsp" 地址错误)</a><p>
 <a href="page/test1.jsp">连接2(href="page/test1.jsp" 地址正确)</a><p>
 <a href="/RequestAndResponse/page/test1.jsp">连接3(href="/RequestAndResponse/page/test1.jsp" 地址正确)</a> <p> 
 <a href="../page/test1.jsp">连接4(href="../page/test1.jsp" 地址错误,跳转至http://localhost:8080/page/test1.jsp)</a><p>
 </body>
</html>

你可能感兴趣的:(Web,url,路径)