1. 程式人生 > >關於springMVC和傳統servlet框架從jsp頁面向後臺請求url問題

關於springMVC和傳統servlet框架從jsp頁面向後臺請求url問題

經過實驗  發現

servlet和springmvc向後臺傳送請求都是根據request.getServletPath()來獲取請求的

如果jsp中沒有<base href="<%=basePath%>">則向後臺的請求會自動解析到專案根目錄下 加上以後會自動解析到檔案的當前目錄下
舉個例子


如上圖專案(兩個jsp內容一樣)

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>
<form action="login.action" method="post">
使用者名稱<input type="text" name="userName">
密碼<input type="text" name="password">
<input type="submit" value="submit">
</form>
</body>
</html>
Test是個servlet程式碼如下

package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("request.getServletPath()="+request.getServletPath());
		System.out.println("request.getContextPath()="+request.getContextPath());
		
	}

}
1.此時瀏覽器輸入http://localhost:8080/TestServlet/page/NewFile.jsp    
控制檯輸出

request.getServletPath()=/page/login.action
request.getContextPath()=/TestServlet

2.瀏覽器輸入http://localhost:8080/TestServlet/page/a/NewFile.jsp    
控制檯輸出
request.getServletPath()=/page/a/login.action
request.getContextPath()=/TestServlet

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

如果在兩個jsp中加入basepath
jsp程式碼如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
使用者名稱<input type="text" name="userName">
密碼<input type="text" name="password">
<input type="submit" value="submit">
</form>
</body>
</html>
1.此時瀏覽器輸入http://localhost:8080/TestServlet/page/NewFile.jsp    

控制檯輸出
request.getServletPath()=/login.action
request.getContextPath()=/TestServlet


2.瀏覽器輸入http://localhost:8080/TestServlet/page/a/NewFile.jsp    
控制檯輸出
request.getServletPath()=/login.action
request.getContextPath()=/TestServlet




通過以上實驗可以看出request.getServletPath()在jsp中加入basepath的時候會直接獲取到請求   如果不加會從專案根目錄開始獲取請求路徑

在springMVC中是一樣的道理 springMVC實際也是通過request.getServletPath()獲取請求的





springMVC和servlet都是根據這種獲取的請求並解析  所以當用springmvc傳送請求報404的時候後臺報no mapping時可能會是這個原因(請求路徑不對應)



ps:這個basePath程式碼
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>" />