Day 12 绝对路径相对路径
2017-01-31 14:17
197 查看
一.绝对路径相对路径
1.开发时一般用绝对路径,写绝对路径可定不会出现问题,写相对路径有可能会出现问题
再由Servlet转发到JSP页面是,此时浏览器上显示的是Servlet的路径,而JSP页面的超链接还是相对于该JSP页面的地址,则可能会出现路径混乱的问题
2.编写绝对路径可以避免上述问题
1)绝对路径:相对于当前WEB应用的根路径的路径(即所有路径都得加contextPath)
如/代表的是站点的根目录则在前面加contextPath就好(而contextPath可以由request或application的getContextPath()方法来获取)
例如:
<%a href="<=request.getContextPath()%/TestServlet">to b</a>
response.sendRedirect(request.getContextPath()+"/path/c.jsp")
http://localhost:8080/contextPath(当前WEB应用的上下文路径)/a.jsp ---是绝对路径
http://localhost:8080/a.jsp --------不是
2)JAVAWEB开发中的 “/” 到底代表什么
①当前WEB应用的根路径:http://localhost:8080/contextPath/
---若/需交由Servlet容器来处理---
---各种定制标签中的---
>请求转发时:
request.getRequestDispatcher("/path/b.jsp").forward(request, response);
>Web.xml文件映射Servlet访问路径:
<servlet-mapping>
</servlet-name>TestServlet</servlet-name>
<servlet-pattern>TestServlet</servlet-pattern>
</servlet-maping>
②WEB站点的路径:http://localhost:8080/
----若/交由浏览器来处理--
>超链接:<a href="TestServlet">to b</a>
>表达式中的action:<form action="/login.jsp">
>请求重定向的时候:request.getRequestDispatcher("/path/b.jsp").forward(request, response)
3.例:
相对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Tra
a03c
nsitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>aaa</h4>
<a href="TestServlet">to b</a>
</body>
</html>TestServlet.java
package com.atguigu.javaweb;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TServlet
*/
@WebServlet("/TServlet")
public class TServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name="aaaccbb";
request.setAttribute("name", name);
request.getRequestDispatcher("/path/b.jsp").forward(request, response);
}
}
path/b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>bbb</h4>
<br>
cities:<%= request.getAttribute("name") %>
<a href="c.jsp">to c </a>
</body>
</html>c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>ccc</h4>
<a href="../a.jsp">to a </a>
</body>
</html>
当从a到b后再到c就出问题了
改为绝对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>aaa</h4>
<a href="<%= request.getContextPath() %>/TestServlet">to b</a>
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>bbb</h4>
<br>
cities:<%= request.getAttribute("name") %>
<a href="<%= request.getContextPath() %>/path/c.jsp">to c </a>
</body>
</html>
c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>ccc</h4>
<a href="<%= request.getContextPath() %>/a.jsp">to a </a>
</body>
</html>
1.开发时一般用绝对路径,写绝对路径可定不会出现问题,写相对路径有可能会出现问题
再由Servlet转发到JSP页面是,此时浏览器上显示的是Servlet的路径,而JSP页面的超链接还是相对于该JSP页面的地址,则可能会出现路径混乱的问题
2.编写绝对路径可以避免上述问题
1)绝对路径:相对于当前WEB应用的根路径的路径(即所有路径都得加contextPath)
如/代表的是站点的根目录则在前面加contextPath就好(而contextPath可以由request或application的getContextPath()方法来获取)
例如:
<%a href="<=request.getContextPath()%/TestServlet">to b</a>
response.sendRedirect(request.getContextPath()+"/path/c.jsp")
http://localhost:8080/contextPath(当前WEB应用的上下文路径)/a.jsp ---是绝对路径
http://localhost:8080/a.jsp --------不是
2)JAVAWEB开发中的 “/” 到底代表什么
①当前WEB应用的根路径:http://localhost:8080/contextPath/
---若/需交由Servlet容器来处理---
---各种定制标签中的---
>请求转发时:
request.getRequestDispatcher("/path/b.jsp").forward(request, response);
>Web.xml文件映射Servlet访问路径:
<servlet-mapping>
</servlet-name>TestServlet</servlet-name>
<servlet-pattern>TestServlet</servlet-pattern>
</servlet-maping>
②WEB站点的路径:http://localhost:8080/
----若/交由浏览器来处理--
>超链接:<a href="TestServlet">to b</a>
>表达式中的action:<form action="/login.jsp">
>请求重定向的时候:request.getRequestDispatcher("/path/b.jsp").forward(request, response)
3.例:
相对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Tra
a03c
nsitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>aaa</h4>
<a href="TestServlet">to b</a>
</body>
</html>TestServlet.java
package com.atguigu.javaweb;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TServlet
*/
@WebServlet("/TServlet")
public class TServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name="aaaccbb";
request.setAttribute("name", name);
request.getRequestDispatcher("/path/b.jsp").forward(request, response);
}
}
path/b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>bbb</h4>
<br>
cities:<%= request.getAttribute("name") %>
<a href="c.jsp">to c </a>
</body>
</html>c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>ccc</h4>
<a href="../a.jsp">to a </a>
</body>
</html>
当从a到b后再到c就出问题了
改为绝对路径:
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>aaa</h4>
<a href="<%= request.getContextPath() %>/TestServlet">to b</a>
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>bbb</h4>
<br>
cities:<%= request.getAttribute("name") %>
<a href="<%= request.getContextPath() %>/path/c.jsp">to c </a>
</body>
</html>
c.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>ccc</h4>
<a href="<%= request.getContextPath() %>/a.jsp">to a </a>
</body>
</html>
相关文章推荐
- WPF后台加载文件的相对路径和绝对路径记载
- unix绝对路径与相对路径
- html中的绝对路径URL和相对路径URL及子目录、父目录、根目录
- 绝对路径和相对路径
- C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- 获取当前相对绝对路径
- 二周第一次课(3月26日)相对和绝对路径、cd命令、mkdir rmdir命令、rm命令
- java中相对路径,绝对路径问题总结
- 相对和绝对路径/cd命令/创建和删除目录mkdir/rmdir/rm命令
- C# url 路径转换 相对路径 转换为 绝对路径
- 相对路径、绝对路径
- jsp引入js时的绝对路径和相对路径
- CMake生成VS2010工程相对路径和绝对路径问题说明
- iOS的相对路径和绝对路径
- 基础 HTML之目录问题(相对路径和绝对路径区别)
- 相对路径 绝对路径 根目录 上一目录 当前目录 src
- jsp中重定向、请求分派中的绝对路径与相对路径!
- 绝对路径与相对路径的相互转换
- C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- 关于相对路径和绝对路径