form中的action與<url-pattern>的理解

來源:互聯網
上載者:User

標籤: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>的理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.