有时候没有数据库的情况下,要保存用户写的东西咋办呢。肯定就 用json或者txt来存了,为了好读取。一般都是json.。
环境的话,就配置好tomcat【 apache-tomcat-9.0.19】装好 jdk【1.8】就可以跑起来了!
运行效果
那么最常用的就是 FileWriter 文件类来用:
try { // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 java.io.FileWriter writer = new java.io.FileWriter(filename, true); String xconent = "{" +conent+"}," ; writer.write(xconent); writer.close(); } catch (java.io.IOException e) { e.printStackTrace(); } finally { }
注册页面使用 表单提交内容:
test.jsp
后面拿到值 a.jsp中
String name=request.getParameter("usename"); String password=request.getParameter("ps"); String email=request.getParameter("email"); String phone=request.getParameter("phone");
这里最注意的就是 json格式拼接有点绕手,
conent = "\"name\""+":" + "\""+name+"\""+","+"\"password\""+":" + "\""+password+"\""+","+"\"email\""+":" + "\""+email+"\""+","+"\"phone\""+":" + "\""+phone+"\""+",";
必须是要 json 格式,为了引号。生成的文件:
文件目录:
下面是完整代码:
test.jsp:
<%@ page contentType="text/html;charset=utf-8" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test5</title> </head> <body> <h1>用户注册</h1> <form name="form1" action="a.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="usename" size="20"></td> </tr> <tr> <td>密码:</td>> <td><input type="password" name="ps" size="20"></input></td>> </tr> <tr> <td>email:</td>> <td><input type="text" autocomplete="false" name="email" size="20"></input></td>> </tr> <tr> <td>电话:</td> <td><input type="text" name="phone" size="20"></input></td> </tr> <tr> <td align="right"><input type="submit" value="提交" ></td> <td align="center"><input type="reset" value="重置"></td> </tr> </table> </form>
a.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String filename = "moParam.json"; String path=request.getRealPath(""); filename = path+"\\"+ filename ; String conent =""; java.io.File f = new java.io.File(filename); //if(!f.exists()) { f.createNewFile(); } %> <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/> <meta http-equiv="Cache-control" content="no-cache" /> <title>生成json lllomh</title> </head> <body> <% java.text.SimpleDateFormat sdfLongTimePlusMill = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS"); java.sql.Date date = new java.sql.Date(new java.util.Date().getTime()); String nowDate = sdfLongTimePlusMill.format(date); %> =================================<%=nowDate %> ==========================================<br/> <% String sesstionStr = session.getId(); java.util.Enumeration e1 = request.getParameterNames(); while(e1.hasMoreElements()){ String paramName = (String)e1.nextElement(); String paramValue = (String)request.getParameter(paramName); // 处理参数名paramName与参数值paramValue conent = conent+"\\r\\n"+sesstionStr+":"+paramName+" = "+paramValue+"\\r\\n" ; } %> Header<br/> <% //获取请求头信息 java.util.Enumeration e2 = request.getHeaderNames(); while (e2.hasMoreElements()) { String name = (String)e2.nextElement(); String value = request.getHeader(name); conent = conent+"\r\n"+sesstionStr+":"+name + "=" + value+"\r\n" ; } %> 文件会自动追加内容json,操作完后下载: <a href="/moParam.json" title="" >下载文件</a>电脑访问,右键文件另存为. </body> </html> <% String name=request.getParameter("usename"); String password=request.getParameter("ps"); String email=request.getParameter("email"); String phone=request.getParameter("phone"); conent = "\"name\""+":" + "\""+name+"\""+","+"\"password\""+":" + "\""+password+"\""+","+"\"email\""+":" + "\""+email+"\""+","+"\"phone\""+":" + "\""+phone+"\""+","; %> <% try { // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 java.io.FileWriter writer = new java.io.FileWriter(filename, true); String xconent = "{" +conent+"}," ; writer.write(xconent); writer.close(); } catch (java.io.IOException e) { e.printStackTrace(); } finally { } %>
如果在实践的过程中遇到什么问题,也可以在评论中或者邮件问我