加载的jar有:
log4j-1.2.16.jar
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.14-bin.jar
org.springfaramework.asm-3.0.5.release.jar
org.springfaramework.beans-3.0.5.release.jar
org.springfaramework.context-3.0.5.release.jar
org.springfaramework.core-3.0.5.release.jar
org.springfaramework.expression-3.0.5.release.jar
org.springfaramework.web-3.0.5.release.jar
commons-logging-1.1.1.jar
文件结构图如下:
代码如下:
DemoDao.java
package org.baicai.spring.dao;
import java.util.List;
public interface DemoDao {
public List
DemoDaoImpl.java
package org.baicai.spring.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.baicai.spring.dao.DemoDao;
public class DemoDaoImpl implements DemoDao {
private DataSource source;
public DataSource getSource()
{
return source;
}
public void setSource(DataSource source)
{
this.source=source;
}
@Override
public List
DemoServlet.java
package org.baicai.spring.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.baicai.spring.dao.DemoDao;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class DemoServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private ApplicationContext context;
private DemoDao dao;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
//由于servlet容器的生命周期不受spring管理,所以注入数据需要主动去spring容器内获取
context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
dao=context.getBean("demoDao",DemoDao.class);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
req.setAttribute("list", dao.query());
req.getRequestDispatcher("/Demo/demo.jsp").forward(req, resp);
} catch (Exception e) {
throw new ServletException(e);
}
}
}
context.xml
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
sql.properties
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://127.0.0.1\:3306/mysql?characterEncoding\=utf8
username=root
password=paul
web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
org.springframework.web.context.ContextLoaderListener
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Spring+servlet
demo.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
${data[0]}-${data[1]}-${data[2]}
注:spring的演示都没有对编码进行设置
执行结果如下: