JSP中的Java组件 : 组件 « JSP技术 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » JSP技术 » 组件屏幕截图 
JSP中的Java组件

<%@ page import="java.util.*" %>
<html>
  <head>
    <title>WishList</title>
    <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/>
    <jsp:useBean class="com.java2s.WishListItems" id="wishListItems" scope="application" />
  </head>
  <body>
    <% Enumeration e = request.getParameterNames(); %>
    <% Set map = wishList.getMap(); %>
    <% while(e.hasMoreElements()){
      String key = (String)e.nextElement();
        if(key.equals(wishListItems.getItem(0).getId())){
          map.add(wishListItems.getItem(0));
        }
        if(key.equals(wishListItems.getItem(1).getId())){
          map.add(wishListItems.getItem(1));
        }
        if(key.equals(wishListItems.getItem(2).getId())){
          map.add(wishListItems.getItem(2));
        }
        if(key.equals(wishListItems.getItem(3).getId())){
          map.add(wishListItems.getItem(3));
        }
        if(key.equals(wishListItems.getItem(4).getId())){
          map.add(wishListItems.getItem(4));
        }
      %>
      Items currently In your Wish List:<br>
      <% if(map.size()==0){ %>
           There are no items in your Wish List<br>
 <%   }
      else {
        Set set = map; %>
        <% if (set!=null){
            com.java2s.Item[] keys = new com.java2s.Item[0];
            keys = (com.java2s.Item[])set.toArray(keys);
            for (int i=0;i<keys.length;i++){ %>
              <%=keys[i].getName()%><%="<br>"%>
   <%       }
          }
      }%>
    <br><a href="wishListForm.jsp">Add another Item</a>
  </body>
</html>

//wishListForm.jsp
<html>
  <head>
    <title>WishList</title>
    <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/>
    <jsp:useBean class="com.java2s.WishListItems" id="wishListItems" scope="application" />
  </head>
  <body>
    Please Select items for your WishList:<br>
    <form method="post" action="wishList.jsp">
      <input type="checkbox" name="<%=wishListItems.getItem(0).getId()%>"/><%=wishListItems.getItem(0).getName()%> -
      <%=wishListItems.getItem(0).getName()%></input><br>
      <input type="checkbox" name="<%=wishListItems.getItem(1).getId()%>"/><%=wishListItems.getItem(1).getName()%> -
      <%=wishListItems.getItem(1).getName()%></input><br>
      <input type="checkbox" name="<%=wishListItems.getItem(2).getId()%>"/><%=wishListItems.getItem(2).getName()%> -
      <%=wishListItems.getItem(2).getName()%></input><br>
      <input type="checkbox" name="<%=wishListItems.getItem(3).getId()%>"/><%=wishListItems.getItem(3).getName()%> -
      <%=wishListItems.getItem(3).getName()%></input><br>
      <input type="checkbox" name="<%=wishListItems.getItem(4).getId()%>"/><%=wishListItems.getItem(4).getName()%> -
      <%=wishListItems.getItem(4).getName()%></input><br>
      <input type="submit" value="Add to My WishList">
    </form>
    <p><a href="wishList.jsp">Show me my wishlist</a>
    <p><a href="logout.jsp">Log Out</a>
  </body>
</html>

//logout.jsp
<html>
  <head>
    <title>WishList</title>
    <jsp:useBean class="com.java2s.WishList" id="wishList" scope="session"/>
  </head>
  <body>
    <% session.removeAttribute("wishList");%>
    You have been logged out

    <p><a href="wishListForm.jsp">Log in again</a>
  </body>
</html>
package com.java2s;

public class Item {
  private String id;
  private String name;
  
  public Item(){}
  
  public Item(String s, String t){
    name=s;
    id=t;
  }
  
  public String getId(){
    return id;
  }
  
  public String getName(){
    return name;
  }
}




package com.java2s;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;

public class WishList implements HttpSessionBindingListener {
  private Set map = new HashSet();
  public Set getMap(){
    return map;
  }

  //Session binding methods
  public void valueBound(HttpSessionBindingEvent e){
    System.out.println("The WishList has been Bound!");
  }

  public void valueUnbound(HttpSessionBindingEvent e){
    Item[] keys = new Item[0];
    System.out.println("Getting values...");
    Iterator it = map.iterator();
    while(it.hasNext()){
      Item item = (Item)it.next();
      System.out.println(item.getName());
    }
  }
}


package com.java2s;

public class WishListItems {
  
  private Item[] items = {
        new Item("ID 1","Name 1"),
        new Item("ID 2","Name 2"),
        new Item("ID 3","Name 3"),
        new Item("ID 4","Name 4"),
        new Item("ID 5","Name 5")
        };
    
  public Item getItem(int i){
    if (items[i]!=null){
      return items[i];
    }
    
    else {
      return null;
    }
  }
  
}
           
       
Related examples in the same category
1. 调用一个私人方法
2. JSP的表格及组件
3. 获取设置属性JSTL
4. 使用属性值
5. 使用构造
6. JSP中使用记数组件
7. 使用JSP技术的Java组件
8. 使用包装的JSP
9. 在JSP中使用UseBean
10. 设置属性值
11. JSP的使用范围会议组件
12. 组件财产显示器
13. 组件scriptlet
14. EL及复杂的JavaBeans
15. JSP的形式及Java组件
16. JSP的电子邮件地址有效检查
17. JSP和Java组件3
18. JSP的标准事件:建立属性
19. JSP和Java组件( JavaBeans技术) 1JSP和Java组件( JavaBeans技术) 1
20. JSP和Java组件( JavaBeans技术) 2JSP和Java组件( JavaBeans技术) 2
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.