login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body bgcolor = "pink"> <!-- 置中對齊 --> <center> 使用者登入<br> <hr> <% String flag = request.getParameter("errNo"); try{ if(flag.equals("1")){ out.println("密碼錯誤"); } if(flag.equals("2")){ out.println("使用者名稱不存在"); } }catch(Exception e){ e.printStackTrace(); } %> <form action = "loginCl.jsp" method="post"> 使用者名稱:<input type="text" name="username"><br> 密 碼:<input type="password" name ="passwd"><br> <input type="submit" value="提交"> <input type="reset" value="重設"> </form> </center> </body></html>
loginCl.jsp驗證使用者名稱密碼
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'loginCl.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <% Connection conn= null ; Statement smt = null; ResultSet rs = null; try{ //接收使用者名稱和密碼 String u = request.getParameter("username"); String p = request.getParameter("passwd"); //在資料庫中驗證使用者 String className="oracle.jdbc.driver.OracleDriver"; //String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat"; String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat"; String userName = "sales_jacdcs"; String userPass ="sales_jacdcs"; //載入驅動 Class.forName(className); //得到串連 //注意這裡飯的錯誤:url userName uerPass已經是字串的格式。下面的語句不需要加""雙引號。害了我10分鐘 conn = DriverManager.getConnection(url,userName,userPass); //建立Statement smt = conn.createStatement(); //查詢 //StringBuffer sb = "select passwd from yyytestusers where username = '; rs = smt.executeQuery("select passwd from yyytestusers where username = '"+ u +"'"); // if(rs.next()){ //c存在使用者名稱 //判斷密碼 if(rs.getString(1).equals(p)){ response.sendRedirect("wel.jsp?user="+u); }else{ //密碼不對返回到登陸 response.sendRedirect("login.jsp?errNo=1"); } }else{ response.sendRedirect("login.jsp?errNo=2"); } }catch(Exception e){ e.printStackTrace(); }finally{ if(null != rs){ rs.close(); } if(null != smt){ smt.close(); } if(null != conn){ conn.close(); } } /* //簡單驗證使用者名稱密碼 if(u.equals("yyy") && p.equals("123")){ //跳轉頁面 //將資料傳到下一個介面(注意這裡的wel.jsp?user中間不允許有空格,否則會出錯) response.sendRedirect("wel.jsp?user="+u); // }else{ //不合法 response.sendRedirect("login.jsp"); } */ %> </body></html>
wel.jsp成功登入介面
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gb2312"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'wel.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> 登陸成功!<%= request.getParameter("user") %><br> <a href="login.jsp">重新登陸</a> <h1>使用者列表</h1> <% //定義分頁用到的4個變數 int pageSize = 3; //每頁顯示3個 int pageNow = 1; //預設第一頁 int rowCount = 0;//所有記錄數 int pageCount=0;// rowCount/pageSize //擷取頁面傳來的pageNow資訊 String pp = request.getParameter("pageTo"); if(pp != null){ try{ pageNow = Integer.parseInt(pp); }catch(Exception e){ out.println("pagenow=" + pageNow); e.printStackTrace(); } } //查詢所有的記錄數 Connection conn= null ; Statement smt = null; ResultSet rs = null; try{ //接收使用者名稱和密碼 String u = request.getParameter("username"); String p = request.getParameter("passwd"); //在資料庫中驗證使用者 String className="oracle.jdbc.driver.OracleDriver"; //String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat"; String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat"; String userName = "sales_jacdcs"; String userPass ="sales_jacdcs"; //載入驅動 Class.forName(className); //得到串連 //注意這裡飯的錯誤:url userName uerPass已經是字串的格式。下面的語句不需要加""雙引號。害了我10分鐘 conn = DriverManager.getConnection(url,userName,userPass); //建立Statement smt = conn.createStatement(); //查詢 //StringBuffer sb = "select passwd from yyytestusers where username = '; rs = smt.executeQuery("select count(*) from yyytestusers "); //一定要rs.next() rs才指向要取得值的地方 if(rs.next()){ rowCount = rs.getInt(1);//1 :表示 一行一列 } }catch(Exception e){ e.printStackTrace(); }finally{ /*if(null != rs){ rs.close(); } if(null != smt){ smt.close(); } if(null != conn){ conn.close(); }*/ } //計算pageCount if(rowCount%pageSize == 0){ pageCount = rowCount / pageSize; }else{ pageCount = rowCount/ pageSize + 1; } //查詢需要顯示的記錄 String select_by_pageNow = "select * from (select rownum rn, a.* from yyytestusers a) where rn < "+ (pageNow*pageSize+1) +"and rn not in (select rn from (select rownum rn, a.* from yyytestusers a) where rn < "+((pageNow-1)*pageSize+1) + ") order by userid"; try{ rs = smt.executeQuery(select_by_pageNow); }catch(Exception e){ e.printStackTrace(); } %> <table border = "1"> <tr><td>使用者ID</td><td>名字</td><td>密碼</td><td>Email</td><td>層級</td></tr> <% while(rs.next()){ %> <!-- 注意:這裡使用的是字串的形式得到值 rs.getInt("userid") rs.getInt(1) 會出異常。。待回覆--> <tr><td><%=rs.getInt("userid") %></td><td><%=rs.getString("username") %></td><td><%=rs.getString("passwd") %></td><td><%=rs.getString("email") %></td><td><%=rs.getInt("grade") %></td></tr> <% } %> </table> <% //顯示超連結 //首頁 out.println("<a href=wel.jsp?pageTo="+1+">首頁</a>"); //上一頁 if(pageNow != 1){ out.println("<a href=wel.jsp?pageTo="+(pageNow - 1)+">上一頁</a>"); } // 判斷頁數的 if(pageNow == 1){ for(int i = pageNow; i <= pageNow + 5; i++){ out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>"); } }else if(pageNow == 2){ for(int i = pageNow-1; i < pageNow + 5; i++){ out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>"); } }else if(pageNow == pageCount){ for(int i = pageCount-4; i <= pageCount; i++){ out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>"); } }else if(pageNow == pageCount-1){ for(int i = pageCount-3; i <= pageCount+1; i++){ out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>"); } }else{ for(int i=pageNow - 2; i <= pageNow + 2; i++){ out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>"); } } //顯示寫一頁 if(pageNow != pageCount){ out.println("<a href=wel.jsp?pageTo="+(pageNow + 1)+">下一頁</a>"); } //末頁 out.println("<a href=wel.jsp?pageTo="+pageCount+">末頁</a>"); %> </body></html>