• javaweb(连接数据库后一个简单的登录界面)


    结果截图:(点击登录之后用户名称和密码都会消失,所以是空的)

    ①什么都不输入

                           

    ②输入错误信息

     

    ③输入正确信息

     

    源代码:(图片设置的代码没有在这个程序里,代码是找同学问的<新手勿怪>)

    ①DBUtil.java

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.SQLException;

    public class DBUtil {

        public static Connection getConnection() {

            try {

                Class.forName("com.mysql.jdbc.Driver").newInstance();

            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            String user = "root";                                                                                    

            String password = "root";

            String url = "jdbc:mysql://localhost:3306/eeeee";

            Connection connection = null;

            try {

                connection = DriverManager.getConnection(url, user, password);

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

            return connection;

        }

    }

    ②login.jsp

    <%@page import = "com.test.Util.DBUtil" %>

    <%@page import = "java.sql.*" %>

    <%@page import = "java.sql.PreparedStatement" %>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <%

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

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

        if(username == null || "".equals(username.trim())){

            request.setAttribute("result", "请输入用户名!");

    %>

            <jsp:forward page = "showlogin.jsp"></jsp:forward>

    <%

        }

        if(password == null || "".equals(password.trim())){

            request.setAttribute("result", "请输入密码!");

    %>

            <jsp:forward page = "showlogin.jsp"></jsp:forward>

    <%

        }

        Connection connection = DBUtil.getConnection();

        boolean flag = false;

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

        PreparedStatement preparedstatement = null;

        ResultSet resultset = null;

        preparedstatement = connection.prepareStatement(sql);

        preparedstatement.setString(1,username);

        resultset = preparedstatement.executeQuery();

        while(resultset.next()){

                if(resultset.getString("password").equals(password)){

                    flag = true;

                    request.setAttribute("result", "登录成功!");

    %>                                                                                                                                                                                                                                       

                    <%=request.getAttribute("result")%>

    <%

                }

                else{

                    request.setAttribute("result", "密码错误!请重新登录!");

    %>

                    <jsp:forward page = "showlogin.jsp"></jsp:forward>

    <%

     

                }

        }

        if(!flag){

            request.setAttribute("result", "该用户不存在!登录失败!");

    %>

            <jsp:forward page = "showlogin.jsp"></jsp:forward>

    <%

        }

    %>

    </html>

    Showlogin.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

        <title>用户登录页面</title>

    </head>

    <body>

        <%=request.getAttribute("result") %>

        <form action="login.jsp" method = "get">

            <table align = "center" border = "1" width = "500">

                <tr>

                    <td>用户名称:</td>

                    <td>

                        <input type = "text" name = "username"/>

                    </td>

                </tr>

                <tr>

                    <td>用户密码:</td>

                        <td>

                            <input type = "password" name = "password"/>

                        </td>

                </tr>

                <tr align = "center">

                        <td colspan = "2">

                                <input type = "submit" value = "登录"/>

                                <input type = "reset" value = "重置"/>

                        </td>

                </tr>

            </table>

        </form>

    </body>

    </html>

    未按时完成的原因:界面代码没有了解清楚,只能看课本一点一点来。

    目标:熟练数据库和界面,自己开发一个小型网站。

  • 相关阅读:
    杜教筛
    linux运维好书推荐:《高性能Linux服务器构建实战Ⅱ》热销中,附实例源码下载
    分布式监控系统ganglia配置文档
    基于Web应用的性能分析及优化案例
    一次Linux系统被攻击的分析过程
    Keepalived中Master和Backup角色选举策略
    linux运维好书《高性能Linux服务器构建实战Ⅱ》已出版发售,附封面照!
    并行分布式运维工具pdsh
    安全运维之:Linux系统账户和登录安全
    安全运维之:文件系统安全
  • 原文地址:https://www.cnblogs.com/mhj666/p/7884678.html
Copyright © 2020-2023  润新知