tomcat启动后程序自动运行,只有停止tomcat才停下来,这是为什么?

LinusXerxes 2014-09-16 03:56:26
贴出html和服务端代码 。。。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
public class addServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
String City=request.getParameter("city");
String Location=request.getParameter("location");
//int ID=Integer.parseInt(request.getParameter("id"));
PrintWriter out=response.getWriter();
System.out.println(City+" "+Location);
//插入到数据库
Connection con=null;
Statement s=null;
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="";
String sql="insert into xx (time,city,location) values(now(),'"+City+"','"+Location+"')";
//JSONObject jsonObj = new JSONObject(s3);
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,user,password);
System.out.println("连接成功!");
s=con.createStatement();
s.executeUpdate(sql);
System.out.println("success");
out.println("{'ok':'成功插入数据!'}");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

html页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HTMLS</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
var timer;
// var sub=function(){
// document.getElementById("forms").submit();
//}
// var sub=function(){
// $("#forms").submit();
// }
//发送请求数据
var sub=function(){
$.ajax(
{type:"post",
url:"xx.do",
data:{'city':'nj','location':'museum'},
success:function(data){
$("#ok").text(data.ok);
}
}
);
}
//设置周期性定时器
var timerStart=function(){
window.setInterval(sub, 3000);
}
//启动定时器
window.addEventListener('load',timerStart,false);
//控制定时器
var stopTimer=function(){
if(timer==undefined){
timer=function(){window.setInterval(sub, 3000);}
}else{
timer=function(){window.clearInterval(timer);}
}
}
</script>
</head>
<body>
<form action="xx.do" method="post" id="forms">
编号:<input type="text" name="id" value="10"/><br>
城市:<input type="text" name="city" value="nj"/><br>
地点:<input type="text" name="location" value="museum"/><br>
<div id="ok"></div>
<input type="button" value="停止提交" onclick="stopTimer()">
</form>
</body>
</html>
...全文
206 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LinusXerxes 2014-09-16
  • 打赏
  • 举报
回复
不管怎么改 程序大约每5秒执行一次。。。。怎么回事
姜小白- 2014-09-16
  • 打赏
  • 举报
回复
引用 2 楼 gp15237125756 的回复:
可是把设置和启动定时器注释掉 tomcat一启动程序还是不停执行。。。。
停掉tomcat,删除tomcat缓存;删除浏览器缓存,重启浏览器 然后 重启tomcat 再去测试
LinusXerxes 2014-09-16
  • 打赏
  • 举报
回复
可是把设置和启动定时器注释掉 tomcat一启动程序还是不停执行。。。。
姜小白- 2014-09-16
  • 打赏
  • 举报
回复
//设置周期性定时器 var timerStart=function(){ window.setInterval(sub, 3000); } //启动定时器 window.addEventListener('load',timerStart,false); //控制定时器 var stopTimer=function(){ if(timer==undefined){ timer=function(){window.setInterval(sub, 3000);} }else{ timer=function(){window.clearInterval(timer);} } 定时器定时去执行,然后去后台获取数据

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧