java servlet 服务器测试代码:
package huxin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RepeateFormServlet
*/
@WebServlet("/RepeateFormServlet")
public class RepeateFormServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RepeateFormServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
try{
Thread.sleep(6000);//延时6秒钟来模拟服务器端响应缓慢的情况
}catch(Exception e){}
out.println("已处理你提交的信息!");
System.out.println("已处理你提交的信息!");
}
}
JavaScript实现防止表单重复提交代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>表单处理</title>
<script type="text/javascript">
//<![CDATA[
function catchEvent(eventObj, event, eventHandler) {
if (eventObj.addEventListener) {
eventObj.addEventListener(event, eventHandler,false);
} else if (eventObj.attachEvent) {
event = "on" + event;
eventObj.attachEvent(event, eventHandler);
}
}
catchEvent(window,"load",setupEvents);
function setupEvents(evnt) {
catchEvent(document.getElementById("LiuYanForm"), "submit", checkForm);
}
var ischeck = false;
function checkForm(){
if(!ischeck){
ischeck=true;
}else{
alert("请勿重复提交!");
ischeck=false;
}
return ischeck;
}
//]]>
</script>
</head>
<body>
<h1>测试表单重复提交的页面</h1>
<form id="LiuYanForm" action="RepeateFormServlet" method="post" >
<p>
留言内容:<textarea name="note"></textarea><br/>
<input type="submit" value="提交"/>
</p>
</form>
</body>
</html>