2014-02-13 24 views
0

考慮一個jsp以下表單標籤:路徑的形式標記操作屬性

<form action="/sampleServlet" method="get"> 

是什麼

<form action="/sampleServlet" method="get"> 

<form action="sampleServlet" method="get"> <!--no leading slash--> 
+0

的事情是,如果你使用/ sampleServlet,那麼它會使用currentpath +/sampleServlet,因此呼叫將轉到您的應用程序中映射的servlet之一。如果僅使用sampleServlet,那麼url本身將更改爲http:// sampleServlet,它無效並導致錯誤 –

+0

您的評論與Alexandre在http://stackoverflow.com/questions/16683877/form中給出的答案相抵觸-action-sampleservlet-給-ME-exception' –

回答

0

代碼<form action="/sampleServlet" method="get">之間的差異將提交表單並調用servlet的doGet()在web.xml中使用alias /sampleServlet映射

在以後的情況下,提交會拋出錯誤(可能是404)。

0

/sampleServlet - 絕對路徑

此路徑是絕對的基礎URL(協議,IP(或主機名)和端口)

current page: http://127.0.0.1:8080/context/test 

target page: http://127.0.0.1:8080/sampleServlet 

sampleServlet - 相對路徑

這個路徑是相對於當前頁面的路徑,例如

current page: http://127.0.0.1:8080/context/test 

target page: http://127.0.0.1:8080/context/test/sampleServlet 

在JSP中,你應該使用絕對路徑,但記得要自動添加上下文路徑和思考URL重寫(如有必要會話ID添加到URL)。

當使用JSTL使用<工作c:url值= 「/ sampleServlet」/ >:

<form action="<c:url value="/sampleServlet"/>" method="get"> 
... 
</form>