相對路徑和絕對路徑
阿新 • • 發佈:2017-06-29
lose 至少 scrip 默認 domain xtend 文件 dog ons
創建一個web項目,在webroot的目錄下新建兩個界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equivlogin.jsp頁面="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!--<link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- /doMain.jsp 前臺路徑: 如果帶 / 它的參照路徑是 http://localhost:8080/ tomcat服務器的根路徑 doMain.jsp 前臺路徑: 如果不帶 / 它的參照路徑是http://localhost:8080/UrlTest/ web項目的根路徑 --> <a href="doMain.jsp">跳轉</a> </body> </html>
<body> <h1>這是處理界面....</h1> </body>doMain.jsp頁面
在webroot下面新創建一個jsp文件夾,之後再jsp文件夾下面創建兩個界面
<body> <%-- 這個TestServlet沒有增加/ 默認的參照路徑是 項目根目錄 --%> <a action="TestServlet">跳轉</a> </body>login.jsp頁面
<body> <h1>這是jsp文件夾下面處理界面....</h1> </body>doMain.jsp頁面
創建對應的servlet
public class TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); // doPost() } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("進入了servlet......"); /** * /jsp/doMain.jsp 後臺路徑 加 / 參照路徑就是項目的根目錄 * request.getRequestDispatcher("/jsp/doMain.jsp").forward(request, * response); */ /** * 重定向 比較特殊! 因為是客戶端的行為,至少兩次訪問服務器,再第二次訪問服務器的時候,就是前臺路徑了, * 這個時候 不能加 / */ response.sendRedirect("/jsp/doMain.jsp"); } }TestServlet
======================解決路徑疊加的問題======================
在webroot下面創建兩個界面
<body> <a href="test/MyServlet">跳轉</a> </body>1.jsp
<body> <%-- 這樣會出現問題 <a href="test/MyServlet">跳轉</a> --%> <a href="${pageContext.request.contextPath}/test/MyServlet">跳轉</a> </body>2.jsp
一定要修改web.xml中MyServlet的url為 test/MyServlet
public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); // doPost() } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/2.jsp").forward(request, response); } }MyServlet
相對路徑和絕對路徑