日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java mysql servlet_Java--用户登录(JDBC,MYSQL,Servlet)

發(fā)布時間:2024/9/19 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mysql servlet_Java--用户登录(JDBC,MYSQL,Servlet) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java--用戶登錄(JDBC,MYSQL,Servlet)

博客說明

文章所涉及的資料來自互聯(lián)網(wǎng)整理和個人總結(jié),意在于個人學(xué)習(xí)和經(jīng)驗(yàn)匯總,如有什么地方侵權(quán),請聯(lián)系本人刪除,謝謝!

用戶登錄案例需求

編寫login.html登錄頁面

username & password 兩個輸入框

使用Druid數(shù)據(jù)庫連接池技術(shù),操作mysql,day14數(shù)據(jù)庫中user表

使用JdbcTemplate技術(shù)封裝JDBC

登錄成功跳轉(zhuǎn)到SuccessServlet展示:登錄成功!用戶名,歡迎您

登錄失敗跳轉(zhuǎn)到FailServlet展示:登錄失敗,用戶名或密碼錯誤

登錄頁面

Title

用戶名:

密碼:

創(chuàng)建數(shù)據(jù)庫

CREATE DATABASE login;

USE login;

CREATE TABLE user(

id INT PRIMARY KEY auto_increment,

username VARCHAR(32) UNIQUE NOT NULL,

password VARCHAR(32) NOT NULL

)

配置文件

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql:///login

username=root

password=root

initialSize=5

maxActive=10

maxWait=3000

項(xiàng)目依賴

image-20200622115607312

創(chuàng)建用戶實(shí)體類

創(chuàng)建包c(diǎn)n.guizimo.domain,創(chuàng)建類User

package cn.guizimo.domain;

/**

* @author tanglei

* @date 2020/6/22 10:28 上午

*/

public class User {

private int id;

private String username;

private String password;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

@Override

public String toString() {

return "User{" +

"id=" + id +

", username='" + username + '\'' +

", password='" + password + '\'' +

'}';

}

}

創(chuàng)建工具類

創(chuàng)建包c(diǎn)n.guizimo.util,編寫工具類JDBCUtils

package cn.guizimo.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

/**

* @author tanglei

* @date 2020/6/22 10:38 上午

*/

public class JDBCUtils {

private static DataSource ds ;

static {

try {

//1.加載配置文件

Properties pro = new Properties();

//使用ClassLoader加載配置文件,獲取字節(jié)輸入流

InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");

pro.load(is);

//2.初始化連接池對象

ds = DruidDataSourceFactory.createDataSource(pro);

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 獲取連接池對象

*/

public static DataSource getDataSource(){

return ds;

}

/**

* 獲取連接Connection對象

*/

public static Connection getConnection() throws SQLException {

return ds.getConnection();

}

}

創(chuàng)建數(shù)據(jù)操作類

創(chuàng)建包c(diǎn)n.guizimo.dao,創(chuàng)建類UserDao,提供login方法

package cn.guizimo.dao;

import cn.guizimo.domain.User;

import cn.guizimo.util.JDBCUtils;

import org.springframework.dao.DataAccessException;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

/**

* @author tanglei

* @date 2020/6/22 10:34 上午

*/

public class UserDao {

private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

public User login(User loginUser) {

try {

String sql = "select * from user where username = ? and password = ?";

User user = template.queryForObject(sql,

new BeanPropertyRowMapper(User.class),

loginUser.getUsername(), loginUser.getPassword());

return user;

} catch (DataAccessException e) {

e.printStackTrace();

return null;

}

}

}

創(chuàng)建servlet類

cn.guizimo.web.servlet.LoginServlet類

package cn.guizimo.web.servlet;

import cn.guizimo.dao.UserDao;

import cn.guizimo.domain.User;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**

* @author tanglei

* @date 2020/6/22 11:15 上午

*/

@WebServlet("/loginServlet")

public class LoginServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//1.設(shè)置編碼

req.setCharacterEncoding("utf-8");

//2.獲取請求參數(shù)

String username = req.getParameter("username");

String password = req.getParameter("password");

//3.封裝user對象

User loginUser = new User();

loginUser.setUsername(username);

loginUser.setPassword(password);

//4.調(diào)用UserDao的login方法

UserDao dao = new UserDao();

User user = dao.login(loginUser);

//5.判斷user

if(user == null){

//登錄失敗

req.getRequestDispatcher("/failServlet").forward(req,resp);

}else{

//登錄成功

//存儲數(shù)據(jù)

req.setAttribute("user",user);

//轉(zhuǎn)發(fā)

req.getRequestDispatcher("/successServlet").forward(req,resp);

}

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doGet(req,resp);

}

}

cn.guizimo.web.servlet.FailServlet類

package cn.guizimo.web.servlet;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**

* @author tanglei

* @date 2020/6/22 11:26 上午

*/

@WebServlet("/failServlet")

public class FailServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//設(shè)置編碼

resp.setContentType("text/html;charset=utf-8");

//輸出

resp.getWriter().write("登錄失敗,用戶名或密碼錯誤");

}

}

cn.guizimo.web.servlet.SuccessServlet類

package cn.guizimo.web.servlet;

import cn.guizimo.domain.User;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**

* @author tanglei

* @date 2020/6/22 11:31 上午

*/

@WebServlet("/successServlet")

public class SuccessServlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//獲取request域中共享的user對象

User user = (User) req.getAttribute("user");

if(user != null){

//設(shè)置編碼

resp.setContentType("text/html;charset=utf-8");

//輸出

resp.getWriter().write("登錄成功!"+user.getUsername()+",歡迎您");

}

}

}

運(yùn)行

image-20200622124825184

登錄成功

image-20200622125016804

登錄失敗

image-20200622124935764

感謝

萬能的網(wǎng)絡(luò)

以及勤勞的自己

總結(jié)

以上是生活随笔為你收集整理的java mysql servlet_Java--用户登录(JDBC,MYSQL,Servlet)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。