[关闭]
@nalan90 2018-06-29T11:16:27.000000Z 字数 7864 阅读 623

三、Javabean

JAVAEE开发


学习目录

Javabean设计原则

示例代码:

  1. public class Students {
  2. private String name;
  3. private int age;
  4. public Students() {}
  5. public void setName(String name) {
  6. this.name = name;
  7. }
  8. public String getName() {
  9. return this.name;
  10. }
  11. public void setAge(int age) {
  12. this.age = age;
  13. }
  14. public int getAge() {
  15. return this.age;
  16. }
  17. }

JSP动作标签

JSP页面中使用javabeans
  1. ## Users.java
  2. package com.po;
  3. public class Users {
  4. private String name;
  5. private String password;
  6. public Users() {
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public String getPassword() {
  12. return password;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public void setPassword(String password) {
  18. this.password = password;
  19. }
  20. }
  21. ## login.jsp
  22. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  23. <html>
  24. <head>
  25. <title>登录页面</title>
  26. </head>
  27. <body>
  28. <h1>用户登录</h1>
  29. <hr>
  30. <form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
  31. 用户名: <input type="text" name="name"><br>
  32. 密码:<input type="password" name="password"><br>
  33. <input type="submit" value="login">
  34. </form>
  35. </body>
  36. </html>
  37. ## dologin.jsp
  38. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  39. <html>
  40. <head>
  41. <title>登录页面</title>
  42. </head>
  43. <body>
  44. <%
  45. request.setCharacterEncoding("utf-8");
  46. %>
  47. <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
  48. <h1>setProperty动作元素</h1>
  49. <hr>
  50. <!-- 根据表单自动匹配所有的属性 -->
  51. <%--
  52. <jsp:setProperty name="myUsers" property="*"/>
  53. --%>
  54. <!-- 根据表单匹配部分的属性 -->
  55. <%--
  56. <jsp:setProperty name="myUsers" property="name" />
  57. --%>
  58. <!--与表单无关,通过手工赋值给属性-->
  59. <%--
  60. <jsp:setProperty name="myUsers" property="name" value="张爽"/>
  61. <jsp:setProperty name="myUsers" property="password" value="888888"/>
  62. --%>
  63. <!--通过URL传参给属性赋值-->
  64. <jsp:setProperty name="myUsers" property="name"/>
  65. <jsp:setProperty name="myUsers" property="password" param="mypass"/>
  66. <!--使用传统的表过式获取用户名跟密码-->
  67. <%--
  68. 用户名: <%=myUsers.getName()%><br>
  69. 密码:<%=myUsers.getPassword()%><br>
  70. --%>
  71. <!--使用getProperty方式来获取用户名跟密码-->
  72. 用户名: <jsp:getProperty name="myUsers" property="name"/><br>
  73. 密码: <jsp:getProperty name="myUsers" property="password"/>
  74. </body>
  75. </html>

Javabean的四个作用域范围
  1. ## login.jsp
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>登录页面</title>
  6. </head>
  7. <body>
  8. <h1>用户登录</h1>
  9. <hr>
  10. <form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
  11. 用户名: <input type="text" name="name"><br>
  12. 密码:<input type="password" name="password"><br>
  13. <input type="submit" value="login">
  14. </form>
  15. </body>
  16. </html>
  17. ## dologin.jsp
  18. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  19. <html>
  20. <head>
  21. <title>登录页面</title>
  22. </head>
  23. <body>
  24. <%
  25. request.setCharacterEncoding("utf-8");
  26. %>
  27. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="application"/>--%>
  28. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="session"/>--%>
  29. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="request"/>--%>
  30. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>--%>
  31. <h1>setProperty动作元素</h1>
  32. <hr>
  33. <!-- 根据表单自动匹配所有的属性 -->
  34. <%--
  35. <jsp:setProperty name="myUsers" property="*"/>
  36. --%>
  37. <!-- 根据表单匹配部分的属性 -->
  38. <%--
  39. <jsp:setProperty name="myUsers" property="name" />
  40. --%>
  41. <!--与表单无关,通过手工赋值给属性-->
  42. <%--
  43. <jsp:setProperty name="myUsers" property="name" value="张爽"/>
  44. <jsp:setProperty name="myUsers" property="password" value="888888"/>
  45. --%>
  46. <!--通过URL传参给属性赋值-->
  47. <jsp:setProperty name="myUsers" property="name"/>
  48. <jsp:setProperty name="myUsers" property="password" param="mypass"/>
  49. <!--使用传统的表过式获取用户名跟密码-->
  50. <%--
  51. 用户名: <%=myUsers.getName()%><br>
  52. 密码:<%=myUsers.getPassword()%><br>
  53. --%>
  54. <!--使用getProperty方式来获取用户名跟密码-->
  55. 用户名: <jsp:getProperty name="myUsers" property="name"/><br>
  56. 密码: <jsp:getProperty name="myUsers" property="password"/>
  57. <br>
  58. <br>
  59. <a href="testScope.jsp">测试Javabean的四个作用域范围</a>
  60. <%--
  61. <%
  62. //request的作用域无法通过超链接传递
  63. request.getRequestDispatcher("testScope.jsp").forward(request, response);
  64. %>
  65. --%>
  66. </body>
  67. </html>
  68. ## testScope.jsp
  69. <%@ page import="com.po.Users" %>
  70. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  71. <html>
  72. <head>
  73. <title>Title</title>
  74. </head>
  75. <body>
  76. <h1>Javabean的四种作用范围</h1>
  77. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="application"/>--%>
  78. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="session"/>--%>
  79. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="request"/>--%>
  80. <%--<jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>--%>
  81. 用户名: <jsp:getProperty name="myUsers" property="name"/><br>
  82. 密码: <jsp:getProperty name="myUsers" property="password"/>
  83. <hr>
  84. <!-- 使用内置对象获取用户名跟密码-->
  85. <%--
  86. 用户名: <%=((Users)application.getAttribute("myUsers")).getName()%><br>-
  87. 密码: <%=((Users)application.getAttribute("myUsers")).getPassword()%>
  88. --%>
  89. <%--
  90. 用户名: <%=((Users)session.getAttribute("myUsers")).getName()%><br>
  91. 密码: <%=((Users)session.getAttribute("myUsers")).getPassword()%>
  92. --%>
  93. <%--
  94. 用户名: <%=((Users)request.getAttribute("myUsers")).getName()%><br>
  95. 密码: <%=((Users)request.getAttribute("myUsers")).getPassword()%>
  96. --%>
  97. <%--
  98. <%
  99. String name = "";
  100. String password = "";
  101. if (pageContext.getAttribute("myUsers") != null) {
  102. name = ((Users)pageContext.getAttribute("myUsers")).getName();
  103. password = ((Users)pageContext.getAttribute("myUsers")).getPassword();
  104. }
  105. %>
  106. 用户名: <%=name%><br>
  107. 密码: <%=password%>
  108. --%>
  109. </body>
  110. </html>

使用Model1实现登录案例
  1. ## UsersDAO.java
  2. package com.dao;
  3. import com.po.Users;
  4. public class UsersDAO {
  5. public boolean usersLogin(Users u) {
  6. if ("admin".equals(u.getName()) && "admin".equals(u.getPassword())) {
  7. return true;
  8. } else {
  9. return false;
  10. }
  11. }
  12. }
  13. ## Users.java
  14. package com.po;
  15. public class Users {
  16. private String name;
  17. private String password;
  18. public Users() {
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public String getPassword() {
  24. return password;
  25. }
  26. public void setName(String name) {
  27. this.name = name;
  28. }
  29. public void setPassword(String password) {
  30. this.password = password;
  31. }
  32. }
  33. ## login.jsp
  34. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  35. <html>
  36. <head>
  37. <title>登录页面</title>
  38. </head>
  39. <body>
  40. <h1>用户登录</h1>
  41. <hr>
  42. <form action="dologin.jsp?mypass=999999" name="loginForm" method="post">
  43. 用户名: <input type="text" name="name"><br>
  44. 密码:<input type="password" name="password"><br>
  45. <input type="submit" value="login">
  46. </form>
  47. </body>
  48. </html>
  49. ##dologin.jsp
  50. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  51. <%
  52. request.setCharacterEncoding("utf-8");
  53. %>
  54. <jsp:useBean id="loginUser" class="com.po.Users" scope="page"/>
  55. <jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/>
  56. <jsp:setProperty name="loginUser" property="*"/>
  57. <%
  58. if (userDAO.usersLogin(loginUser)) {
  59. session.setAttribute("loginUser", loginUser.getName());
  60. request.getRequestDispatcher("login_success.jsp").forward(request, response);
  61. } else {
  62. response.sendRedirect("login_fail.jsp");
  63. }
  64. %>
  65. ## login_success.jsp
  66. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  67. <html>
  68. <head>
  69. <title>登录成功</title>
  70. </head>
  71. <body>
  72. <%
  73. String loginUser = "";
  74. if (session.getAttribute("loginUser") != null) {
  75. loginUser = session.getAttribute("loginUser").toString();
  76. }
  77. %>
  78. 欢迎您<%=loginUser%>
  79. </body>
  80. </html>
  81. ## login_fail.jsp
  82. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  83. <html>
  84. <head>
  85. <title>登录失败</title>
  86. </head>
  87. <body>
  88. <h3>登录失败</h3>
  89. <a href="login.jsp">重新登录</a>
  90. </body>
  91. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注