欢迎光临散文网 会员登陆 & 注册

Java web:JSP实现登录界面,DBUtils,EL表达式,个人理解【诗书画唱】

2020-09-14 23:32 作者:诗书画唱  | 我要投稿

用JSP等的知识做出个登录界面(自己写的题目)

个人的理解:因为form表单中要用上action,所以可以取前3个字母的缩写act当用户名,账号名







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'),("关注",'888' );

select *  from  user

 

如果是手建的话,导出来有时看你自己的设计会为(红色部分比较有用,其他的没太多的用处):


/*

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');




但是我更喜欢用代码建表,就用我前面的建的表的内容:



package com.xhj.bean;

//bean的类名于表的名字一致,而且首字母必须大写

public class User {

    //bean类中的属性名应该和表中的列名一致

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是数据访问的意思,

//在这个类中负责对user表进行增删改查的功能实现

//Dao类中的方法就是负责执行sql语句,

//只要有执行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);

//设置占位符

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文件读取到内存中去

    InputStream is = DBUtils.class.getClassLoader()

    .getResourceAsStream("db.properties");

    //加载内容

    try {

prop.load(is);

//读取内容

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();

}

    }

    

    //获取数据库连接对象的方法

    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不要些错为doLogin.jsp

    ,action中的内容的大小不可以写错不然会报错等

    --%>

        <form action="dologin.jsp" method="post">

            <table border="1">

                <tr>

                    <td>账号:</td>

                    <td><input type="text" name="act"

                     placeholder="请输入账号"/></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、获取用户输入的账号和密码:

    String act = request.getParameter("act");

    String pwd = request.getParameter("pwd");

    System.out.println(act);

    //2、查询数据库:

    UserDao userDao = new UserDao();

    User u = userDao.selectByActAndPwd(act, pwd);

    //3、根据查询出来的结果进行处理

    if(u.getId() != null && u.getId() > 0) {

    //将登录的账号存放到session

    //后面当你跳转到任何的页面时,

    //还需要验证是否是合法的访问

    request.getSession().setAttribute

    ("userName", act);

    //a、根据账号和密码能够查询记录,就表示登录成功,

    //跳转到后台管理页面

    request.getRequestDispatcher

    ("manage.jsp").forward(request, response);

    } else {

    String msg = "账号或者密码错误";

    request.setAttribute("msg", msg);

//b、没有查询到记录,就表示登录失败,跳转回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>欢迎登录本系统,${userName }</h1>

    </body>

</html>



Java web:JSP实现登录界面,DBUtils,EL表达式,个人理解【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律