JSP表單提交與接收

JSP表單提交與接收javascript

在Myeclipse中新建web project,在webroot中新建userRegist1.jsp,代碼以下html

<%@ 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="acceptUserRegist1.jsp" name="form1" onsubmit="return on_submit()">
  新用戶註冊<br>
  用戶名(*):<input type="text" name="username" size="20"><br>&nbsp; 碼(*):<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="8">7</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>

(代碼中javascript解決邏輯判斷問題,保證輸入格式的正確性)java

在瀏覽器輸入地址,運行,而後輸入相關參數:web

而後在webroot中新建acceptUserRegist1.jsp,用於接收相關內容,代碼以下:瀏覽器

<%@ page contentType="text/html;charset=gb2312"%>
<%!
public String codeToString(String str){//處理中文字符串的函數
  String s=str;
  try{
    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>

運行結果以下:eclipse

 

 參考資料《java web輕量級開發全體驗》jsp

相關文章
相關標籤/搜索