您的位置:首页 > 数据库 > MySQL

MySQL+DBUtils+C3P0+ Servlet+HTML完成Web端登陆验证案例实现

2017-07-31 21:17 429 查看
1.技术分析
Web客户端采用HTML编写布局文件,利用Form表单标签完成Http请求的提交。服务器采用Tomcat服务器,利用Servlet来处理Http请求,后台数据库采用MySQL数据库,利用DBUtils+C3P0来完成JDBC操作。



图1.1 案例架构
2.技术实现步骤
2.1后台数据库创建
1.创建数据库的SQL语句:CREATE DATABASE person;
2.选择指定的数据库:USE person;
3.创建数据库表用户表(Person表)
CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,
        username VARCHAR(20) ,
       passworld VARCHAR(20) );
4.插入数据库的SQL语句:
INSERT INTO person(id,username,passworld) VALUES(1,'nuc','123');
INSERT INTO person(id,username,passworld) VALUES(2,'bupt','123');
图2.1 数据库建表情况
2.2 Web前端的布局代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
#username{
width: 250px;
}
#password{
margin-left: 16px;
width: 250px;
}
</style>
</head>
<body>
<form action="http://localhost:8080/Web2/LoginServlet",method="post">
用户名<input id="username", name="username",rrequired="true"/><br/>
密码<input id="password" name="password",rrequired="true"//><br/>
<input type="submit",name",value="登陆"/><br/>
</form>
</body>
</html


图2-2 Web前端代码
2.3 服务器Servlet代码

public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//1.设置发送数据的编码格式
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2.获取请求报文中携带的数据
Person person;
String userName = request.getParameter("username");
String password= request.getParameter("password");

a6d2
//3 利用DBUtils完成查询
QueryRunner qr= new QueryRunner(C3P0Utils.getDataSource());
String sqlString = "select * from person where username = ? and passworld=?";
//String[] paramString = new String []{userName,password};
//System.out.println(paramString[0]+paramString[1]);
try {
person =qr.query(sqlString, new BeanHandler<Person>(Person.class),userName,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.getWriter().write("登陆错误");
System.out.println("登陆错误");
throw new RuntimeException();
}
if(person!=null)
{
System.out.println("登陆成功");
response.getWriter().write("登陆成功");
}
else
{
System.out.println("账号密码错误");
response.getWriter().write("账号密码错误");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

this.doGet(request,response);
}
}
注1:Person是javaBean对象,其中具有id username passworld字段,设置了相应的无参/有参,get/set方法。
注2:C3P0Utils是对C3P0代码的封装,以下是其具体实现:
public class C3P0Utils
{
public static DataSource mDataSource;
static
{
mDataSource = new ComboPooledDataSource();
}
public static  Connection getConnection()
{
Connection connection;
try {
connection = mDataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new  RuntimeException();
}
return connection;
}
public static DataSource getDataSource()
{
return mDataSource;
}
}


2.4 在利用好Servlet完成Http响应的时候,需要配置Web.xml文件,用来设计Servlet对应Url,这里贴出LoginServlet对应Web.xml文件配置内容 
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.butp.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

2.5 利用C3P0连接池的时候,同样需要填写配置文件,以下是配置文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/person</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>

<named-config name="oracle">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/person</property>
<property name="user">root</property>
<property name="password">123</property>
</named-config>

</c3p0-config>                         


                                
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: