web-xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<servlet>
<servlet-name>jmsListener</servlet-name>
<servlet-class>com.jms.topic.test.JmsReceiveTopicTest2</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->
<!-- 线图 -->
<servlet>
<servlet-name>line_basic</servlet-name>
<servlet-class>highcharts.servlet.Line_basic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>line_basic</servlet-name>
<url-pattern>/line_basic.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
servlet:
package highcharts.servlet;
import highcharts.service.line_sum;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class Line_basic extends HttpServlet{
protected static final Logger logger = LoggerFactory
.getLogger(Line_basic.class);
private static final long serialVersionUID = 7255053394404579739L;
@Resource
private line_sum lsum;
/**
* line_basic 曲线图报表
* 把相应的数据在后台组装好,在前台显示就好。
* 数据大小的x作标会相应的变化。
*/
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String categories="['0时','1时','2时','3时','4时','5时','6时','8时','9时','10时','11时','12时','13时','14时','15时','16时','17时','18时','19时','20时','21时','22时','23时']";
req.setAttribute("categories",categories);
System.out.println(categories);
String deviceId ="QY_ZX_TVD001";
ArrayList da=null;
try {
da = lsum.findData(deviceId);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dataString =
"{name: '交通量',"+da+"}";
req.setAttribute("data",dataString);
System.out.println(dataString);
req.getRequestDispatcher("/line_basic.jsp").forward(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
service:
package highcharts.service;
import java.sql.SQLException;
import java.util.ArrayList;
import highcharts.dao.jk_sumDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class line_sum {
protected static final Logger logger = LoggerFactory
.getLogger(line_sum.class);
@Autowired
private jk_sumDao sumdao;
public ArrayList findData(String deviceId) throws SQLException{
return sumdao.queryListAsHashMap(deviceId);
}
}
dao:
package highcharts.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import cn.rhytech.serviceframework.receiver.db.BaseDao;
@Repository
public class jk_sumDao extends BaseDao{
protected static final Logger logger = LoggerFactory
.getLogger(jk_sumDao.class);
// 创建静态全局变量
static Connection conn;
static Statement st;
/* 查询数据库,输出符合要求的记录的情况*/
public ArrayList queryListAsHashMap(String deviceId) throws SQLException{
conn = getConnection(); //同样先要获取连接,即连接到数据库
String sql = "select * from JK_RD_SUM_DATA WHERE T.DEVICE_ID=?"+deviceId; // 查询数据的sql语句
st = (Statement) conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rs = st.executeQuery(sql); //执行sql查询语句,返回查询数据的结果集
System.out.println("最后的查询结果为:");
ArrayList a = new ArrayList();
while (rs.next()) { // 判断是否还有下一个数据
// 根据字段名获取相应的值
int flow =rs.getInt("flow");
a.add(flow);
}
return a;
}
/* 获取数据库连接的函数*/
public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Mysql数据驱动
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:jkdb", "monitor", "monitor");// 创建数据连接
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
}
把Servlet加上@Component? 楼主没有理解WEB应用被容器加载的原理啊,在Servlet中使用Spring容器要这样用。
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
且需要在web.xml中配置
< param-name > contextConfigLocation </ param-name >
< param-value > classpath:applicationContext.xml </ param-value >
</ context-param >
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
因为WEB容器加载顺序为
listener >> filter >> servlet >> spring。其中filter的执行顺序是filter- mapping在web.xml中出现的先后顺序。加载顺序会影响对spring bean的调用。比如filter 需要用到bean ,但是加载顺序是先加载filter 后加载spring,则filter中初始化操作中的bean为null。所以,如果过滤器中要使用到 bean,可以将spring 的加载改成Listener的方式。
而ContextListener就是注册WebApplicationContext 的