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


相对路径



项目结构1






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">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">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">to A.jsp</a>
</body>
</html>


运行结果1





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


项目结构2




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">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">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">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








点击 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">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">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">to A.jsp</a>
</body>
</html>
修改后,运行结果正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值