1. 項目結構
2. 客戶端路徑
1. 超鏈接
<%@ 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</title>
</head>
<body><!-- 超鏈接和表單都有三種書寫路徑的方式 1,絕對地址 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --><!-- 1.絕對地址 --><!-- 完整的URL --><a href="http://localhost:8080/testPath/jsp/b.jsp">這是絕對地址超鏈接</a><br /><!-- 2.以"/"開頭的相對地址 --><!-- /代表了整個web項目,即:http://localhost:8080/ --><a href="/testPath/jsp/b.jsp">這是以"/"開頭的相對地址超鏈接</a><br /><!-- 3.不以"/"開頭的相對地址 --><!-- 不以/開頭,則相對于當前資源的路徑 當前資源的路徑為:http://localhost:8080/testPath/jsp/ 而b.jsp也在此路徑下 所以直接書寫b.jsp --><a href="b.jsp">這是不以"/"開頭的相對地址超鏈接</a><br />
</body>
</html>
2. 表單
<%@ 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</title>
</head>
<body><!-- 所有的表單都是提交到b.jsp --><!-- 表單提交路徑有三種書寫方式 1,絕對地址 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --><form action="http://localhost:8080/testPath/jsp/b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---絕對地址 "></form><!-- 以/開頭的相對地址,此時的/代表整個web項目,即:http://localhost:8080/ --><form action="/testPath/jsp/b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---以/開頭的相對地址"></form><form action="b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---不以/開頭的相對地址 "></form><!-- 表單提交到Servlet --><!-- 表單提交到Servlet有三種書寫方式 1,絕對路徑 2,以"/"開頭的相對地址 3,不以"/"開頭的相對地址 --><!-- 1.絕對地址 --><!-- 完整的URL --><form action="http://localhost:8080/testPath/PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---絕對地址"></form><!-- 2.以/開頭的相對地址 --><!-- 此時的/代表整個web項目,即:http://localhost:8080/ --><form action="/testPath/PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---以/開頭的相對地址"></form><!-- 3.不以/開頭的相對地址 --><!-- 不以/開頭的相對路徑是相對于當前資源的路徑 此時form.jsp的地址為:http://localhost:8080/testPath/jsp/form.jsp 所以當前資源路徑為:http://localhost:8080/testPath/jsp 而要提交的Servlet的路徑為Http://localhost:8080/testPath/PathServlet 所以路徑為當前路徑的上一級路徑下 即路徑為:../PathServlet 注:.代表當前路徑 ..代表當前路徑的上一級路徑 --><form action="../PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---不以/開頭的相對地址"></form>
</body>
</html>
3.重定向
package cn.test.path;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** @author Guozhen_Zhao* 創建時間:2018年3月17日 上午10:12:21* 備注:重定向有三種路徑書寫方式 : * 1. 絕對路徑 * 2. 以"/"開頭的相對路徑 * 3. 不以"/"開頭的相對路徑 */
@WebServlet("/RedirectServlet")
public class RedirectServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.sendRedirect("http://localhost:8080/testPath/jsp/b.jsp");/* * 2.以"/"開頭的相對路徑 * 此時,/代表整個web工程的路徑,即http://localhost:8080/ */// response.sendRedirect("/testPath/jsp/b.jsp"); /* * 3.不以"/"開頭的相對路徑 * 此時是相對于當前資源的相對路徑 * 當前資源路徑為:http://localhost:8080/testPath/RedirectServlet * 即表示:RedirectServlet在路徑http://localhost:8080/testPath之下 * 而b.jsp在http://localhost:8080/testPath/jsp/b.jsp * 所以最終地址寫為:jsp/b.jsp */// response.sendRedirect("jsp/b.jsp"); }protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
服務器端路徑
請求轉發
package cn.test.path;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** @author Guozhen_Zhao* 創建時間:2018年3月17日 上午10:09:59* 備注: 服務器端的路徑不能是絕對路徑,只能是相對路徑,也分為以/開頭和不以/開頭兩種 * 1.以"/"開頭的相對路徑 * 2.不以"/"開頭的相對路徑*/
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* * 1.以"/"開頭的相對路徑 * 此時,/代表當前web項目,即:http://localhost:8080/javaee */
// request.getRequestDispatcher("/jsp/b.jsp").forward(request, response); /* * 2.不以"/"開頭的相對路徑 * 相對于當前資源的相對路徑 * 此時,當前資源的路徑為:http://localhost:8080/javaee/DispatcherServlet * 所以要轉發去的資源的路徑以:http://localhost:8080/javaee開頭 */ request.getRequestDispatcher("jsp/b.jsp").forward(request, response); }protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
轉載于:https://blog.51cto.com/13416247/2087844