[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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>
0
cool_baba
Asked:
cool_baba
  • 4
  • 4
1 Solution
 
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.
0
 
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.
 
0
 
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
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
cool_babaAuthor Commented:
nope it doesn't work.
0
 
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
0
 
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.


0
 
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);
    %>
0
 
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..

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now