1. 程式人生 > >java web中表單提交的方式

java web中表單提交的方式

      在java web中表單提交的幾種方法,本人才學疏淺,望大神幫我補充,

      第一種也是最簡單的方法就是在form 標籤中加上一個action,action對應的就是你要將表單提交的地址,其中按扭為submit,例如:

<form action="servlet/Denglu"method="post">
       使用者名稱:<input type="text" name="username"/><br/>
       密碼:<input type="password" name="keyword"/><br/>
      學號:<input type="num" name="num"/><br/>
     驗證碼:<input type="text" name="verifycode" size="3"/>
       <img id="verifyCode" src="/stucheck/servlet/VerifyCodeServlet" border="2"/>
       <a href="javascript:_change()">換一張</a>
       <br/>   
       <input type="submit" value="登入">
按下登入按鈕,則把使用者輸入的內容提交給Denglu的servlet進行處理

     第二種方法是採用button元件的onclick()函式進行提交,例如:

<form id="modify" style="display:none" action="servlet/XiuGai" method="post">
		      <%
                   String massage = " ";
                   String msg = (String)request.getAttribute("msg");
                   if(msg != null){
                        massage = msg;
                    }
                  %>
                  <font><%=massage %></font>
					<span id="user"><img src="images/agent.png" alt=""><input tabindex="1" type="text" 
					               placeholder="請輸入您的學號" name="user"></span>
					<span id="password"><img src="images/lock.png" alt=""><input tabindex="2" type="password" 
					                    placeholder="請輸入您要修改的密碼" name="password"></span>
					<span id="passwordone"><img src="images/lock.png" alt=""><input tabindex="2" type="password" 
					               placeholder="請確認您的新密碼" name="passwordone"></span>
       
					<div id="button" type="submit" onclick="word()"><a style="color:#fff;text-decoration: none;" 
					         href="servlet/XiuGai" >確定</a></div>
					
					<div id="close">取消</div>
		</form>
word()函式為:
function word(){
		document.getElementById("modify").submit();
		alert('您的密碼已經修改成功!');
		}  
則表單的資料提交給XiuGai的servlet進行處理。

       第三種方法是通過form的onsubmit()函式

<form name="reply"  method="post" onsubmit="return validateForm( );">  
        <input type="text" name="title"  size="80" /><br />  
        <textarea name="cont" cols="80" rows="12"></textarea><br />  
        <input type="submit" value="提交" >  
</form>  
其中onsubmit中一定要加上return,否則會一直執行,不會返回

按鈕的型別要為submit