天天看点

javaWEB总结(24):相对路径和绝对路径相对路径绝对路径

相对路径

项目结构1

javaWEB总结(24):相对路径和绝对路径相对路径绝对路径

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_24</display-name>
  <welcome-file-list>
    <welcome-file>a.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           

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>A.jsp</title>
</head>
<body>

	<!-- 相对路径,当前目录下的path下的b.jsp -->

	<a href="./path/b.jsp" target="_blank" rel="external nofollow" >To B.jsp</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>B.jsp</title>
</head>
<body>


	<!-- 相对路径,当前目录下的c.jsp -->
	<a href="./c.jsp" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >To C.jsp</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>C.jsp</title>
</head>
<body>

	<!-- 相对路径,上层目录下的b.jsp -->
	<a href="../a.jsp" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >to A.jsp</a>
</body>
</html>
           

运行结果1

javaWEB总结(24):相对路径和绝对路径相对路径绝对路径
javaWEB总结(24):相对路径和绝对路径相对路径绝对路径
javaWEB总结(24):相对路径和绝对路径相对路径绝对路径
javaWEB总结(24):相对路径和绝对路径相对路径绝对路径

有时候我们需要显示一些数据,可能会通过转发的方式到另外一个页面。

项目结构2

javaWEB总结(24):相对路径和绝对路径相对路径绝对路径

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_24</display-name>
  <welcome-file-list>
    <welcome-file>a.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           

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>A.jsp</title>
</head>
<body>


	<a href="toBServlet" target="_blank" rel="external nofollow" >To B.jsp</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>B.jsp</title>
</head>
<body>

	cities:<%=request.getAttribute("cities") %>
	<br><br>
	
	<!-- 相对路径,当前目录下的c.jsp -->
	<a href="./c.jsp" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >To C.jsp</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>C.jsp</title>
</head>
<body>

	<!-- 相对路径,上层目录下的b.jsp -->
	<a href="../a.jsp" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >to A.jsp</a>
</body>
</html>
           

ToBServlet.java

package com.dao.chu;

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 ToBServlet
 */
@WebServlet("/toBServlet")
public class ToBServlet 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 {
	
		List<String> cities =Arrays.asList("北京","上海","广州","深圳");
		request.setAttribute("cities", cities);
		
		
		request.getRequestDispatcher("/path/b.jsp").forward(request, response);
	}

}

           

运行结果2

javaWEB总结(24):相对路径和绝对路径相对路径绝对路径
javaWEB总结(24):相对路径和绝对路径相对路径绝对路径
javaWEB总结(24):相对路径和绝对路径相对路径绝对路径

点击 To B.jsp后,地址栏中http://localhost:8080/javaWeb_24/toBServlet 为跟路径下,所以点击To C.jsp会默认以跟路径为相对路径http://localhost:8080/javaWeb_24/c.jsp,这样便出现了问题。

绝对路径

javaWeb中什么是绝对路径?

相对于当前web应用的跟路径。此处为:http://localhost:8080/javaWeb_24/。即任何的路径都必须戴上contextPath.

注意:http://localhost:8080/此路径为WEB站点的跟路径。

如何完成编写?

若/代表的是当前WEB站点的跟路径,则在其前面加上contextPath就可以。

而contextpath可以由request或application的getContextPath()方法来获取。

javaweb开发中的"/“代表什么?

①当前WEB应用的跟路径:

1.请求转发时request.getRequestDispatcher("/path/b.jsp").forward(request, response);

2.web.xml文件中映射servlet访问路径

3.各种标签中的/

②WEB站点的跟路径:

1.超链接.

2.表单中的action.

3.请求重定向时,response.sendRedirect.

总结:若"/“需交由Servlet容器来处理,则代表当前WEB应用的跟路径。若"/”交由游览器来处理,则代表当前WEB站点的跟路径。

所以只需在三个超链接加上<%=request.getContextPath() %>即可。

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>A.jsp</title>
</head>
<body>


	<a href="<%=request.getContextPath() %>/toBServlet" target="_blank" rel="external nofollow" >To B.jsp</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>B.jsp</title>
</head>
<body>

	cities:<%=request.getAttribute("cities") %>
	<br><br>
	
	<!-- 相对路径,当前目录下的c.jsp -->
	<a href="<%=request.getContextPath() %>/path/c.jsp" target="_blank" rel="external nofollow" >To C.jsp</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>C.jsp</title>
</head>
<body>

	<!-- 相对路径,上层目录下的b.jsp -->
	<a href="<%=request.getContextPath() %>/a.jsp" target="_blank" rel="external nofollow" >to A.jsp</a>
</body>
</html>
           

修改后,运行结果正常