当前位置: 代码迷 >> Java Web开发 >> tomcat启动后程序自动运行,唯有停止tomcat才停下来,这是为什么
  详细解决方案

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

热度:309   发布时间:2016-04-16 21:49:06.0
tomcat启动后程序自动运行,只有停止tomcat才停下来,这是为什么?
贴出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>

------解决思路----------------------
引用:
可是把设置和启动定时器注释掉 tomcat一启动程序还是不停执行。。。。

停掉tomcat,删除tomcat缓存;删除浏览器缓存,重启浏览器
然后 重启tomcat 再去测试
  相关解决方案