一.绝对路径相对路径
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 --------不是
>超链接:<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>