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

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

    2.源代码:

     1 package com.jaovo.msg.Util;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.SQLException;
     6 public class DBUtil {
     7     public static Connection getConnection() {
     8         try {
     9             Class.forName("com.mysql.jdbc.Driver").newInstance();
    10         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    11             // TODO Auto-generated catch block
    12             e.printStackTrace();
    13         }
    14         String user = "root";
    15         String password = "root";
    16         String url = "jdbc:mysql://localhost:3306/jaovo_msg";
    17         Connection connection = null;
    18         try {
    19             connection = DriverManager.getConnection(url, user, password);
    20         } catch (SQLException e) {
    21             // TODO Auto-generated catch block
    22             e.printStackTrace();
    23         }
    24         return connection;
    25     }
    26 }
    DBUtil.java
     1 <%@page import = "com.jaovo.msg.Util.DBUtil" %>
     2 <%@page import = "java.sql.*" %>
     3 <%@page import = "java.sql.PreparedStatement" %>
     4 <%@ page language="java" contentType="text/html; charset=UTF-8"
     5     pageEncoding="UTF-8"%>
     6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     7 <html>
     8 <%
     9     String username = request.getParameter("username");
    10     String password = request.getParameter("password");
    11     if(username == null || "".equals(username.trim())){
    12         request.setAttribute("result", "账户不能为空!");
    13 %>
    14         <jsp:forward page = "addInput.jsp"></jsp:forward>
    15 <%
    16     }
    17     if(password == null || "".equals(password.trim())){
    18         request.setAttribute("result", "密码不能为空!");
    19 %>
    20         <jsp:forward page = "addInput.jsp"></jsp:forward>
    21 <%
    22     }
    23     Connection connection = DBUtil.getConnection();
    24     boolean flag = false;
    25     String sql = "select * from user where username = ?";
    26     PreparedStatement preparedstatement = null;
    27     ResultSet resultset = null;
    28     preparedstatement = connection.prepareStatement(sql);
    29     preparedstatement.setString(1,username);
    30     resultset = preparedstatement.executeQuery();
    31     while(resultset.next()){
    32             if(resultset.getString("password").equals(password)){
    33                 flag = true;
    34                 request.setAttribute("result", "登陆成功!");
    35 %>
    36                 <%=request.getAttribute("result")%>
    37 <%
    38             }
    39             else{
    40                 request.setAttribute("result", "密码错误!请重新输入!");
    41 %>
    42                 <jsp:forward page = "addInput.jsp"></jsp:forward>
    43 <%
    44 
    45             }
    46     }
    47     if(!flag){
    48         request.setAttribute("result", "该账户不存在!");
    49 %>
    50         <jsp:forward page = "addInput.jsp"></jsp:forward>
    51 <%
    52     }
    53 %>
    54 </html>
    add.jsp
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6     <title>用户登录页面</title>
     7 </head>
     8 <body>
     9     <%=request.getAttribute("result") %>
    10     <form action="add.jsp" method = "get">
    11         <table align = "center" border = "1" width = "500">
    12             <tr>
    13                 <td>用户账号:</td>
    14                 <td>
    15                     <input type = "text" name = "username"/>
    16                 </td>
    17             </tr>
    18             <tr>
    19                 <td>用户密码:</td>
    20                     <td>
    21                         <input type = "password" name = "password"/>
    22                     </td>
    23             </tr>
    24             <tr align = "center">
    25                     <td colspan = "2">
    26                             <input type = "submit" value = "登录"/>
    27                             <input type = "reset" value = "重置"/>
    28                     </td>
    29             </tr>
    30         </table>
    31     </form>
    32 </body>
    33 </html>
    addInput.jsp

    3.运行结果截图:

    初始界面:

    成功登陆:

    账户为空:

    密码为空:

     密码错误:

    账户不存在:

    4.未完成原因:

    数据库连接出现问题。

    5.目标:

    能够快速独立的完成JavaWeb项目。  

  • 相关阅读:
    hdu 4768 Flyer 二分
    hdu 4767 Bell
    hdu 4759 Poker Shuffle 二进制
    uva 12589
    zoj 3057 Beans Game 博弈论
    poj 2480 Longge's problem 积性函数
    重新启程
    【Luogu P1502】 窗口的星星
    【BZOJ1855】[Scoi2010] 股票交易
    【BZOJ1122】[POI2008] 账本BBB
  • 原文地址:https://www.cnblogs.com/kangxy/p/7884452.html
Copyright © 2011-2022 走看看