JSP資料互動習題錯誤總結

來源:互聯網
上載者:User

標籤:des   style   http   java   os   資料   io   2014   

1:如果註冊完頁面有中文字元需要在提交後的頁面顯示註冊資訊,切記先把接受到的request的編碼方式改為中文:request.setCharacterEncoding("utf-8");
不然會出現亂碼
2:提交表單的時候,錯誤的把事件綁定到了submit按鈕上了,應該綁定到表單標籤,擷取表單資訊!

因為跳轉後的頁面需要顯示上個註冊頁面的資訊,用request.getParameter(String name);這個方法的作用是擷取上個請求頁面的所有提交的表單資訊, 這個方法只能擷取有name屬性的標籤值。 radio單選框最終選的是一個值,雖然性別選項的name值都是gender,但是我們最終擷取的是一個選項,所以還可以用request.getParameter()方法。 checkbox複選框裡因為可以選多個值所以要用request.getParameterValues()方法擷取一個數組(複選框的所有name值要一致,複選框一定要設定value屬性值要不然取到的值全是on,因為取不到框外的值,value和框外值要一致),然後遍曆數組取值。

還有一個下拉框<select name="choice"><option>1</option> <option>1</option> <option>1</option></select>這裡因為最終也是只取一個下拉框選項所以最終也只取一個值用request.getParameter(),但是option標籤裡沒有name屬性,不能亂加,只能在select標籤裡給一個name屬性,取到選中值! 如:

MyEclipse建立的項目
***********************
1:index.jsp
*****************
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP ‘index.jsp‘ starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 <script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
 <script type="text/javascript">
   function mycheck(){
          var inputs=$("#year").val();
          var reg=/^(19[5-9][0-9])|20[0-1][0-4]$/;
          if(inputs==""){
           alert("年份不可為空");
           //$("#year").val(2014);
           return false;
              }
          else if(!reg.test(inputs)){
           //$("#year").val(2014);
               alert("必須輸入1950-2014之內的正整數");
               return false;
              }
          return true;
    }
   $(function(){
   
          $("#year").css("width","80px");
          //年份Regex
          $("#year").blur(function(){
                mycheck();
              })
          $("#tijiao").submit(function(){
               return mycheck();
              })
    //自動改變月份日分
     $("#one").change(function(){
      var $num=$("#one option:selected").text();
                   if($num==1|$num==3|$num==5|$num==7|$num==9|$num==10|$num==12){
                       var str="";
                        for(var i=1;i<=31;i++){
                         str+="<option>"+i+"</option>";
                        }
                        $("#two").html(str);
                       }else if($num=="請選擇"){
                               $("#two").html("請選擇");
                           }else if($num==2){
                               var sts="";
                               for(var i=1;i<=28;i++){
                                   sts+="<option>"+i+"<option>";
                                   }
                               $("#two").html(sts);
                           }else{
                           var strs="";
                        for(var i=1;i<=30;i++){
                                strs+="<option>"+i+"<option>";
                              }
                        $("#two").html(strs);
                           }
              })
    })
 </script>
  </head>

  <body>
     <form id="tijiao" action="welcome.jsp"  method="post">
       <table>
          <tr>
            <td>使用者名稱:</td>
            <td><input type="text" name="txtname"/>只能輸入字母或數字,4-16個字元</td>
          </tr>
         
          <tr>
            <td>密碼:</td>
            <td><input type="password" name="txtpwd"/>密碼長度6-12位</td>
          </tr>
         
          <tr>
            <td>確認密碼:</td>
            <td><input type="password" name="txtrepwd"/></td>
          </tr>
         
          <tr>
            <td>性別:</td>
            <td>
            <input type="radio" name="gender" value="男" checked="checked"/>男
            <input type="radio" name="gender" value="女"/>女
            </td>
          </tr>
         
          <tr>
            <td>電子郵件地址:</td>
            <td><input type="text" name="txtemail"/>輸入正確的Email地址</td>
          </tr>
         
          <tr>
            <td>出生日期:</td>
            <td>
            <input id="year" type="text" name="txtyear"/>年
            <select name=‘month‘ id="one">
            <option >請選擇</option>
               <%  
                 for(int i=1;i<=12;i++){
                  out.print("<option id=‘+i+‘>"+i+"</option>");
                 }
               %>
            </select>月
             <select name="day" id="two">
               <option>請選擇</option>
             </select>日
            </td>          
          </tr>
         <tr>
            <td><input type="submit" id="txtagree" value="同意以下協議條款並提交"/></td>
            <td><textarea rows="10" cols="30" >一、總則
  速度加快薩拉寬頻阿斯頓has機會打撒時間大時代阿斯頓阿大使館的哈市國家打掃打掃打掃過的愛上大事大家</textarea></td>
          </tr>
       </table>
     </form>
  </body>
</html>

2.welcome.jsp
********************
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">   
    <title>My JSP ‘welcome.jsp‘ starting page</title>  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>

  <body>
    歡迎觀臨,您的註冊資訊為:
    <table>
       <tr>
         <td>使用者名稱:</td>
         <%request.setCharacterEncoding("utf-8"); %>
         <td><%=request.getParameter("txtname") %></td>
       </tr>
      
       <tr>
         <td>密碼:</td>
         <td><%=request.getParameter("txtpwd") %></td>
       </tr>
      
       <tr>
         <td>性別:</td>
         <td><%=request.getParameter("gender") %></td>
       </tr>
      
       <tr>
         <td>電子郵件地址:</td>
         <td><%=request.getParameter("txtemail") %></td>
       </tr>
      
       <tr>
         <td>出生日期:</td>
         <td><%=request.getParameter("txtyear")+"年"+request.getParameter("month")+"月"+request.getParameter("day")+"日"
          %>
          </td>
       </tr>
    </table>
  </body>
</html>

聯繫我們

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