0

I have a requirement where on clicking a button, I have to empty the text area, which is populated from a file, and need to empty the file contents also.

My jsp page looks like this

<%@page import="com.hcentive.rule.DisplayLog"%>
<%@ page import="java.lang.*" import="java.util.Scanner"  
    import="java.io.FileReader" import="java.net.*"%>  

<%  
DisplayLog log = new DisplayLog();  
log.display();  
%>  

<html>   
    <body>  
     <script>
        function validate() {
           alert("clear clicked");
           document.getElementById("rule").value = "";
           <%
                log.clear(); 
           %>
        }
        </script>   
      <br><br>
        <FORM>
            <textarea rows="20" cols="162" wrap="off" id="rule" ><%=log.getFileOutput()%></textarea>  
            <br><br>
            <input type="button" value="Clear" onclick="validate();" >
        </FORM>  
    </body> 
</html>  

In above code, DisplayLog is a java class. display method is used to read file contents and write in the text area and clear method is used to empty the file contents.

It is working fine when i click the clear button it empties the text area.The problem that i am facing is that on browser refresh, log.clear() is also getting called which i don't understand.

Please someone help.

4

2 回答 2

1

首先,您应该避免在 JSP 中使用脚本。

其次,改为使用 Ajax :

  1. 创建一个可以对文件进行清除部分的 servlet
  2. 在按钮中使用 ajaxonClick并调用 servlet。然后 servlet 将清除服务器端的内容。
于 2013-05-22T10:35:38.870 回答
0

对 log.clear 的调用是在一个没有被调用的 JavaScript 函数中这一事实在这里无关紧要。当页面在刷新时呈现时,所有的 scriptlet 都会被评估,包括这个。在提交表单而不是在用户清除表单时清除文件内容可能更合适。但是,如果您确实想在用户清除表单时执行此操作,那么您可以使用 AJAX 调用服务器并清除那里的日志。

于 2013-05-22T10:34:51.547 回答