Reputation: 21320
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.
Upvotes: 0
Views: 1459
Reputation: 15644
First, you should avoid using scriplets in JSP.
Second, instead use Ajax :
onClick
of your button and make a call to the
servlet. Then the servlet will clear up the content on server side.Upvotes: 1
Reputation: 2107
The fact that the call to log.clear is in a JavaScript function which is not being invoked is irrelevant here. When the page is rendered on refresh, all the scriptlets are evaluated, including this one. It is probably appropriate to clear the file contents when you submit the form rather than when the user clears the form. However if you do want to do it when the user clears the form then you could use AJAX to call out to the server and clear the log there.
Upvotes: 0