標籤:webroot cti ons 路徑 send 應用程式 使用者 str 相對
一、<form action="Test/Login" method="post">
在action中有兩種表示方式:
1、"/Test/Login" 說明是相對於web伺服器的根目錄,可以理解為 http://localhost:8080/Test/Login
2、“Test/Login” 說明是相對於當前web應用程式的根目錄,可以理解為 http://localhost:8080/項目名稱/Test/Login
二、@WebServlet(name="Login",urlPatterns={"/Test/Login"})
urlPatterns與<url-pattern>的作用相同
urlPatterns代表了servlet的路徑,即當URI為:http://localhost:8080/項目名稱/Test/Login?name=""時,web容器會調用名字為Login的servlet為使用者提供服務。
所以說action是與urlPatterns相對應的,即action中的URL需要填寫urlPatterns中的URL。
再額外記錄幾點:
1、當html在webroot/html檔案夾時,action中的URL需要為“../Test/Login”表示將路徑改到"http://localhost:8080/項目名稱/"下,否則URL將為“http://localhost:8080/項目名稱/html/Test/Login”
2、當urlPatterns={"/Test/Login"}時,如果要在其修飾的servlet中使用response.sendRedirect(url),URL會為http://localhost:8080/項目名稱/Test/url,即在Test/下添加了url。這是就需要根據實際情況,令url="../"+url。
form中的action與<url-pattern>的理解