zoukankan      html  css  js  c++  java
  • 登录界面

    题目:

    界面要求:绘制一个登陆界面:包括两个标签控件、两个文本框和一个登陆按钮;(完成界面绘制功能1分);

    功能要求:

    1、数据库要求:要求在数据库中建立用户表,表中包括用户名和密码(1分),实现数据库连接功能(1 分);

    2、输入正确的用户名和密码,点击登录按钮,提示登录成功(0.5分)。输入错误的用户名或者密码,登录会失败,并提示错误信息(0.5分)

    3什么都不输入,点击登陆按钮,提示“请输入用户名”信息(0.5分)

    4、输入密码时加密显示(以星号“*”显示个数信息)。(0.5分)

    程序源代码:

    连接数据库:

    复制代码
    package com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        
        public  static  Connection getConnection() {
            try {
                //1 鍔犺浇椹卞姩
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "sa";
            String password = "123456";
            String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";
            Connection connection = null;
            try {
                //2 鍒涘缓閾炬帴瀵硅薄connection
                 connection = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
        //鍏抽棴璧勬簮鐨勬柟娉�
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    复制代码

    登录的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>
        <%String res=(String)request.getAttribute("error");
        if(res=="null"||res==null)
        {
            res="";
        }
        %>
        <%=res%>
        <form action="logincheck.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="重置" />
                        <input type="button" value="注册" onClick="window.location.href='addInput.jsp'"/>
                    
                        
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    复制代码

    检查登录信息是否正确:

    复制代码
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
    <%@page import="com.jaovo.msg.model.User"%>
    <%@ 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("error", "用户名不能为空");
    %>
    <jsp:forward page="login.jsp"></jsp:forward>
    <%
        }
    %>
    <%
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
    
        UserDaoImpl userDao = new UserDaoImpl();
        try {
            if (userDao.checkuser(user)) {
    %>
    <jsp:forward page="success.jsp"></jsp:forward> 
    <%
        } else
        {
            request.setAttribute("error", "用户名/密码有误");
    %>
    
    <jsp:forward page="login.jsp"></jsp:forward>
    <%
        }
        } catch (UserException e) {
    %>
    <h2 style="color: red; font-size: 50px">
        发生错误 :
        <%=e.getMessage()%></h2>
    <%
        }
    %>
    </html>
    复制代码

    程序结果截图:

    数据库表格:

    输入正确的账户密码:

    输入错误的账户密码:

    什么都不输入:

    输入密码时加密:

    列出你对这门课的希望和自己的目标,

    并具体列出你计划每周花多少时间在这门课上。

     

    我希望自己能在JavaWeb上有建树,不再像java一样平平常常。

    每周花至少十二个小时

  • 相关阅读:
    SSIS ->> Excel Destination无法接受大于255个字符长度的字符字段(转载)
    在express站点中使用ejs模板引擎
    Kali Linux系列教程之OpenVas安装
    Kali Linux Web 渗透测试视频教程— 第二课 google hack 实战
    Kali Linux Web 渗透测试视频教程— 第四课 google hack 实战
    google hack 之 查询语法
    shellKali Linux Web 渗透测试— 初级教程(第三课)
    NODE-WEBKIT教程(12)全屏
    node-webkit教程(11)Platform Service之shell
    node-webkit教程(10)Platform Service之File dialogs
  • 原文地址:https://www.cnblogs.com/cc-9878/p/7886555.html
Copyright © 2011-2022 走看看