搭建servlet+jsp环境

c3p0:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="mvcapp">
<!-- 指定连接数据库的属性 -->
<property name="user">root</property>
<property name="password">root</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/bookstore</property>

<!-- 若数据库中连接数不足时,一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大数据库连接数 -->
<property name="maxPoolSize">10</property>

<!-- c3po 数据库连接池可以维护的Statements 的个数 -->
<property name="maxStatements">10</property>
<!--每个连接同时可以使用 Statement 对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>

JDBCUtils:

package cn.ntrj.bookstore.utils;

import java.sql.Connection;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
* 获取和释放数据库连接的工具类
*
* @author 南通东方标准
*
*/
public class JDBCUtils {
private static DataSource ds = new ComboPooledDataSource("mvcapp");

/**
* 获取数据连接
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

/**
* 释放数据库连接的方法
*
* @param conn
*/
public static void releaseConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

BaseDao:

package cn.ntrj.bookstore.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.ntrj.bookstore.utils.JDBCUtils;

/**
* 定义一些数据库的基本操作 这个类是专门用来被其他Dao 继承的
*
* @author 南通东方标准
*
* @param <T>
*/
public class BaseDao<T> {
private QueryRunner qr = new QueryRunner();
private Class<T> type;

public BaseDao() {
// 创建一个无参的构造器,这个构造器是由子类调用
// UserDao extends BaseDao<User>
// 获取当前子类的类型
Class cla = this.getClass();
// 获取父类的类型
ParameterizedType pt = (ParameterizedType) cla.getGenericSuperclass();
// 获取所有的泛型
Type[] types = pt.getActualTypeArguments();
this.type = (Class<T>) types[0];
}

/**
* 查询一个对象
*
* @param sql
* @param params
* @return
*/
public T getBean(String sql, Object... params) {
T t = null;
// 获取数据库连接
Connection conn = JDBCUtils.getConnection();
try {
t = qr.query(conn, sql, new BeanHandler<T>(type), params);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.releaseConnection(conn);
}
return t;
}

/**
* 查询一组对象列表
*
* @param sql
* @param params
* @return
*/
public List<T> getBeanList(String sql, Object... params) {
List<T> list = null;
// 获取数据库连接
Connection conn = JDBCUtils.getConnection();
try {
list = qr.query(conn, sql, new BeanListHandler<T>(type), params);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.releaseConnection(conn);
}
return list;
}

/**
* 更新数据库操作的方法
*
* @param sql
* @param params
* @return
*/
public int update(String sql, Object... params) {
int count = 0;
// 获取数据库连接
Connection conn = JDBCUtils.getConnection();
try {
count = qr.update(conn, sql, params);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.releaseConnection(conn);
}
return count;
}

}

ServletUtils:

package cn.ntrj.bookstore.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

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

import cn.ntrj.bookstore.servlet.client.UserServlet;

public class BaseUtils extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取用户传递的请求参数
String methodName = request.getParameter("method");
// 通过方法名获取到方法的对象
// 获取当前类的Class 对象
Class cla = this.getClass();
// 获取cla 的方法(Method 对象)
// getDeclaredMethod 需要2个参数,方法名和参数名
try {
// 获取方法对象
Method method = cla.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 设置方法的访问权限
method.setAccessible(true);
// 调用方法
// invoke 用于调用一个方法
method.invoke(this, request, response);
} catch (Exception e) {
e.printStackTrace();
}

// 根据methodName的值调用不同的方法
/*
* if ("login".equals(methodName)) { //调用login方法
* login(request,response);
*
* } else if ("regist".equals(methodName)){ regist(request,response); }
* else if("delUser".equals(methodName)){ delUser(); }
*/

}

}

WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>bookStore_DF02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>cn.ntrj.bookstore.servlet.client.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/client/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RegistServlet</display-name>
<servlet-name>RegistServlet</servlet-name>
<servlet-class>cn.ntrj.bookstore.servlet.client.RegistServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistServlet</servlet-name>
<url-pattern>/client/RegistServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UserServlet</display-name>
<servlet-name>UserServlet</servlet-name>
<servlet-class>cn.ntrj.bookstore.servlet.client.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/client/UserServlet</url-pattern>
</servlet-mapping>
</web-app>

标签:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

上一篇:Java程序员的日常 —— 《编程思想》持有对象


下一篇:将一个list分为每份小鱼1000的