Browser refresh problem...

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.io.*"%>
<%
  java.util.Date date = new java.util.Date();
  String text = request.getParameter("txt1");
  String name = request.getParameter("fld1");
  if(name==null||name.trim().equals(""))name="anonymous";
  if(text!=null&&!text.trim().equals("")){
      try{
          FileWriter fw = new FileWriter(new File(application.getRealPath("Sample.txt")), true);
                fw.write("\n"+name + ": ("+date +") : " + text + "\r\n\n");
                fw.flush();
                fw.close();
      
      } catch (IOException ioe) {}

}

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

<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/>
<br><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"/>
</form></center>
</body>
</html>
cool_babaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RedKelvinCommented:
You made no point of it, but this is asp.net, 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.
cool_babaAuthor Commented:
I am using JSP.
 To see how this program works please follow this URL
http://134.153.97.244:8080/file.jsp

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.
 
RedKelvinCommented:
Sorry I could not get your link to work, but maybe some of the suggestions in this post will help.

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20971293.html

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

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

cool_babaAuthor Commented:
nope it doesn't work.
RedKelvinCommented:
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
http://ask.metafilter.com/mefi/24127

to send the user to file2 when the button is clicked you set the target using the "action" property of your form tag
http://www.htmlhelp.com/reference/wilbur/block/form.html

RedK
cool_babaAuthor Commented:
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.


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

    <%
        String redirectURL = "http://yoururl.com/file1.htm";
        response.sendRedirect(redirectURL);
    %>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cool_babaAuthor Commented:
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..

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Browsers

From novice to tech pro — start learning today.