Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Saturday, December 1, 2007

java code for JSP with Java bean

<%@ page import="java.util.*" %>


WishList




<% 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:

<% if(map.size()==0){ %>
There are no items in your Wish List

<% }
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[i].getName()%><%="
"%>
<% }
}
}%>

Add another Item



//wishListForm.jsp


WishList




Please Select items for your WishList:


<%=wishListItems.getItem(0).getName()%> -
<%=wishListItems.getItem(0).getName()%>

<%=wishListItems.getItem(1).getName()%> -
<%=wishListItems.getItem(1).getName()%>

<%=wishListItems.getItem(2).getName()%> -
<%=wishListItems.getItem(2).getName()%>

<%=wishListItems.getItem(3).getName()%> -
<%=wishListItems.getItem(3).getName()%>

<%=wishListItems.getItem(4).getName()%> -
<%=wishListItems.getItem(4).getName()%>



Show me my wishlist

Log Out



//logout.jsp


WishList



<% session.removeAttribute("wishList");%>
You have been logged out

Log in again


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;
}
}

}

No comments: