在Java開發(fā)中路徑是一個無法避免的問題,筆者在多次遇到這樣的問題之后打算寫一篇博客來總結(jié)一下。
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 Page</h4> <a href="path/b.jsp">To B page</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 Page</h4> <a href="c.jsp">To C Page</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 Page</h4> <a href="../a.jsp">To A Page</a></body></html>三個jsp中的代碼很簡單,就是超鏈接的路徑寫的是相對路徑,不通過Servelt可以正常訪問。但是通過Servlet,寫一個TestServlet的代碼如下:
package com.lsy.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 TestServlet */@WebServlet("/testServlet")public class TestServlet 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 { //模擬從數(shù)據(jù)庫中查詢數(shù)據(jù) List<String> cities = Arrays.asList("北京","上海","廣州"); request.setAttribute("cities", cities); //通過轉(zhuǎn)發(fā)方式響應 request.getRequestDispatcher("/path/b.jsp").forward(request, response); }}在測試中可以發(fā)現(xiàn)從a.jsp通過Servlet可以正常訪問b.jsp,但是b.jsp則無法通過相對路徑訪問c.jsp,可以發(fā)現(xiàn)相對路徑帶來的問題。在通過Servlet訪問b.jsp時網(wǎng)頁的路徑變?yōu)樵擁椖肯耯ttp://localhost:8080/session_Learn/testServlet,由b.jsp到c.jsp,jsp頁面在path路徑下,就出現(xiàn)了問題。 a.jsp->/Servlet -轉(zhuǎn)發(fā)->b.jsp(有一個超鏈接:和b.jsp在同一路徑下的c.jsp)->無法得到頁面。
2)編寫絕對路徑可以避免上述問題 ①在javaweb中什么是“絕對路徑”,即任何的路徑都要加上contextPath. 相對于當前WEB應用的根路徑的路徑,以當前項目為例,Session_Learn/a.jsp,a.jsp是相對于當前web應用的,則稱他是絕對路勁。Session_Learn其實是contextPath(當前web應用的上下文路徑) http://localhost:8080/testServlet 這個就不是 ②如何完成編寫: 1. 若/代表的是站點的根目錄, 則在其前面加上contextPath就可以了。<a href="testServlet">To B page</a>–><a href=<%=request.getContextPath()%>"testServlet">To B page</a>
3). JavaWeb中的/的特殊含義 ①.當前WEB應用的根路徑,若/需交由Servlet容器來處理:http://localhost:8080/contextPath/testServlet 1. 請求轉(zhuǎn)發(fā)時,request.getRequestDispatcher(“/path/b.jsp”).forward(request, response); 2. web.xml配置文件中映射Servlet訪問路徑: 3. 各種定制標簽中的/
②.當前WEB站點的根路徑http://localhost:8080/,若/交由瀏覽器解析 1. 超鏈接:<a href="testServlet">To B page</a> 2. 表達中的action:<form action="/login.jsp"> 3. 請求重定向的時候:response.sendRedirect(“/a.jsp”)
|
新聞熱點
疑難解答