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" %>

搭建servlet+jsp环境的更多相关文章

  1. 使用Intellij搭建Servlet开发环境

    https://blog.csdn.net/yhao2014/article/details/45740111 使用Tomcat 9时,必须使用jre 1.8,否则会出现Unable to ping ...

  2. Ubuntu14.04搭建JSP与Servlet开发环境及其测试详解

    一,搭建JDK开发环境 1,在Java官网下载Jdk软件包,我的系统是64位Ubuntu14.04,所以选择jdk-8u25-linux-x64.tar.gz. 2,解压Jdk软件包 tar xvzf ...

  3. Tomcat Jsp环境搭建全过程--重拾jsp

    搭建Jsp环境最简单的办法就是 Java+Tomcat 能很好的支持jsp代码 .首先,我们需要下载JDK和Tomcat安装包 JDK下载地址 http://www.oracle.com/techne ...

  4. Linux下搭建JSP环境

    Linux下搭建JSP环境     作为一名Java EE系统架构工程师,经常需要搭配和建立JSP(Java Server Pages)的开发环境和运行环境,所以本人在平时的工作中积累了一些在Linu ...

  5. 通过Jetty搭建一个简单的Servlet运行环境

    最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境, ...

  6. servlet+jsp+java实现Web 应用

    servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...

  7. javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  8. 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(二)

    前言:在使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)中已经介绍了如何对web基础环境进行搭建,这里主要演示,如何对spring环境进行搭建,然后 ...

  9. JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

随机推荐

  1. 【手机端判断】PC_to_M自写

    var current_url = window.location.href; var replace_url = [ ['笔试简章','http://beijing.ysedu.com/zt/bjt ...

  2. video自定义

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. js获取地址栏上参数的值

    function GetQuerystring(name){ var reg=new RegExp("(^|&)" + name +"=([^&]*)(& ...

  4. Java并发编程:深入剖析ThreadLocal(转)

    目录大纲: 一.对ThreadLocal的理解 二.深入解析ThreadLocal类 三.ThreadLocal的应用场景 原文链接:http://www.cnblogs.com/dolphin052 ...

  5. WPF中的数据绑定(初级)

    关于WPF中的数据绑定,初步探讨 数据绑定属于WPF中比较核心的范畴,以下是对WPF中数据绑定的一个初步探讨.个人感觉还是带有问题性质的叙述比较高效,也比较容易懂 第一,什么是数据绑定? 假定有这么一 ...

  6. AX2009 批处理作业中使用多线程---独立任务模式

    每个工单独立一个任务. Class /* 独立任务模式 */ class DemoBatchIndividualTasks extends RunBaseBatch { str 20 SalesOrd ...

  7. Vue+Webpack构建移动端京东金融(一、开发前准备)

    一.开发前准备 1.node环境搭建 去node.js官网下载长期支持版本的node,采用全局安装,安装方式自行百度 网址:https://nodejs.org/zh-cn/ 安装后在cmd命令行运行 ...

  8. Unity Button事件的简洁处理

    看到很多人依然还是通过最原始的方法给button绑定事件并处理,这种通过Find往子集一个个的查找,获取到后再绑定事件这种操作很费事,有些人则是对查找对象写了个方法自动往子集遍历更方便获取对象,但还是 ...

  9. 2-创建spring boot项目

    想要创建一个spring boot项目,最好的方法就是参照官网的例子: https://spring.io/guides/gs/maven/#scratch 创建的过程我就不再啰嗦了,官网描述非常详细 ...

  10. PCL-CMAKELIST书写格式

    cmake_minimum_required(VERSION 2.6 FATAL_ERROR)//cmake最小版本 find_package(PCL 1.3 REQUIRED COMPONENTS ...