Need help in using a vector in a jsp page

Discussions

General J2EE: Need help in using a vector in a jsp page

  1. Need help in using a vector in a jsp page (5 messages)

    how can i traverse through a vector by clicking a button or link in my jsp page?can anyone show me a sample code?thanks
  2. 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
  3. 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) {<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
    thanks sir Alex!!!It's a great help for my homework =)
  4. Better yet, don't do any of that stupid shit and either use JSTL or write a custom JSP iterator tag.
  5. 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.
  6. 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.
    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.