zoukankan      html  css  js  c++  java
  • jsp3 javabean

     Javabean
    解决的代码重复编写,
    1公有类2无参公有构造方法3属性私有4getter和aetter方法
    JSP动作元素
      为请求阶段体
    1:在JSP页面使用Javabean
    ---new    像普通java类
    Uers.java
    1. package com.po;
    2. public class Users {
    3. private String username;// 用户名
    4. private String password;// mima
    5. public Users() {
    6. // 默认构造方法
    7. }
    8. public String getUsername() {
    9. return username;
    10. }
    11. public void setUsername(String username) {
    12. this.username = username;
    13. }
    14. public String getPassword() {
    15. return password;
    16. }
    17. public void setPassword(String password) {
    18. this.password = password;
    19. }
    20. }
    .jsp
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
    2. <%@page import="com.po.Users" %>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    6. %>
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    8. <html>
    9. <head>
    10. <base href="<%=basePath%>">
    11. <title>My JSP 'index.jsp' starting page</title>
    12. <meta http-equiv="pragma" content="no-cache">
    13. <meta http-equiv="cache-control" content="no-cache">
    14. <meta http-equiv="expires" content="0">
    15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    16. <meta http-equiv="description" content="This is my page">
    17. <!--
    18. <link rel="stylesheet" type="text/css" href="styles.css">
    19. -->
    20. </head>
    21. <body>
    22. <%
    23. Users user=new Users();
    24. user.setUsername("admin");
    25. user.setPassword("123456");
    26. %>
    27. <h1>使用普通方式使用Javabean</h1>
    28. <hr>
    29. 用户名:<%=user.getUsername() %><br>
    30. 密码:<%=user.getPassword() %><br>
    31. </body>
    32. </html>

    ---使用jsp动作标签使用javabean
    <jsp:useBeans> 在jsp页面实例化或在指定范围使用javabean;
    <jsp:useBen id="标识符" class="java类名"  scope="作用范围"/>
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'index.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21. <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
    22. <h1>使用useBean使用Javabean</h1>
    23. <hr>
    24. 用户名:<%=myUsers.getUsername() %><br>
    25. 密码:<%=myUsers.getPassword() %><br>
    26. </body>
    27. </html>
    <jsp:setProperty>给已经实例化的javaBean对象属性赋值:4



    login.jsp
    1. <body>
    2. <h1>系统登录</h1>
    3. <hr>
    4. <form name="loginForm" action="dologin.jsp?mypass=999999" methood="post">
    5. <table>
    6. <tr>
    7. <td>用户名</td>
    8. <td><input type="text" name="username" value="" /></td>
    9. </tr>
    10. <tr>
    11. <td>密码</td>
    12. <td><input type="password" name="password" value="" /></td>
    13. </tr>
    14. <tr>
    15. <td colspan="2" align="center"><input type="submit" value="登录" /></td>
    16. </tr>
    17. </table>
    18. </form>
    19. </body>
    dologin.jsp
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %>
    2. <%@page import="com.po.Users" %>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    6. %>
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    8. <html>
    9. <head>
    10. <base href="<%=basePath%>">
    11. <title>My JSP 'index.jsp' starting page</title>
    12. <meta http-equiv="pragma" content="no-cache">
    13. <meta http-equiv="cache-control" content="no-cache">
    14. <meta http-equiv="expires" content="0">
    15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    16. <meta http-equiv="description" content="This is my page">
    17. <!--
    18. <link rel="stylesheet" type="text/css" href="styles.css">
    19. -->
    20. </head>
    21. <body>
    22. <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>
    23. <h1>setProperty动作</h1>
    24. <hr>
    25. <!-- 根据表单自动匹配所有的属性 -->
    26. <%-- <jsp:setProperty name="myUsers" property="*"/> --%>
    27. <!-- 根据表单匹配部分的属性 -->
    28. <%-- <jsp:setProperty property="username" name="myUsers"/> --%>
    29. <!-- 于表单无关,手工赋值属性 -->
    30. <%-- <jsp:setProperty name="myUsers" property="username" value="ilas"/>
    31. <jsp:setProperty name="myUsers" property="password" value="12345"/> --%>
    32. <!-- 通过URL传参数,给属性赋值 -->
    33. <jsp:setProperty property="username" name="myUsers"/>
    34. <jsp:setProperty name="myUsers" property="password" param="mypass"/>
    35. 用户名:<%=myUsers.getUsername() %><br>
    36. 密码:<%=myUsers.getPassword() %><br>
    37. </body>
    38. </htm l>
    <jsp:getProperty>获取指定javanbean对象属性值
     <jsp:getProperty name="javaBean实例名" property="属性名">
      
    1. <!-- 使用getProperty来获取用户名和密码 -->
    2. 用户名:<jsp:getProperty name="myUsers" property="username"/><br>
    3. 密码:<jsp:getProperty name="myUsers" property="password"/><br>
    javabean作用域
       使用useBean的scope属性,来指定javabean的作用范围
    page//仅在当前页面有效
    request//通过HtttpRequest.getAttribute()
    session//通过HttpSession.getAttribute()
    applicatoin//通过applicatoin.getAttribute()---可以作用 整个WEB应用

    1. <h1>JAVAbean的四个作用范围</h1><hr>
    2. <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/><!-- page applicatio session request -->
    3. 用户名:<jsp:getProperty name="myUsers" property="username"/><br>
    4. 密码:<jsp:getProperty name="myUsers" property="password"/><br>
    5. <!-- 使用内置对象获取用户名和密码 -->
    6. <hr>
    7. <%--
    8. 用户名:<%=((Users)application.getAttribute("myUsers")).getUsername() %><br>
    9. 密码:<%=((Users)application.getAttribute("myUsers")).getPassword() %><br> --%>
    10. <hr>
    11. <%-- 用户名:<%=((Users)session.getAttribute("myUsers")).getUsername() %><br>
    12. 密码:<%=((Users)session.getAttribute("myUsers")).getPassword() %><br> --%>
    13. <hr>
    14. <%-- 用户名:<%=((Users)request.getAttribute("myUsers")).getUsername() %><br>
    15. 密码:<%=((Users)request.getAttribute("myUsers")).getPassword() %><br> --%>
    Model1模式
     jsp接受页面,
     
     





  • 相关阅读:
    WebAPI的文件上传与下载
    cefSharp框架中的C#方法和Web项目中的JS方法相互调用
    C# 引用类型的对象克隆(深拷贝)。
    C++模板特化
    一、JavaScript高级程序设计-----JavaScript简介
    二、C#图解教程第七章--类和继承
    C#IDIspose接口的使用
    CLR via C# 可空值类型
    WPF数据绑定
    计算机网路基础
  • 原文地址:https://www.cnblogs.com/liuruimiku/p/5457802.html
Copyright © 2011-2022 走看看