String userId = request.getParameter("userId"); String passWord = request.getParameter("password"); |
1. package bookstore; 2. 3. public class User 4. { 5. PRivate String userId;//用戶Id 6. private String password;//密碼 7. private String userName;//用戶名 8. public String getPassword() { 9. return password; 10. } 11. public String getUserId() { 12. return userId; 13. } 14. public String getUserName() { 15. return userName; 16. } 17. public void setPassword(String password) { 18. this.password = password; 19. } 20. public void setUserId(String userId) { 21. this.userId = userId; 22. } 23. public void setUserName(String userName) { 24. this.userName = userName; 25. } 26. } |
提示: 你可以通過JBuilder的Bean Express工具快速創建User.java的代碼,在一般情況下,你應該通過Bean Express來創建Bean的屬性,這樣不但自動產生get/set的屬性訪問方法,還保證了Bean命名規范。 |
![]() 圖 10 指定switch.jsp的名字 |
![]() 圖 11 指定JSP中引用Bean |
![]() 圖 12 選擇類作為Bean |
![]() 圖 13 引用一個Bean |
![]() |
1. <%@ page contentType="text/html; charset=GBK" %> 2. <html> 3. <head> 4. ?。紅itle> 5. switch 6. ?。?title> 7. </head> 8. <jsp:useBean id="userBean" scope="page" class="bookstore.User" /> 9. <jsp:setProperty name="userBean" property="*" /> 10. <body bgcolor="#ffffff"> 11. <h1> 12. JBuilder Generated JSP 13.?。?h1> 14. </body> 15. </html> |
1. <%@ page contentType="text/html; charset=GBK" %> 2. <jsp:useBean id="userBean" scope="page" class="bookstore.User" /> 3. <jsp:setProperty name="userBean" property="*" /> |
1. <%@page contentType="text/html; charset=GBK"%> 2. <%@page import="bookstore.*"%> 3. <%@page import="java.sql.*"%> 4. <jsp:useBean id="userBean" scope="session" class="bookstore.User"/> 5. <jsp:setProperty name="userBean" property="*"/> 6. <% 7. Connection conn = null; 8. try { 9. conn = DBConnection.getConnection(); 10. PreparedStatement pStat = conn.prepareStatement( 11. "select USER_NAME from T_USER where USER_ID=? and password = ?"); 12. pStat.setString(1, userBean.getUserId()); 13. pStat.setString(2, userBean.getPassword()); 14. ResultSet rs = pStat.executeQuery(); 15. if (rs.next()) { //密碼正確 16. userBean.setUserName(rs.getString(1));//設置用戶名 17. session.setAttribute("ses_userBean", userBean);//將userBean放入Session對象中 18. %><jsp:forward page=" welcome.jsp "></jsp:forward> 19. <%} else { //密碼錯誤%> 20. <jsp:forward page="fail.jsp"></jsp:forward> 21. <% 22. }} finally { 23. if(conn != null) conn.close(); 24. } 25. %> |
![]() 圖 14 可怕的錯誤處理頁面 |
新聞熱點
疑難解答