clear form data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stack
    New Member
    • Sep 2007
    • 40

    clear form data

    Hi all,

    I am creating a web app using JSP. I have a bean with some variables whose values are set by the user by using a form in a jsp (method is post). When the user enters those values these are stored in my bean and they are available to all my jsps since I'm creating a "session". So, after the submission of the form the values are still displayed in the form and this is exactly what I want. In my jsp I have an if statement which causes the values to be displayed unless their values are "null". However, when I decide to start a different session and I set all the variables to "null" the values are still displayed on my page after every refresh. I have tested that with firefox, ie, opera and safari, and the only browser that does what I want is safari. The way I'm doing it is to call from my jsp another jsp which sets the vars to "null". I have tried using reset() on the form but it doesn't solve my problem. I have also tried to invalidate the session (after I had set the session's attributes and made my jsp display the session's attributes rather than getting the vars from my bean) but still the values are displayed.
    What am I missing here? Sorry for the long message.

    Regards
    stack
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You'll need to post your code, so that someone can see where you're going wrong.

    Also, seeing as it's a JSP problem, I'm moving this thread to the Java forum.

    Comment

    • stack
      New Member
      • Sep 2007
      • 40

      #3
      index.jsp

      Code:
      <jsp:useBean id="bean" scope="session" class="myPack.bean" />
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
      <% 
          if (request.getMethod().equalsIgnoreCase("POST")){
                 bean.setP(request.getParameter("p"));
           }
      %>
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>JSP Page</title>
              
              <script language="javascript">
                  function clearVars(url, popN){
                      window.open(url, popN, "width=300, height=200, location=no,toolbar=no, scrollbars=no");
                  }
              </script>
          </head>
          <body>
              <h2>form</h2>
            
              <form action="index.jsp" method="post">
                  <input name="p" value="<% if(bean.getP() != null) out.print(bean.getP()); %>"/>
                  <input type="submit" value="send form" />
              </form>
              
              <input type="button" value="clear all" onclick='clearVars("clearSession.jsp", "popup");' />
          </body>
      </html>

      clearSession.js p

      Code:
      <jsp:useBean id="bean" scope="session" class="myPack.bean" />
      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
      
      <% 
           bean.setP(null);  
       %>
      
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>Reset Session</title>
              <script language="javascript">
                  
                  function refreshParent(){
                      //close this popup
                      window.close();
                      // reload the parent
                     window.opener.location.reload(true);
                }
              </script>
          </head>
          <body>
              <h4>All variables cleared from memory.<br> 
                 Please press close to clear any old values </h4>
              <input type="button" value="close" onclick="refreshParent();">
        </body>
      </html>
      bean.java

      Code:
      package myPack;
      
      public class bean {
          private String p;
      
          public String getP() {
              return p;
          }
          public void setP(String p) {
              this.p = p;
          }
      }
      thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The problem is in refreshParent() where you have window.close before the parent reload - swap the two lines around.

        PS. maybe I should've asked for the code before moving the thread. It seems it is a JavaScript problem, after all.

        Comment

        • stack
          New Member
          • Sep 2007
          • 40

          #5
          acoder thank you for your answer.

          I tested what you said but that's not it. I still see the old information in my form (the reload() was working anyway). Maybe it is a JSP issue after all :-)
          Any more suggestions?

          thanks

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Aargh!

            OK, when you reload the page, does it display a message about re-posting?

            Comment

            • stack
              New Member
              • Sep 2007
              • 40

              #7
              yes it does and I confirm to resend.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                That's the source of the problem. The values are reposted and in line 8, p is reset again to the posted value.

                You can either use GET or use the Post-Redirect-Get pattern (see Post/Redirect/Get - Wikipedia, the free encyclopedia)

                Comment

                • stack
                  New Member
                  • Sep 2007
                  • 40

                  #9
                  You were right. Fortunately the solution was much simpler.... Using a redirect the browser can reload a URL without repeating the original request (or at least this is what I understood by doing some googling)

                  So adding following code in line 9, solves the problem.

                  Code:
                  response.sendRedirect("index.jsp");
                  Thanks for all your help acoder!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    No problem at all. Glad you got it working :)

                    Comment

                    Working...