-
Need help in using a vector in a jsp page (5 messages)
- Posted by: theo salvador
- Posted on: September 13 2006 20:21 EDT
how can i traverse through a vector by clicking a button or link in my jsp page?can anyone show me a sample code?thanksThreaded Messages (5)
- Re: Need help in using a vector in a jsp page by Alexander Hess on September 14 2006 01:56 EDT
- Re: Need help in using a vector in a jsp page by theo salvador on September 14 2006 02:59 EDT
- Re: Need help in using a vector in a jsp page by Christopher Stach II on September 14 2006 02:54 EDT
- Re: Need help in using a vector in a jsp page by Alexander Hess on September 14 2006 03:22 EDT
- Re: Need help in using a vector in a jsp page by Jeroen Wenting on September 14 2006 03:42 EDT
- Re: Need help in using a vector in a jsp page by Alexander Hess on September 14 2006 03:22 EDT
-
Re: Need help in using a vector in a jsp page[ Go to top ]
- Posted by: Alexander Hess
- Posted on: September 14 2006 01:56 EDT
- in response to theo salvador
Hi theo, it's very easy to do that: // Your Servlet or JSP for init the Vector Vector v = new Vector(); // Fill Vector // Save in Session session.setAtrribute("v", v); // Your JSP for view Vector v = (Vector) session.getAttribute("v"); int p = 1; int next = p + 1; int previous = p - 1; try { p = Integer.parseInt(request.getParameter("p")); } catch(Exception e) { // parameter was null or alphanumeric } if(next >= v.size()) { // no next link } if(previous <= 1) { // No back link } if(p <= 0 && p >= v.size()) { Object o = v.get(p); // Output Object } else { // Not found/error/first/last/current } But, I have a better idea for you. Don't use a Vector. Use a ArrayList with the List interface. // Your Servlet or JSP for init the List List l = new ArrayList(); // Fill List // Save in Session session.setAttribute("l", l); session.setAttribute("li", l.listIterator()); // Your JSP for view ListIterator li = (ListIterator) session.getAttribte("li"); String way = request.getParameter("way"); Object o = null; if("back".equals(way) && li.hasPrevious()) { o = li.previous(); doSmthWithO(o); } else if(li.hasNext()) { o = li.next(); doSmthWithO(o); } else { // error } Hope, it help you. Best Regards Alex -
Re: Need help in using a vector in a jsp page[ Go to top ]
- Posted by: theo salvador
- Posted on: September 14 2006 02:59 EDT
- in response to Alexander Hess
Hi theo,
thanks sir Alex!!!It's a great help for my homework =)
it's very easy to do that:
// Your Servlet or JSP for init the Vector
Vector v = new Vector();
// Fill Vector
// Save in Session
session.setAtrribute("v", v);
// Your JSP for view
Vector v = (Vector) session.getAttribute("v");
int p = 1;
int next = p + 1;
int previous = p - 1;
try {
p = Integer.parseInt(request.getParameter("p"));
} catch(Exception e) {
// parameter was null or alphanumeric
}
if(next >= v.size()) {
// no next link
}
if(previous <= 1) {<br> // No back link
}
if(p <= 0 && p >= v.size()) {
Object o = v.get(p);
// Output Object
} else {
// Not found/error/first/last/current
}
But, I have a better idea for you. Don't use a Vector. Use a ArrayList with the List interface.
// Your Servlet or JSP for init the List
List l = new ArrayList();
// Fill List
// Save in Session
session.setAttribute("l", l);
session.setAttribute("li", l.listIterator());
// Your JSP for view
ListIterator li = (ListIterator) session.getAttribte("li");
String way = request.getParameter("way");
Object o = null;
if("back".equals(way) && li.hasPrevious()) {
o = li.previous();
doSmthWithO(o);
} else if(li.hasNext()) {
o = li.next();
doSmthWithO(o);
} else {
// error
}
Hope, it help you.
Best Regards
Alex -
Re: Need help in using a vector in a jsp page[ Go to top ]
- Posted by: Christopher Stach II
- Posted on: September 14 2006 02:54 EDT
- in response to theo salvador
Better yet, don't do any of that stupid shit and either use JSTL or write a custom JSP iterator tag. -
Re: Need help in using a vector in a jsp page[ Go to top ]
- Posted by: Alexander Hess
- Posted on: September 14 2006 03:22 EDT
- in response to Christopher Stach II
Better yet, don't do any of that stupid shit and either use JSTL or write a custom JSP iterator tag.
That's really true and more reusable. -
Re: Need help in using a vector in a jsp page[ Go to top ]
- Posted by: Jeroen Wenting
- Posted on: September 14 2006 03:42 EDT
- in response to Alexander Hess
Indeed, but it doesn't tip on the original question, the answer to which is "no way, learn the difference between serverside and clientside code". At least if the OP is (as is often the case) hoping to iterate over a Java collection using pure Javascript code. A simple servlet returning a single record at a time from that collection to a JSP when called using a request generated on clicking a button would suffice though to do it.Better yet, don't do any of that stupid shit and either use JSTL or write a custom JSP iterator tag.
That's really true and more reusable.