将html的表单数据写入文件

下面是注册信息填写:

enroll.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="enrollwrite.jsp" method="post">
<table style="font-size: 20px; align:center;">
<tr>
<th>用户注册信息</th>
</tr>
<tr>
<td>邮件名称: <input type="email" name="email"></td>
</tr>
<tr>
<td>密码: <input type="password" name="password"></td>
</tr>
<tr>
<td>确认密码: <input type="password" name="repassword"></td>
</tr>
<tr>
<td>验证码:<input type="text" name="validate"></td>
</tr>
<tr>
<td>
<input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>


下面是jsp文件,处理表单:

enrollwrite.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>写入文件</title>
</head>
<body>
<%
String email = new String(request.getParameter("email").getBytes("ISO-8859-1"), "UTF-8");
String password = new String(request.getParameter("password").getBytes("ISO-8859-1"), "UTF-8");
String repassword = new String(request.getParameter("repassword").getBytes("ISO-8859-1"), "UTF-8");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("G:/coding/eclipse-workspace/first/save.txt",true)); 

 //FileWriter(File file, boolean append) ,append为true是接入文件尾,append为false时则会覆盖
StringBuilder sb = new StringBuilder();
sb.append(email + "\t");
sb.append(password + "\t");
sb.append(repassword + "\t\n");
String a = sb.toString();
byte[] b = (a).getBytes();
bw.write(a, 0, b.length);
bw.close();
response.sendRedirect("enrollSuc.html");
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>


下面是提交成功的html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>
<b>恭喜您注册成功</b>
</h1>
</center>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值