javaweb学习中的路径问题
生活随笔
收集整理的這篇文章主要介紹了
javaweb学习中的路径问题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 項(xiàng)目結(jié)構(gòu)
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>頁(yè)面a</title> </head> <body><!-- 超鏈接和表單都有三種書(shū)寫(xiě)路徑的方式 1,絕對(duì)地址 2,以"/"開(kāi)頭的相對(duì)地址 3,不以"/"開(kāi)頭的相對(duì)地址 --><!-- 1.絕對(duì)地址 --><!-- 完整的URL --><a href="http://localhost:8080/testPath/jsp/b.jsp">這是絕對(duì)地址超鏈接</a><br /><!-- 2.以"/"開(kāi)頭的相對(duì)地址 --><!-- /代表了整個(gè)web項(xiàng)目,即:http://localhost:8080/ --><a href="/testPath/jsp/b.jsp">這是以"/"開(kāi)頭的相對(duì)地址超鏈接</a><br /><!-- 3.不以"/"開(kāi)頭的相對(duì)地址 --><!-- 不以/開(kāi)頭,則相對(duì)于當(dāng)前資源的路徑 當(dāng)前資源的路徑為:http://localhost:8080/testPath/jsp/ 而b.jsp也在此路徑下 所以直接書(shū)寫(xiě)b.jsp --><a href="b.jsp">這是不以"/"開(kāi)頭的相對(duì)地址超鏈接</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>頁(yè)面b</title> </head> <body><!-- 所有的表單都是提交到b.jsp --><!-- 表單提交路徑有三種書(shū)寫(xiě)方式 1,絕對(duì)地址 2,以"/"開(kāi)頭的相對(duì)地址 3,不以"/"開(kāi)頭的相對(duì)地址 --><form action="http://localhost:8080/testPath/jsp/b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---絕對(duì)地址 "></form><!-- 以/開(kāi)頭的相對(duì)地址,此時(shí)的/代表整個(gè)web項(xiàng)目,即:http://localhost:8080/ --><form action="/testPath/jsp/b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---以/開(kāi)頭的相對(duì)地址"></form><form action="b.jsp" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="提交---不以/開(kāi)頭的相對(duì)地址 "></form><!-- 表單提交到Servlet --><!-- 表單提交到Servlet有三種書(shū)寫(xiě)方式 1,絕對(duì)路徑 2,以"/"開(kāi)頭的相對(duì)地址 3,不以"/"開(kāi)頭的相對(duì)地址 --><!-- 1.絕對(duì)地址 --><!-- 完整的URL --><form action="http://localhost:8080/testPath/PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---絕對(duì)地址"></form><!-- 2.以/開(kāi)頭的相對(duì)地址 --><!-- 此時(shí)的/代表整個(gè)web項(xiàng)目,即:http://localhost:8080/ --><form action="/testPath/PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---以/開(kāi)頭的相對(duì)地址"></form><!-- 3.不以/開(kāi)頭的相對(duì)地址 --><!-- 不以/開(kāi)頭的相對(duì)路徑是相對(duì)于當(dāng)前資源的路徑 此時(shí)form.jsp的地址為:http://localhost:8080/testPath/jsp/form.jsp 所以當(dāng)前資源路徑為:http://localhost:8080/testPath/jsp 而要提交的Servlet的路徑為Http://localhost:8080/testPath/PathServlet 所以路徑為當(dāng)前路徑的上一級(jí)路徑下 即路徑為:../PathServlet 注:.代表當(dāng)前路徑 ..代表當(dāng)前路徑的上一級(jí)路徑 --><form action="../PathServlet" methoe="get">username:<input type="text" name="username" value=""> <inputtype="submit" value="表單提交到Servlet---不以/開(kāi)頭的相對(duì)地址"></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* 創(chuàng)建時(shí)間:2018年3月17日 上午10:12:21* 備注:重定向有三種路徑書(shū)寫(xiě)方式 : * 1. 絕對(duì)路徑 * 2. 以"/"開(kāi)頭的相對(duì)路徑 * 3. 不以"/"開(kāi)頭的相對(duì)路徑 */ @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.以"/"開(kāi)頭的相對(duì)路徑 * 此時(shí),/代表整個(gè)web工程的路徑,即http://localhost:8080/ */// response.sendRedirect("/testPath/jsp/b.jsp"); /* * 3.不以"/"開(kāi)頭的相對(duì)路徑 * 此時(shí)是相對(duì)于當(dāng)前資源的相對(duì)路徑 * 當(dāng)前資源路徑為:http://localhost:8080/testPath/RedirectServlet * 即表示:RedirectServlet在路徑http://localhost:8080/testPath之下 * 而b.jsp在http://localhost:8080/testPath/jsp/b.jsp * 所以最終地址寫(xiě)為:jsp/b.jsp */// response.sendRedirect("jsp/b.jsp"); }protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}服務(wù)器端路徑
請(qǐng)求轉(zhuǎn)發(fā)
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* 創(chuàng)建時(shí)間:2018年3月17日 上午10:09:59* 備注: 服務(wù)器端的路徑不能是絕對(duì)路徑,只能是相對(duì)路徑,也分為以/開(kāi)頭和不以/開(kāi)頭兩種 * 1.以"/"開(kāi)頭的相對(duì)路徑 * 2.不以"/"開(kāi)頭的相對(duì)路徑*/ @WebServlet("/DispatcherServlet") public class DispatcherServlet extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {/* * 1.以"/"開(kāi)頭的相對(duì)路徑 * 此時(shí),/代表當(dāng)前web項(xiàng)目,即:http://localhost:8080/javaee */ // request.getRequestDispatcher("/jsp/b.jsp").forward(request, response); /* * 2.不以"/"開(kāi)頭的相對(duì)路徑 * 相對(duì)于當(dāng)前資源的相對(duì)路徑 * 此時(shí),當(dāng)前資源的路徑為:http://localhost:8080/javaee/DispatcherServlet * 所以要轉(zhuǎn)發(fā)去的資源的路徑以:http://localhost:8080/javaee開(kāi)頭 */ request.getRequestDispatcher("jsp/b.jsp").forward(request, response); }protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}轉(zhuǎn)載于:https://blog.51cto.com/13416247/2087844
總結(jié)
以上是生活随笔為你收集整理的javaweb学习中的路径问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ActiveMQ与spring整合
- 下一篇: 我为什么卸载了今日头条