刷新操作页面时如何停止将数据重新提交到数据库中

时间:2013-07-29 03:54:57

标签: jsp

我的jsp项目有问题。当我提交表单时,数据被插入到数据库中,并向用户显示自动生成的用户ID和密码。但是当我单击刷新按钮时,将使用新的用户标识和密码重新提交相同的数据。 表格和插入的代码如下。

stinsrt.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>STUDENT REGISTRATION</title>
         <%@ include file="connect.jsp" %>
         <script>
    window.history.forward();
    </script>
    <script language="javascript">

</script>


    </head>
            <center><img border="0" src="Student Info Systems.jpg" alt="Pulpit rock" width="304" height="228" /></center>
    <body>
        <%@ page language="java" import="java.sql.* , java.io.*" %>   
          <%! String str1,str2,str3,str4,str5,str6,str7,str8,str10,sid,tsid,date,month,year,day;
          int max;%>
        <% 

        try
        {

        str1=request.getParameter("fname");
        str2=request.getParameter("lname");
        str3=request.getParameter("faname");
        str5=request.getParameter("phno");
        str6=request.getParameter("email");
        str7=request.getParameter("gend");

        month=request.getParameter("DateOfBirth_Month");
        day=request.getParameter("DateOfBirth_Day");
        year=request.getParameter("DateOfBirth_Year");
        int str9=Integer.parseInt(request.getParameter("Department"));
        str10=request.getParameter("add");
        str8=day+"-"+month+"-"+year;

        //out.println(str1+"-"+str2+"-"+str3+"-"+str5+"-"+str6+"-"+str7+"-"+str8+"-"+str9+"-"+str10);
        if(str1.length()!=0&&str2.length()!=0&&str3.length()!=0
            &&str6.length()!=0&&str7.length()!=0&&str8.length()!=0&&str10.length()!=0)  
            {
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select max(id) from student");
            if(rs.next())
                sid=rs.getString(1);
            if(sid==null)
                sid="S100";
            tsid=sid.substring(1,sid.length());
            max=Integer.parseInt(tsid);
            max++;
            sid="S"+max;




            PreparedStatement pst=con.prepareStatement("INSERT INTO student values(?,?,?,?,?,?,?,?,?,?)");
            pst.setString(1,sid);
            pst.setString(2,str1);
            pst.setString(3,str2);
            pst.setString(4,str3);
            pst.setString(5,str5);
            pst.setString(6,str6);
            pst.setString(7,str7);
            pst.setString(8,str8);
            pst.setInt(9,str9);
            pst.setString(10,str10);
            int k=pst.executeUpdate();

            %>
            <b><h2><center>Student Registration Successful...</center></h2></b>
            <h2> Student Id is  <% out.println(sid);%> </h2>
            <h3> Default Password is
            <% 
            String str="";
            for(int i=0;i<=8;i++)
            {
            str=str+(char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1));
            }
            out.println(str);
            PreparedStatement dst=con.prepareStatement("INSERT INTO LOGIN(USER_ID,PASSWORD,ROLE,COUNT) VALUES(?,?,?,?)");
            dst.setString(1,sid);
            dst.setString(2,str);
            dst.setString(3,"STUDENT");
            dst.setInt(4,0);
            dst.executeUpdate();
            %>
            <br>
            <a href="adminhome.html">Back</a>
        <% pst.close();
            con.close(); }
            else
            {
            out.println("<b>Incomplete Student Information</b>");
            }

        }
        catch(Exception e)
        {
        out.println("Error:"+e);
        }%>

    </body>
</html>

学生注册表

<html>
    <head>
    <title>Student Registration</title>
    <script>
    window.history.forward();
    </script>

    <script>

            function validate()
            {
            formObj=document.f;
            for(var i=0;i<formObj.length;i++)
            {
            if(formObj.elements[i].value==""||formObj.elements[i].value=="Invalid")
            {
            alert("All fields must be completed with valid inormation for submission");
            formObj.elements[i].focus();
            return false;
            }
            }
       //Name
       var fname=document.f.Firstname;
       var nameexp=/[^a-zA-Z]$/;
     res=nameexp.test(fname.value);
     //alert(res);
       if(res)
       {
       alert("Enter Alphabets only for First Name");
       fname.select();
       return false;
       }

       var lname=document.f.Lastname;
       res=nameexp.test(lname.value);
       //alert(res);
       if(res)
       {
       alert("Enter Alphabets only for Last Name");
       lname.select();
       return false;
       }  

    //Address
                   var Address=document.f.Address.value;
                   var addxp=/[^a-zA-Z0-9]$/;
                  // alert(addxp.test(address));
                   if(addxp.test(address))
                   {
                   alert("check address");
                   return false;
                   }




               var mexp=/^(7|8|9)\d{9}$/;
               res=mexp.test(phoneno);
               if(!res)
               {
               alert("phoneno must start with 7,8 or 9");
               return false;
               }
                   //email ID
                   var emailid=document.f.emailid.value;
               var mailxp=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
               if(!mailxp.test(emailid))
               {
               alert("check emailid");
               return false;
               }
                   }

    </script>                   







</head>
            <center><img border="0" src="Student Info Systems.jpg" alt="Pulpit rock" width="304" height="228" /></center><body  text="blue">
        <form action="stinsrt.jsp" method="post" name="f" onSubmit="return validate();">
        <center><h2>STUDENT REGISTRATION</h2>
        <pre>
        <table width="50%">
        <tr><td> Firstname:</td><td><input type="text" name="fname" size="15"/></td></tr>
        <tr><td> Lastname:</td><td><input type="text" name="lname" size="15"/></td></tr>
        <tr><td> Guardian/Parent Name:</td><td><input type="text" name="faname" size="15"/></td></tr>
        <tr><td>D.O.B:</td><td><select name="DateOfBirth_Month">

<option> - Month - </option>

<option value="Jan">Jan</option>

<option value="Feb">Feb</option>

<option value="Mar">Mar</option>

<option value="Apr">Apr</option>

<option value="May">May</option>

<option value="Jun">Jun</option>

<option value="Jul">Jul</option>

<option value="Aug">Aug</option>

<option value="Sep">Sep</option>

<option value="Oct">Oct</option>

<option value="Nov">Nov</option>

<option value="Dec">Dec</option>

</select>

<select name="DateOfBirth_Day">

<option> - Day - </option>

<option value="1">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>

<option value="13">13</option>

<option value="14">14</option>

<option value="15">15</option>

<option value="16">16</option>

<option value="17">17</option>

<option value="18">18</option>

<option value="19">19</option>

<option value="20">20</option>

<option value="21">21</option>

<option value="22">22</option>

<option value="23">23</option>

<option value="24">24</option>

<option value="25">25</option>

<option value="26">26</option>

<option value="27">27</option>

<option value="28">28</option>

<option value="29">29</option>

<option value="30">30</option>

<option value="31">31</option>

</select>



<select name="DateOfBirth_Year">

<option> - Year - </option>

<option value="04">2004</option>

<option value="03">2003</option>

<option value="02">2002</option>

<option value="01">2001</option>

<option value="00">2000</option>

<option value="99">1999</option>

<option value="98">1998</option>

<option value="97">1997</option>

<option value="96">1996</option>

<option value="95">1995</option>

<option value="94">1994</option>

<option value="93">1993</option>

<option value="92">1992</option>

<option value="91">1991</option>

<option value="90">1990</option>

<option value="89">1989</option>

<option value="88">1988</option>

<option value="87">1987</option>

<option value="86">1986</option>

<option value="84">1984</option>

<option value="83">1983</option>

<option value="82">1982</option>

<option value="81">1981</option>

<option value="80">1980</option>

<option value="79">1979</option>

<option value="78">1978</option>

<option value="77">1977</option>

<option value="76">1976</option>

<option value="75">1975</option>

<option value="74">1974</option>

<option value="73">1973</option>

<option value="72">1972</option>

<option value="71">1971</option>

<option value="70">1970</option>

<option value="69">1969</option>

<option value="68">1968</option>

<option value="67">1967</option>

<option value="66">1966</option>

<option value="65">1965</option>

<option value="64">1964</option>

<option value="63">1963</option>

<option value="62">1962</option>

<option value="61">1961</option>

<option value="60">1960</option>

<option value="59">1959</option>

<option value="58">1958</option>

<option value="57">1957</option>

<option value="56">1956</option>

<option value="55">1955</option>

<option value="54">1954</option>

<option value="53">1953</option>

<option value="52">1952</option>

<option value="51">1951</option>

<option value="50">1950</option>

<option value="49">1949</option>

<option value="48">1948</option>

<option value="47">1947</option>

<option value="46">1946</option>

<option value="45">1945</option>

<option value="44">1944</option>

<option value="43">1943</option>

<option value="42">1942</option>

<option value="41">1941</option>

<option value="40">1940</option>

<option value="39">1939</option>

<option value="38">1938</option>

<option value="37">1937</option>

<option value="36">1936</option>

<option value="35">1935</option>

<option value="34">1934</option>

<option value="33">1933</option>

<option value="32">1932</option>

<option value="31">1931</option>

<option value="30">1930</option>

<option value="29">1929</option>

<option value="28">1928</option>

<option value="27">1927</option>

<option value="26">1926</option>

<option value="25">1925</option>

<option value="24">1924</option>

<option value="23">1923</option>

<option value="22">1922</option>

<option value="21">1921</option>

<option value="20">1920</option>

<option value="19">1919</option>

<option value="18">1918</option>

<option value="17">1917</option>

<option value="16">1916</option>

<option value="15">1915</option>

<option value="14">1914</option>

<option value="13">1913</option>

<option value="12">1912</option>

<option value="11">1911</option>

<option value="10">1910</option>

<option value="09">1909</option>

<option value="08">1908</option>

<option value="07">1907</option>

<option value="06">1906</option>

<option value="05">1905</option>

<option value="04">1904</option>

<option value="03">1903</option>

<option value="02">1902</option>

<option value="01">1901</option>

<option value="00">1900</option>

</select></td></tr></tr>
        <tr><td>Gender:</td><td><input type="radio" name="gend" value="male"/>Male  <input type="radio" name="gend" value="female"/>Female</td></tr>
        <tr><td>Phone no:</td><td><input type="text" name="phno" size="15"/></td></tr>
        <tr><td>email ID:</td><td><input type="text" name="email" size="15"></td></tr>
        <tr><td>Department:</td><td><select name="Department" >
                            <option value="1">CSE</option>
                            <option value="2">ECE</option>
                            <option value="3">EEE</option>
                            <option value="4">MECH</option>
                            <option value="5">MBA</option>
                            </select></td></tr></td></tr>
        <tr><td>Address:</td><td><textarea name="add" rows="4" cols="20"  ></textarea></td></tr>



        <tr><td><input type="submit" name="sub" value="submit">
        <input type="button" name="cancel" value="Cancel" onclick="window.location.replace('adminhome.html')"></td></t>
        </table>
        </pre>
        </form>
    </body>
</html>
请帮助我。 谢谢。

1 个答案:

答案 0 :(得分:0)

清除表格对象字段中的数据“之后”通过循环将其插入。 如果用户需要知道他们在返回的页面中插入了什么,请使用HTML普通文本元素而不是表单来写入,或者至少从表单标记中删除表单请求URL。