Browser refresh problem...

Posted on 2006-04-09
Last Modified: 2008-03-17
Hi all,
 I have added code with question.  When i press refresh button, it resends data to server, which causes duplicate entries in previous comments text area. I am using Tomcat server. Please let me know if there is some way i can fix this problem.
Thanks in advance,
<%@ page import="*"%>
  java.util.Date date = new java.util.Date();
  String text = request.getParameter("txt1");
  String name = request.getParameter("fld1");
          FileWriter fw = new FileWriter(new File(application.getRealPath("Sample.txt")), true);
                fw.write("\n"+name + ": ("+date +") : " + text + "\r\n\n");
      } catch (IOException ioe) {}


<title>::: Comments Page :::</title>
<center><form name="frmx">
<font face="arial" size="3" color="black">
<b>Please enter your name and add comments!</b></font>

<b>Name : </b><input type=field name="fld1" cols="30"/><br/><br><br>
<b>Enter Comments here:</b><br>
<textarea name="txt1" cols="100%" rows="4"/></textarea><br/>
<b>Previous Comments :</b><br/>
<textarea readonly name="txt2" cols="100%" rows="15">
<jsp:include page="Sample.txt"/></textarea><br/><br/>
<input type="submit" name="cmdbtn1" value="Add Comments"/>
Question by:cool_baba
    LVL 22

    Expert Comment

    You made no point of it, but this is, is it not.

    Your code appears to be stating that if the input fields are not empty, then write a new record, which implies that on page refresh the fields are not empty.

    Do you have "enableviewstate" set to true, this would cause the fields to maintain their values across the refresh, so then the fields are not empty after the refresh and a new record is written.

    Author Comment

    I am using JSP.
     To see how this program works please follow this URL

    now if you add comments once and then press the browsers refresh button, it will make another entry in the previous comments text area. i.e. it resends data to server. I dont want this to happen. I need  a solid solution so that it works on every computer and browser.

    thanks for your time.
    LVL 22

    Expert Comment

    Sorry I could not get your link to work, but maybe some of the suggestions in this post will help.

    Perhaps setting no cache will drop the cache on a refresh, and prevent your problem

    Author Comment

    nope it doesn't work.
    LVL 22

    Expert Comment

    Your solution is actually very simple, when the user clicks the submit button, send them to a different page to process the request, instead of posting the page to itself, which causes the page to reprocess.

    Sorry if I have confused you, I will give you an example

    you start from File1.htm (this is a simple html form page, there is no jsp processing on this page.) Clicking submit on this page, causes the user to be directed to File2.jsp (this page contains your jsp processing code)

    So if the user clicks refresh on File1.htm, they are not processing your jsp.

    Here is a reference

    to send the user to file2 when the button is clicked you set the target using the "action" property of your form tag


    Author Comment

    Good idea..
     but what if i want to allow user to add comments more than once.. I mean the benefit of having one file is that it will allow user to add multiple comments using same site. If we have two files then user wont be able to add more comments once he/she will move to the JSP page.
     I appreciate your efforts.

    LVL 22

    Accepted Solution

    Once the file2 (jsp processing file) has added the entry, redirect the user back to file1.htm

            String redirectURL = "";

    Author Comment

    your solution will cause an additional problem which is how to read from file using html in "file1.htm".
     I have read number of related questions on EE and i think there is no way to overcome this problem.. however i can try POST in order to give user a warning about data resubmission to the webserver.

    Anyways thanks for your help and i believe you deserve points as your ideas were great..


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Now-a-days, indirectly, postal services have been replaced by email services. Yes, whenever we hear the word "email" a lot of people only think of gmail. Some people still think that email and gmail are one and the same thing :-). Let's see some …
    Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
    Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
    How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now