Form表單中的action路徑問題,form表單action路徑
www.BkJia.Com 網友分享於: 2014-08-14 08:08:01 瀏覽數44525次
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讀取,一行一行分析.