Java web:JSP實(shí)現(xiàn)登錄界面,DBUtils,EL表達(dá)式,個(gè)人理解【詩書畫唱】
用JSP等的知識(shí)做出個(gè)登錄界面(自己寫的題目)
個(gè)人的理解:因?yàn)閒orm表單中要用上action,所以可以取前3個(gè)字母的縮寫act當(dāng)用戶名,賬號(hào)名



create table user(
id int primary key auto_increment,
act varchar(100) ,
pwd varchar(100)?
);
--drop table user
insert into user(act,pwd) values ("詩書畫唱",'666'),("三連",'233'),("關(guān)注",'888' );
select *? from? user
?

如果是手建的話,導(dǎo)出來有時(shí)看你自己的設(shè)計(jì)會(huì)為(紅色部分比較有用,其他的沒太多的用處):
/*
Navicat MySQL Data Transfer
Source Server? ? ? ? ?: mysql
Source Server Version : 50639
Source Host? ? ? ? ? ?: localhost:3306
Source Database? ? ? ?: j190802
Target Server Type? ? : MYSQL
Target Server Version : 50639
File Encoding? ? ? ? ?: 65001
Date: 2020-09-14 11:00:08
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `act` varchar(30) DEFAULT NULL,
? `pwd` varchar(30) DEFAULT NULL,
? PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'admin2020', '123456');

但是我更喜歡用代碼建表,就用我前面的建的表的內(nèi)容:



package com.xhj.bean;
//bean的類名于表的名字一致,而且首字母必須大寫
public class User {
? ? //bean類中的屬性名應(yīng)該和表中的列名一致
private Integer id;
private String act;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}


package com.xhj.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.xhj.Utils.DBUtils;
import com.xhj.bean.User;
//Dao是數(shù)據(jù)訪問的意思,
//在這個(gè)類中負(fù)責(zé)對(duì)user表進(jìn)行增刪改查的功能實(shí)現(xiàn)
//Dao類中的方法就是負(fù)責(zé)執(zhí)行sql語句,
//只要有執(zhí)行sql語句的代碼都必須寫在dao類中
public class UserDao {
? ? public User selectByActAndPwd
? ? (String act,String pwd){??
? ? String sql = "select * "
+ "from User where act = ? and pwd = ?";
? ? Connection conn = null;
? ? PreparedStatement pstm = null;
? ? ResultSet rs = null;
? ? User u = new User();
? ? try {
? ? ? ? conn = DBUtils.getConn();
pstm = conn.prepareStatement(sql);
//設(shè)置占位符
pstm.setString(1, act);
pstm.setString(2, pwd);
rs = pstm.executeQuery();
if(rs.next()) {
Integer id = rs.getInt("id");
u.setId(id);
u.setAct(act);
u.setPwd(pwd);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源
DBUtils.close(rs, pstm, conn);
}
? ? return u;
? ? }
? ? public static void main(String[] args) {
? ? UserDao ud = new UserDao();
? ? User u = ud.selectByActAndPwd("詩書畫唱", "666");
? ? if(u.getId() != null && u.getId() > 0) {
? ? System.out.println("登錄成功");
? ? } else {
? ? System.out.println("登錄失敗");
? ? }
}
}


package com.xhj.Utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DBUtils {
private static String driverName;
? ? private static String url;
? ? private static String user;
? ? private static String pwd;
? ??
? ? static {
? ? //讀取properties文件
? ? Properties prop = new Properties();
? ? //將db.properties文件讀取到內(nèi)存中去
? ? InputStream is = DBUtils.class.getClassLoader()
? ? .getResourceAsStream("db.properties");
? ? //加載內(nèi)容
? ? try {
prop.load(is);
//讀取內(nèi)容
driverName = prop.getProperty("dn");
//System.out.println(driverName);
url = prop.getProperty("url");
user = prop.getProperty("un");
pwd = prop.getProperty("up");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? }
? ??
? ? //獲取數(shù)據(jù)庫連接對(duì)象的方法
? ? public static Connection getConn(){
? ? Connection conn = null;
? ? try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}? ?
? ? return conn;
? ? }
? ??
public static void close(ResultSet rs,
PreparedStatement pstm
? ? ,Connection conn){
? ? ? ? try {
? ? ? ? if(rs != null) {
? ? ? ? ? ? rs.close();
? ? ? ? ? ? }
? ? ? ? ? ? if(pstm != null) {
? ? ? ? ? ? pstm.close();
? ? ? ? ? ? }
? ? ? ? ? ? if(conn != null) {
? ? ? ? ? ? conn.close();
? ? ? ? ? ? }
? ? ? ? } catch(Exception e) {
? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}


dn=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/firstjsp?useUnicode=true&characterEncoding=UTF-8
un=root
up=root


<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()
? ? +"://"+request.getServerName()
? ? +":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01
?Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control"?
? ? ? ? content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content
? ? ? ? ="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content
? ? ? ? ="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size:50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ??
? ? <%-- 要記住dologin.jsp不要些錯(cuò)為doLogin.jsp
? ? ,action中的內(nèi)容的大小不可以寫錯(cuò)不然會(huì)報(bào)錯(cuò)等
? ? --%>
? ? ? ? <form action="dologin.jsp" method="post">
? ? ? ? ? ? <table border="1">
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td>賬號(hào):</td>
? ? ? ? ? ? ? ? ? ? <td><input type="text" name="act"
? ? ? ? ? ? ? ? ? ? ?placeholder="請(qǐng)輸入賬號(hào)"/></td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td>密碼:</td>
? ? ? ? ? ? ? ? ? ? <td><input type="password"?
? ? ? ? ? ? ? ? ? ? name="pwd" /></td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td colspan="2" align="center">
? ? ? ? ? ? ? ? ? ? ? ? <input type="submit"?
? ? ? ? ? ? ? ? ? ? ? ? value="提交" />
? ? ? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? </table>
? ? ? ? ? ? <div>${msg }</div>
? ? ? ? </form>
? ? </body>
</html>


<%@page import="com.xhj.bean.User"%>
<%@page import="com.xhj.DAO.UserDao"%>
<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? //中文亂碼處理:
? ? request.setCharacterEncoding("utf-8");
? ? //1、獲取用戶輸入的賬號(hào)和密碼:
? ? String act = request.getParameter("act");
? ? String pwd = request.getParameter("pwd");
? ? System.out.println(act);
? ? //2、查詢數(shù)據(jù)庫:
? ? UserDao userDao = new UserDao();
? ? User u = userDao.selectByActAndPwd(act, pwd);
? ? //3、根據(jù)查詢出來的結(jié)果進(jìn)行處理
? ? if(u.getId() != null && u.getId() > 0) {
? ? //將登錄的賬號(hào)存放到session
? ? //后面當(dāng)你跳轉(zhuǎn)到任何的頁面時(shí),
? ? //還需要驗(yàn)證是否是合法的訪問
? ? request.getSession().setAttribute
? ? ("userName", act);
? ? //a、根據(jù)賬號(hào)和密碼能夠查詢記錄,就表示登錄成功,
? ? //跳轉(zhuǎn)到后臺(tái)管理頁面
? ? request.getRequestDispatcher
? ? ("manage.jsp").forward(request, response);
? ? } else {
? ? String msg = "賬號(hào)或者密碼錯(cuò)誤";
? ? request.setAttribute("msg", msg);
//b、沒有查詢到記錄,就表示登錄失敗,跳轉(zhuǎn)回login.jsp
request.getRequestDispatcher("login.jsp")
.forward(request, response);
? ? }? ?
%>


<%@ page language="java" contentType=
"text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()
? ? +"://"+request.getServerName()
? ? +":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
?4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control"?
? ? ? ? content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? <meta http-equiv="keywords"?
? content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description"?
? ? ? ? content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>歡迎登錄本系統(tǒng),${userName }</h1>
? ? </body>
</html>






