- HTML code
<%@page import="org.apache.catalina.Store"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ page import="java.io.*,java.util.Properties"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><% String ip = null; String account = null; String password = null; if(request!=null){ try{ Properties P = new Properties(); String filePath = getClass().getResource("/").getPath()+"ip.properties"; File file = new File(filePath); FileOutputStream outputFile = new FileOutputStream(file); if(ip!=request.getParameter("ip")){ P.setProperty("IP", request.getParameter("ip")); P.store(outputFile, "Update 'IP' value"); } if(account != request.getParameter("account")){ P.setProperty("Account", request.getParameter("account")); P.store(outputFile, "Update 'Account' value"); } if(password !=request.getParameter("password")){ P.setProperty("Password", request.getParameter("password")); P.store(outputFile, "Update 'Password' value"); } outputFile.close(); }catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } try{ Properties P = new Properties(); InputStream in = this.getClass().getResourceAsStream("/ip.properties"); P.load(in); in.close(); ip = P.getProperty("IP"); account = P.getProperty("Account"); password = P.getProperty("Password"); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException ioe){ ioe.printStackTrace(); } %> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>显示IP</title></head><body> IP:<input type ="text" name ="ip" size="20" value=<%=ip%>><p> account:<input type="text" name="account" size="20" value=<%=account%>><p> password:<input type="password" name="password" size="20" value=<%=password%>><p> <form action = "login.jsp" method = "post"> <input type = "submit" value="返回"> </form> <form method = "post"> <input type = "submit" value="修改"> </form></body></html>
修改三个文本框的任意一个后,点击修改按钮时候,IP account 都会显示null值。。。properties文件的值也没有改变。
原本设想是点击修改后,讲修改后的内容写入properties文件,然后显示最新的值。。。
------解决方案--------------------------------------------------------
再看了遍,还有其它奇怪的地方:
String ip = null;
String account = null;
String password = null;
你根本没有赋值,所以:
if(ip!=request.getParameter("ip"))
永远相当于:
if (null != request.getParameter("ip"))
那何必还写的这么复杂。。。