zoukankan      html  css  js  c++  java
  • 实现简单的登录界面

    1. 需要掌握的技术;

    Java语言(Java database Connectivity技术、Servlet技术、jsp(Java Server Pages)技术,JavaBean(Application)应用组件技术)、面向对象分析设计思想、设计模式和框架结构、XML语言、网页脚本语言、开发工具(数据库、web服务器、集成开发环境(IDE))

    程序源代码:

    DBUtil.java

    package com.jaovo.msg.Util;
    
    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/jaovo_msg";
            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.jaovo.msg.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 = "longinwindow.jsp"></jsp:forward>
    <%
        }
        if(password == null || "".equals(password.trim())){
            request.setAttribute("result", "请输入密码!");
    %>
            <jsp:forward page = "longinwindow.jsp"></jsp:forward>
    <%
        }
        Connection connection = DBUtil.getConnection();
        boolean flag = false;
        String sql = "select * from t_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 = "longinwindow.jsp"></jsp:forward>
    <%
    
                }
        }
        if(!flag){
            request.setAttribute("result", "没有这个人!登录失败!");
    %>
            <jsp:forward page = "longinwindow.jsp"></jsp:forward>
    <%
        }
    %>
    </html>

    loginwindow.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>

    运行结果截图:

    什么都不输入的情况下:

    密码输入错误的情况下:

    没有用户的情况下:

    登录成功时:

  • 相关阅读:
    Triangle
    Pascal's Triangle II
    Pascal's Triangle
    Populating Next Right Pointers in Each Node II
    Populating Next Right Pointers in Each Node
    [c++]this指针理解
    [oracle]一个最简单的oracle存储过程"proc_helloworld"
    Oracle 的 INSERT ALL和INSERT FIRST
    Linux2.6 内核的 Initrd 机制解析
    /boot/grub/menu.lst详解
  • 原文地址:https://www.cnblogs.com/guo-xu/p/7875760.html
Copyright © 2011-2022 走看看