題目:
界面要求:繪制一個登陸界面:包括兩個標簽控件、兩個文本框和一個登陸按鈕;(完成界面繪制功能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>
程序結果截圖:
數據庫表格:
輸入正確的賬戶密碼:
輸入錯誤的賬戶密碼:
什么都不輸入:
輸入密碼時加密:
列出你對這門課的希望和自己的目標,
並具體列出你計划每周花多少時間在這門課上。
希望自己可以熟練掌握Java web的網頁系統開發,以及腳本語言的語法,還有對數據庫應該熟練操作。並且在熟練掌握后,可以對相關的程序進行程序的優化,是程序更加的簡潔,運行的高效。本人計划在該門課上每周花費10個小時的時間來學習軟件工程概論。