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. <title> 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 可怕的错误处理页面 |
新闻热点
疑难解答