参数从输入传递到文本区域时遇到问题。当方法foo()将参数传输到textarea时,将使用服务器上文件中的值对textarea进行初始化,并删除本地更改。如何确保它们不会被擦除?
<script>
function foo(){
var x = document.getElementsByTagName("input");
for(var i = 0; i < x.length; i++) {
document.getElementById("textForPushOnServer").innerHTML += x[i].value;
}
}
</script>
<script>
function WriteToFile() {
<%
if(request.getParameter("applyChange") != null){
try{
FileWriter writer = new FileWriter(path, false);
String updURL = request.getRequestURL().toString().replace("mytools.jsp","") + "logconfig";
writer.write(request.getParameter("textForPushOnServer"));
writer.flush();
writer.close();
response.sendRedirect(updURL);
}catch(Exception e){
out.println(e.getMessage());
}
}
%>
}
</script>
<%!
public final String path = System.getProperty("user.dir") + "/log.properties";
%>
</head>
<body>
<form name="for">
<b>Input class name</b>
<input type="text" name="classname" id="classname" size="40">
<b>Input a name of logfile</b>
<input type="text" name="logfilename" id="logfilename" size="40">
<button type="submit" name="prep" onclick="foo();">Prepare</button>
<textarea name="textForPushOnServer" id="textForPushOnServer" style="width:80%" rows="33">
<%
try(FileInputStream fin = new FileInputStream(path)) {
int i =- 1;
while((i = fin.read()) != -1) {
out.print((char)i);
}
}
catch(IOException ex) {
out.println(ex.getMessage());
}
%>
</textarea>
<button type="submit" name="applyChange" onClick="WriteToFile();">Submit</button>
</form>
</body>
</html>
问题出现在这个地方
<textarea name="textForPushOnServer" id="textForPushOnServer" style="width:80%" rows="33">
<%
try(FileInputStream fin = new FileInputStream(path)) {
int i =- 1;
while((i = fin.read()) != -1) {
out.print((char)i);
}
}
catch(IOException ex) {
out.println(ex.getMessage());
}
%>
</textarea>