Form表單中的action路徑問題,form表單action路徑

來源:互聯網
上載者:User

Form表單中的action路徑問題,form表單action路徑
今天剛接觸web,在用jsp和servlet做一個簡單的登陸的時候在Form表單action屬性和method屬性的一些問題; 


我遇到的是Form表單提交到servelet處理時遇到的問題: 
(1)<form name="login" action="①?" method=“②?”>

                //表單內容

username:<input type = "text" name = "username"> <br>
password:<input type = "password" name = "pwd"> <br>
<input type = "submit">
</form> 

(2)對應的處理使用者請求的servlet類為HelloServlet.java; 


(3)配置web.xml檔案: 
<servlet> 
<servlet-name>③servlet</servlet-name> 
<servlet-class>/HelloServlet</servlet-class> 
</servlet> 

<servlet-mapping> 
<servlet-name>③servlet</servlet-name> 
<url-pattern>/①welcome</url-pattern> 
</servlet-mapping> 

然後在login.jsp中應該是: 

<form action="①welcome" method="②?"> 

這樣的話login.jsp的url是http://localhost:8080/jsp/login.jsp 

而HelloServlet.java的url是http://localhost:8080/jsp/welcome 


在HelloServlet中可以取值

                String name = "";
String pwd = "";
name = req.getParameter("username");
pwd = req.getParameter("pwd");


注:web.xml中③對應的兩個servlet-name要一致;①中的url-pattern要與form表單中的action屬性值一致 ;③ method方法預設是get方法,但是這種方法會將值暴露在瀏覽器上,所以一般使用的是post方法,隱藏值內容;




對於java中form表單action路徑問題

一般action的路徑配置你應該在struts.xml中給package加一個namespace,
然後讓namespace的值和jsp檔案夾的名字一致。
那麼在寫form 的action路徑的時候,你就可以直接寫相對路徑了。例如
<form method="post" action="add_Emp.action">
而看到樓主action的請求有_ 應該是在struts.xml的action 中用了萬用字元吧
<action name="add_* " class="略" method="{1}">這樣就是配置的通常用法
希望樓主能用的開心
 
在action中怎得到form表單file類型的檔案路徑

<form action="" method="post">
<input type="file" name="file1"/>
</form>
request.getParameter("file1");

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file1"/>
</form>
ServletInputStream sis = request.getInputStream();
用sis讀取,一行一行分析.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.