搭建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" %>
搭建servlet+jsp环境的更多相关文章
- 使用Intellij搭建Servlet开发环境
https://blog.csdn.net/yhao2014/article/details/45740111 使用Tomcat 9时,必须使用jre 1.8,否则会出现Unable to ping ...
- Ubuntu14.04搭建JSP与Servlet开发环境及其测试详解
一,搭建JDK开发环境 1,在Java官网下载Jdk软件包,我的系统是64位Ubuntu14.04,所以选择jdk-8u25-linux-x64.tar.gz. 2,解压Jdk软件包 tar xvzf ...
- Tomcat Jsp环境搭建全过程--重拾jsp
搭建Jsp环境最简单的办法就是 Java+Tomcat 能很好的支持jsp代码 .首先,我们需要下载JDK和Tomcat安装包 JDK下载地址 http://www.oracle.com/techne ...
- Linux下搭建JSP环境
Linux下搭建JSP环境 作为一名Java EE系统架构工程师,经常需要搭配和建立JSP(Java Server Pages)的开发环境和运行环境,所以本人在平时的工作中积累了一些在Linu ...
- 通过Jetty搭建一个简单的Servlet运行环境
最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境, ...
- servlet+jsp+java实现Web 应用
servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- 使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(二)
前言:在使用IntelliJ IDEA和Maven管理搭建Web开发环境(以Spring MVC为例)(一)中已经介绍了如何对web基础环境进行搭建,这里主要演示,如何对spring环境进行搭建,然后 ...
- JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
随机推荐
- 2018-2019-2 20175126谢文航 实验一《Java开发环境的熟悉》实验报告
一.实验报告封面 课程:Java程序设计 班级:1751班 姓名:谢文航 学号:20175126 指导教师:娄嘉鹏 实验日期:2019年3月28日 实验时间:--- 实验序号:实验一 实验名称:Jav ...
- Vue-input框checkbox强制刷新
在引用input框的checkbox属性时,选中后会出现数据已经刷新,checkbox选中状态不会改变.这时在事件触发后可以调用this.$forceUpdate(),强制刷新页面解决这个问题. in ...
- 使用idea的springboot项目出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
参考: https://www.cnblogs.com/lfm601508022/p/InvalidBoundStatement.html https://blog.csdn.net/xsggsx/a ...
- 微信小程序开发过程问题总汇
之前在开发一个控车小程序,把过程中稍微需要搜索的问题做了记录. 1. 关键词:本地资源图片无法通过WXSS获取 描述:做小程序开发的时候,如果你需要使用图片作为背景,也就是想使用background- ...
- Python爬虫——Request模块
# 使用 Requests 发送网络请求# 1.导入 Requests 模块import requests# 2.尝试获取某个网页 # HTTP 请求类型r = requests.get('https ...
- 13. Redis监控运维云平台CacheCloud
13. Redis监控运维云平台CacheCloud13.1 CacheCloud是什么13.1.1 现有问题13.1.2 CacheCloud基本功能13.2 快速部署13.2.1 CacheClo ...
- matplotlib -- 基础知识
matplotlib 组织图表的方式 最上层是一个 Figure 实例,包含了所有可见的和其他一些不可见的内容.该 Figure 实例包含了一个 Axes 实例的成员属性 Figure.axes,同时 ...
- java学习(五)
学号 20189214 <Java程序设计>第五周学习总结 教材学习内容总结 输入输出 文件系统可以包含3种类型的对象:文件.目录和符号链接. 一个文件或路径是一个java.io.File ...
- Winform 利用 Oracle.ManagedDataAccess访问Oracle数据库
Winform 利用 Oracle.ManagedDataAccess访问Oracle数据库时出现以下错误: Message = "每个配置文件中只允许存在一个 <configSect ...
- laravel-重定向携带自定义消息
在控制器写: return redirect(route('member_create'))->with('success', '操作成功'); //指定到路由名member_create // ...