这是用servlet和ajax写了一个,你可以参考下
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submitBtn").click(function(){
var userName=$("#userName").val();
if(userName=="" || $.trim(userName)==""){
alert("姓名不能为空")
return false;
}
var chinese=$("#chinese").val();
var math=$("#math").val();
var reg=/^[0-9]{1,3}$/;
if(chinese=="" || $.trim(chinese)==""){
alert("语文成绩不能为空");
return false;
}
if(math=="" || $.trim(math)==""){
alert("数学成绩不能为空");
return false;
}
if(parseInt(chinese)<0 || parseInt(chinese)>100 || parseInt(math)<0 || parseInt(math)>100 || !reg.test(chinese) || !reg.test(math)){
alert("输入的成绩不是有效的");
return false;
}
$.post("myservice.do",{userName:userName,chinese:chinese,math:math},function(data){
if(data==1){
alert("添加成功");
}else{
alert("添加失败");
}
});
})
})
</script>
<body>
<form id="myForm">
<table>
<thead>
<tr>
<th>成绩信息管理</th>
</tr>
</thead>
<tbody>
<tr>
<td>姓名</td>
<td><input type="text" name="userName" id="userName"/></td>
</tr>
<tr>
<td>语文</td>
<td><input type="text" name="chinese" id="chinese"/></td>
</tr>
<tr>
<td>数学</td>
<td><input type="text" name="math" id="math"/></td>
</tr>
<tr>
<td colspan="2"><button id="submitBtn">提交</button><input
type="reset" value="重置" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
MyServlet
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String userName=request.getParameter("userName");
String chinese=request.getParameter("chinese");
String math=request.getParameter("math");
if(userName!=null && chinese!=null & math!=null){
File file=new File("d:/score.txt");
if(!file.exists()){
file.createNewFile();
}
PrintWriter out=response.getWriter();
RandomAccessFile raf=new RandomAccessFile(file, "rw");
raf.seek(raf.length());
try{
//System.getProperty("line.separator"))根据不同的系统获取不同的换行符
raf.write((userName+";"+chinese+";"+math+System.getProperty("line.separator")).getBytes());
out.print(1);
}catch(Exception ex){
out.print(0);
}finally{
raf.close();
out.close();
}
System.out.println("over.....");
}
}
}
web.xml
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/myservice.do</url-pattern>
</servlet-mapping>