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>
運行結果截圖:
什么都不輸入的情況下:
密碼輸入錯誤的情況下:
沒有用戶的情況下:
登錄成功時: