JSP簡單練習-擷取表單資料,jsp擷取表單資料

來源:互聯網
上載者:User

JSP簡單練習-擷取表單資料,jsp擷取表單資料

在JSP中,伺服器端程式與用戶端互動最常用的方法就是採用表單提交資料。表單提交的方法主要有兩種,一種是get方法,另一種是post方法,兩者最大的區別:使用get方法提交的資料會顯示在瀏覽器的地址欄中,而post方法則不會顯示,故post方法更為常用。表單中提交的資料可以是文字框、列表框及文本地區等。使用request對象的getParameter()方法可得到表單中相應資料項目的值。

以下為“獲得表單資料”的代碼:

<!-- userRegist2.jsp --><%@ page contentType="text/html;charset=gb2312" %><script language="javascript">   function on_submit()  // 驗證資料的合法性   {      if(form1.username.value=="")      {         alert("使用者名稱不可為空,請輸入使用者名稱!");         form1.username.focus();         return false;      }      if(form1.userpassword.value=="")      {         alert("使用者密碼不可為空,請輸入密碼!");         form1.userpassword.focus();         return false;      }      if(form1.reuserpassword.value=="")      {         alert("使用者確認密碼不可為空,請輸入密碼!");         form1.reuserpassword.focus();         return false;      }      if(form1.userpassword.value!=form1.reuserpassword.value)      {         alert("密碼與確認密碼不同");         form1.userpassword.focus();         return false;      }      if(form1.email.value.length!=0)      {         for(i=0;i<form1.email.value.length;i++)         {            if(form1.email.value.charAt(i)=='@')            {               break;            }         }         if(i==form1.email.value.length)         {            alert("非法E-mail地址!");            form1.email.focus();            return false;         }      }      else      {         alert("請輸入E-mail!");         form1.email.focus();         return false;      }   }</script><html><head><title>新使用者註冊</title></head><body><form method="POST" action="acceptUserRegist.jsp" name="form1" onsubmit="return on_submit()">新使用者註冊<br>使用者名稱(*):<input type="text" name="username" size="20"><br>密 碼(*):<input type="password" name="userpassword" size="20"><br>再輸一次密碼(*):<input type="password" name="reuserpassword" size="20"><br>性別:<input type="radio" value="男" checked name="sex">男<input type="radio" name="sex" value="女">女<br>出生年月:<input name="year" size="4" maxlength=4>年         <select name="month">              <option value="1" selected>1</option>              <option value="2">2</option>              <option value="3">3</option>              <option value="4">4</option>              <option value="5">5</option>              <option value="6">6</option>              <option value="7">7</option>              <option value="8">8</option>              <option value="9">9</option>              <option value="10">10</option>              <option value="11">11</option>              <option value="12">12</option>         </select>月         <input name="day" size="3" maxlength=4>日<br>電子郵箱(*):<input name="E-mail" maxlength=28><br>家庭住址:<input type="text" name="address" size="20"><br><input type="submit" value="提交" name="B1"><input type="reset" value="全部重寫" name="B2"><br></form></body></html>
<!-- acceptUserRegist.jsp --><%@ page contentType="text/html; charset=gb2312" %><%!   public String codeToString(String str)   {        String s=str;        try{             /* 下面需注意:不能使用gb2312,需使用ISO-8859-1 */             byte tempB[]=s.getBytes("ISO-8859-1");             s=new String(tempB);             return s;        }catch(Exception e)        {             return s;        }   }%><html><head><title>接收新使用者註冊</title></head><body>這是新使用者註冊所提交的資料:<br>使用者名稱是:<%=codeToString(request.getParameter("username")) %><br>密碼是:<%=codeToString(request.getParameter("userpassword")) %><br>性別是:<%=codeToString(request.getParameter("sex")) %><br>出生年月:<%=request.getParameter("year")+request.getParameter("month")+request.getParameter("day") %><br>電子郵箱:<%=request.getParameter("E-mail") %><br>家庭住址:<%=codeToString(request.getParameter("address")) %><br></body></html>


jsp表單資料插入資料庫的問題(具體見補充)

估計你可能是剛學的jsp。
sqlstr="insert into "+tableName+" values('id','name','age')";
你的這個字串變成sql語句時為:insert into student values("id","name","age");
很明顯你的sql語句執行後肯定是把id,name,age三個字串插入到了資料庫中,因為你根本就沒有傳入你輸入表單中的值。
我建議你在提交表單資料到index.jsp頁面後,在index頁面中取出你提交的三個值,然後把他們設定在<jsp:setProperty name="student" property="id" value=""/>、<jsp:setProperty name="student" property="name" value=""/>、
<jsp:setProperty name="student" property="age" value=""/>三個jsp標籤的對應的value中,這樣你在用student調用insert()方法,但是你的insert中要設定參數如insert(Student student),然後再將sqlstr的值改變一下:sqlstr = "insert into "+tablename+" values("+student.getId()+","+student.getName()+","+student.getAge()+")";
我大致看了一下你的代碼,改這些東西應該沒有問題了。
 
JSP中擷取表單資料的方法,列舉2鐘以上並解釋

第一種:
在form標籤中有一個輸入框
<input type="text" name="name"/>
後台用 String name = request.getParameter("name");來擷取輸入框的值

第二種:
url傳值的方法
<form action="login.do?name=值" method="post">;
<a href="login.do?name=值"></a>
一般情況下使用button或者超連結的方法提交表單的時候用,或者輸入框不在form中使用
 

聯繫我們

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