Day 12 绝对路径相对路径

一.绝对路径相对路径

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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值