1.目的:
输入用户名,密码,在数据库表中进行索引,索引到,则 跳转页面否则返回原来的页面.
2.登录页面的: login.jsp
<form action="/demo17_war/apple" method="get">//点击表单提交,页面跳转:网址:http://localhost:8080/demo17_war/index.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>登入页面</title>
</head>
<body>
<form action="/demo17_war/apple" method="get">
<table border="1" width="500">
<tr>
<td>用户名:</td>
<td><input type="textn" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="textn" name="password"></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="checkcode" size="6">
</td>
</tr>
<tr>
<td> <input type="checkbox" name="remember" value="true"> </td>
<td>记住用户名</td></tr>
<tr>
<td colspan="2"><input type="submit" value="登入"></td>
</tr>
</table>
</form>
</body>
</html>
效果:
2.创建:User类:
private String password; // private String username;
先创建成员变量,再生成get(),set(),方法 get():是返回成员变量值 set(); 传入值赋值给成员变量 ()
package com.example.demo17;
public class User {
private String password;
private String username;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}
3.创建UserModel类:
目的: 实现java和数据库的连接,且查找值。password,username.
queryRunner = new QueryRunner(JDBCUtils.getDataSource());//驱动了数据库,还连接了数据库
User esistUser = queryRunner.query("select * from user where username=?and password=?", new BeanHandler<User>(User.class), user.getUsername(),user.getPassword());
//使用了:dbutils.QueryRunner,dbutils.handlers.BeanHandler.
//dbutil jar包
//查询user.getUsername(),user.getPassword(),这两个值,在数据库查询
注意:new BeanHandler<User>(User.class),写法
把值赋值给: esistUser(User对象)
// return esistUser; 如果:找到了返回一个User对象值,如果找不到返回
null值
package com.example.demo17;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
public class UserModel {
public static void main(String[] args){
}
public User login(User user) throws SQLException {
QueryRunner queryRunner = null;
try {
queryRunner = new QueryRunner(JDBCUtils.getDataSource());//驱动了数据库,还连接了
} catch (Exception e) {
e.printStackTrace();
System.out.println("错误!");
}
User esistUser = queryRunner.query("select * from user where username=?and password=?", new BeanHandler<User>(User.class), user.getUsername(),user.getPassword());
//如果找不到就返回:null,判断语句
return esistUser;
}}
4.创建 loginServlet extends HttpServlet:类
里面有doget(){},//每刷新类(这个网址)一次页面,就执行一次doget(){}
创建User类对象 existUser = userModel.login(user);//如果:查询成功,就返回类对象的地址值给existUser. 否则返回null,给existUser.
if(existUser==null){ //如果:existUser==null,就执行重定向,跳转原来的页面,
resp.sendRedirect("/demo17_war/login.jsp");//requset重定向
}
else {//如果不为null,就跳转成功页面。
resp.sendRedirect("/demo17_war/index.jsp"); //resp的重定向
}
package com.example.demo17;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println(username);
System.out.println(password);
User user=new User();
user.setUsername(username);
user.setPassword(password);
String eee=user.getUsername();
System.out.println(eee);
UserModel userModel=new UserModel();
User existUser= null;
try {
existUser = userModel.login(user);
System.out.println(existUser);
} catch (SQLException throwables) {
throwables.printStackTrace();
System.out.println("错误!");
}
if(existUser==null){
resp.sendRedirect("/demo17_war/login.jsp");
}
else {
resp.sendRedirect("/demo17_war/index.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
5.效果图:
输入: usernane:111 password:444 页面跳转原来的页面。
输入: username:77,password:4,页面成功跳转 成功 数据库:表
6.c3p0-configxml配置:
<?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/new_schema</property>
<property name="password">your-password</property>
<property name="user">root</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- This app is massive! -->
<named-config name="oracel">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/new_schema</property>
<property name="user">root</property>
<property name="password">your-password</property>
</named-config>
<named-config name="apple">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/new_schema</property>
<property name="user">root</property>
<property name="password">your-password</property>
</named-config>
</c3p0-config>
7.放入jar包注意位置:
c3p0-config.xml:放入resources目录下中(才能被找到)
在webapp目录下的WEB-INF中,创建一个lib包(目录), 在把jar包,放入·lib里面: c3p0-0.9.2.1.jar //连接池 common-dbutils-1.7.jar //dbutils mchange-commons-java-0.2.3.4jar //c3p0辅助工具 mysql-connector-java-8.027.jar //数据库驱动
7.web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>demo8</servlet-name>
<servlet-class>com.example.demo17.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo8</servlet-name>
<url-pattern>/apple</url-pattern>
</servlet-mapping>
</web-app>
之后补充
|