【问题标题】:stop submitting of previous data of form on page reload to DB in jsp停止在jsp中将页面上表单的先前数据重新加载到DB
【发布时间】:2020-08-31 10:13:42
【问题描述】:

我已经完成了以下编码 ContactUs.jsp

<form action="contact1.jsp" method="post" id="contact_form">
                <div class="name">
                    <label for="name"></label> <input type="text" placeholder="My name is" name="name" id="name_input" required>
                </div>
                <div class="email">
                    <label for="email"></label> <input type="email"
                        placeholder="My e-mail is" name="email" id="email_input" required>
                </div>
                <div class="telephone">
                    <label for="name"></label>
                    <input type="text" placeholder="My number is" id="telephone_input" name="telephone" title="lenght of number should be 10 digits for valid number" pattern="[1-9]{1}[0-9]{9}" required/> 

                </div>

                <div class="message">
                    <label for="message"></label>
                    <textarea name="message" placeholder="I'd like to chat about"
                        id="message_input" cols="30" rows="5" required></textarea>
                </div>
                <div class="submit">
                    <input type="submit" value="Send Message" id="form_button" />
                </div>
            </form>

contact1.jsp 的代码是

String name=request.getParameter("name");
String email=request.getParameter("email");
String telephone=request.getParameter("telephone");
String message=request.getParameter("message");

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh", "Cinthiya@098");
Statement st=con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into contact values('"+ name + "','"+ email + "','"+ telephone + "','"+ message + "');");
if (i > 0) {
    request.getSession().setAttribute("Message", "Sent Successfully!");
    request.getRequestDispatcher("ContactUs.jsp").forward(request, response);
}
else{
    request.getSession().setAttribute("Message", "Incomplete Process!");
    request.getRequestDispatcher("ContactUs.jsp").forward(request, response);   
}

//close connection
%>

一旦我将数据添加到表单并提交它,这工作正常,但是当我点击刷新它重新提交数据时,我希望在我重定向响应时清除之前提交的数据到相同的 ContactUs.jsp ,如果可能请帮忙。

【问题讨论】:

    标签: java sql jsp refresh


    【解决方案1】:

    您可以使用 java 脚本 History.replaceState() 函数来解决此问题。

    History.replaceState() 方法修改当前历史记录条目,将其替换为方法参数中传递的 stateObj、title 和 URL。

    在jsp页面中添加这个脚本-

    <script>
        if ( window.history.replaceState ) {
            window.history.replaceState( null, null, window.location.href );
        }
    </script>
    

    或者, 如果提交数据不存在,则仅在表“用户”中插入数据。您必须检查唯一列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多