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. H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛

    题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...

  2. 转)Ubuntu安装teamviewer

    以下内容提炼于:https://www.cnblogs.com/wmr95/p/7574615.html 官网下载相应包:https://www.teamviewer.com/zhcn/downloa ...

  3. SSM商城开发学习

    功能模块:前端:门户.商品搜索.商品展示.购物车.注册&登录 后端:商品管理.订单管理.cms 上线,bug,维护,停到上线,维护,打包,上线 某一个模块出现bug,停到这个模块 tomcat ...

  4. K3精益版给物料添加属性,并在BOM中新增字段引用该属性

    1.给物料新增属性 打开“系统--基础资料--公共资料--核算项目管理”,然后双击物料,弹出核算项目类别-修改对话框.再点新增按钮: 输入你想新增字段的类型,长度,想要放置的位置. 相关属性里面选的是 ...

  5. Requset模块

    Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库 各种请求方式: #!/urs/bin/evn python # -*- cod ...

  6. windows service 2008 R2 升级 sp1遇到的问题

    因为我的程序是以vs2015开发的,所以在在布署windows service 2008 R2 项目的时候报出 红框里的错,说明要安装.net framework4.6. 感觉so easy,下载一个 ...

  7. linux升级openssh到7.9

    客户linux主机ssh存在高危漏洞,需要进行升级修复. linux联网后,直接命令行: [root@gw ~]# yum update openssl -y 此命令只是小版本的升级,比如将opens ...

  8. layer.tips属性

    layer.tips(新加的内容,'选择节点',{time: 0, area: ['20%', '20%'], skin: 'layui-layer-rim', tips: [3, '#ffffff' ...

  9. PLSQL账号密码

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca

  10. Scrum冲刺阶段6

    成员今日完成的任务 人员 任务 何承华 学习后端设计 陈宇 后端设计 丁培辉 学习后端设计 温志铭 信息界面设计 杨宇潇 信息界面界面设计 张主强 服务器构建学习 成员遇到的问题 人员 问题 何承华 ...