我的第一個web項目就是一個登陸的頁面。主要的就是搭建服務器,配置Tomcat環境和eclipse開發。基本的源代碼如下
login.jsp
1 <%@ page import="java.sql.*" 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 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>登錄界面</title> 8 </head> 9 <body> 10 <center> 11 <h1 style="color:red">登錄</h1> 12 <form id="indexform" name="indexForm" action="logincheck.jsp" method="post"> 13 <table border="0"> 14 <tr> 15 <td>賬號:</td> 16 <td><input type="text" name="username"></td> 17 </tr> 18 <tr> 19 <td>密碼:</td> 20 <td><input type="password" name="password"> 21 </td> 22 </tr> 23 </table> 24 <br> 25 <input type="submit" value="登錄" style="color:#BC8F8F"> 26 </form> 27 <form action="zhuce.jsp"> 28 <input type="submit" value="注冊" style="color:#BC8F8F"> 29 </form> 30 </center> 31 </body> 32 </html>
logincheck.jsp
1 <%@ page import="java.sql.*" 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 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <jsp:useBean id="db" class="Bean.Bean" scope="page" /> 11 <% 12 request.setCharacterEncoding("UTF-8"); 13 String username=(String)request.getParameter("username"); 14 String password=(String)request.getParameter("password");//取出login.jsp的值 15 16 //下面是數據庫操作 17 String sql="select * from login where username="+"'"+username+"'";//定義一個查詢語句 18 ResultSet rs=db.executeQuery(sql);//運行上面的語句 19 if(rs.next()) 20 { 21 /* if(password.equals(rs.getString(2))) 22 { 23 24 } */ 25 if(password.equals(rs.getObject("password"))){ 26 response.sendRedirect("loginsuccess.jsp"); 27 } 28 else{ 29 out.print("<script language='javaScript'> alert('密碼錯誤');</script>"); 30 response.setHeader("refresh", "0;url=login.jsp"); 31 } 32 } 33 else 34 { 35 out.print("<script language='javaScript'> alert('賬號錯誤——else');</script>"); 36 response.setHeader("refresh", "0;url=login.jsp"); 37 } 38 39 %> 40 </body> 41 </html>
loginsuccess.jsp
1 <%@ page import="java.sql.*" 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 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>登錄成功 </h1> 11 </body> 12 </html>
Bean.java
1 package Bean; 2 import java.sql.*; 3 public class Bean { 4 private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 5 private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=JXP"; 6 private String dbusername = "sa"; 7 private String dbpassword = "密碼"; 8 private Connection conn = null; 9 private Statement stmt = null; 10 11 public Bean() 12 { 13 try 14 { 15 Class.forName(driverStr); 16 conn = DriverManager.getConnection(connStr, dbusername, dbpassword); 17 stmt = conn.createStatement(); 18 } 19 catch (Exception ex) { 20 System.out.println("數據連接失敗!"); 21 } 22 23 } 24 25 public int executeUpdate(String s) { 26 int result = 0; 27 System.out.println("--更新語句:"+s+"\n"); 28 try { 29 result = stmt.executeUpdate(s); 30 } catch (Exception ex) { 31 System.out.println("執行更新錯誤!"); 32 } 33 return result; 34 } 35 36 public ResultSet executeQuery(String s) { 37 ResultSet rs = null; 38 System.out.print("--查詢語句:"+s+"\n"); 39 try { 40 rs = stmt.executeQuery(s); 41 } catch (Exception ex) { 42 System.out.println("ִ執行查詢錯誤!"); 43 } 44 return rs; 45 } 46 public void execQuery(String s){ 47 try { 48 stmt.executeUpdate(s); 49 } catch (SQLException e) { 50 // TODO Auto-generated catch block 51 System.out.println("執行插入錯誤!"); 52 } 53 } 54 55 public void close() { 56 try { 57 stmt.close(); 58 conn.close(); 59 } catch (Exception e) { 60 } 61 } 62 }
這些簡單的代碼和數據庫中的數據就可以構建一個簡單的登陸界面。
我覺得還是對Jsp和html的認識和使用不理解,對數據庫的認識不深,這還需要我們的大量的練習。