Another JAVA question

Discussion in 'OT Technology' started by XtremelyCanadian, Mar 2, 2003.

  1. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    is there anyway to store an array in a session variable???
    right now i have this, which is just a guess but it wont work

    session.setAttribute("array", array);

    ArrayList array = (ArrayList) session.getAttribute("array");
     
  2. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I'm assuming you are creating a session correctly and you can get a string value back and forth already. Is that correct?
     
  3. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    Yes, it just doesnt like the ArrayList part, it will work if i just pass a string, but only one value. I just dont know what the syntax is for passing an array.
     
  4. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I just set it up and it's working fine for me.

    index.jsp
    Code:
    <html>
    <head>
    <title>
    index
    </title>
    </head>
    <body>
    Submit!<br />
    <form method="post" action="helper">
    <br><br>
    <input type="submit" name="Submit" value="Submit">
    <input type="reset" value="Reset">
    </form>
    </body>
    </html>
    
    Helper Servlet
    Code:
    public class Helper extends HttpServlet {
    
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList array = new ArrayList();
        array.add(0, "hello");
        array.add(1, "user");
    
        HttpSession session = request.getSession(true);
        session.setAttribute("array", array);
    
        response.sendRedirect("second.jsp");
      }
    }
    
    Second.jsp
    Code:
    <%@ page import="java.util.ArrayList" %>
    
    <%
      ArrayList array = (ArrayList)session.getAttribute("array");
    %>
    
    <html>
    <head>
    <title>
    second
    </title>
    </head>
    <body>
    
    Show array:<br />
    <%= array.get(0) %><br />
    <%= array.get(1) %>
    
    </body>
    </html>
    
     
  5. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    Thanks for the walkthrough, another problem just popped up though, im trying to put random number into the array, but it doesnt seem to like the way im doing it.

    ArrayList array = new ArrayList();
    for (int i=0;i<nArrayLimit;i++)
    {
    array.add(i,rand.nextInt(50));
    }
    session.setAttribute("array", array);

    im guessing the compiler thinks that the i is not an int, which it is, any suggestions???
     
  6. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Where is the rand object coming from?
     
  7. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    it is a instance of the random class that i declared earlier

    Random rand = new Random();
     
  8. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Oh ok. :)

    You're getting errors because the add method takes an int (index) and an object (element). Your rand.nextInt call returns an int. That's why you're getting the error. Since you cannot cast an int to an object type, try this...

    Code:
    for (int i = 0; i < nArrayLimit; i++)
    {
          array.add(i, Integer.toString(rand.nextInt(50)));
    }
    
     
  9. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    Thanks alot, that worked like a charm !!!
    one last question i swear
    That all went through fine, but when i try to retrieve it in the jsp, it gives me this error

    type Exception report

    message Internal Server Error

    description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

    exception

    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:486)
    at java.util.ArrayList.get(ArrayList.java:302)
    at org.apache.jsp.TicketConfirmation$jsp._jspService(TicketConfirmation$jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(Unknown Source)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(Unknown Source)
    at org.apache.jasper.servlet.JspServlet.service(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(Unknown Source)
    at org.apache.catalina.core.StandardWrapperValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invoke(Unknown Source)
    at org.apache.catalina.core.ContainerBase.invoke(Unknown Source)
    at org.apache.catalina.core.StandardContextValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invoke(Unknown Source)
    at org.apache.catalina.core.ContainerBase.invoke(Unknown Source)
    at org.apache.catalina.core.StandardContext.invoke(Unknown Source)
    at org.apache.catalina.core.StandardHostValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.valves.ErrorReportValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.valves.AccessLogValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invoke(Unknown Source)
    at org.apache.catalina.core.ContainerBase.invoke(Unknown Source)
    at org.apache.catalina.core.StandardEngineValve.invoke(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invokeNext(Unknown Source)
    at org.apache.catalina.core.StandardPipeline.invoke(Unknown Source)
    at org.apache.catalina.core.ContainerBase.invoke(Unknown Source)
    at org.apache.catalina.connector.http.HttpProcessor.process(Unknown Source)
    at org.apache.catalina.connector.http.HttpProcessor.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:479)
     
  10. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    sorry here is the code....

    <html>
    <head>
    <title>Lotto 649</title>
    <%@ page language="java" import="java.sql.*,java.util.*" %>
    </head>
    <body bgcolor = 'goldenrod'>
    <table align='center'>
    <tr><td colspan='2'><h1>You have purchased tickets</h1></td></tr>
    <tr><td colspan='2'><strong>Your ticket numbers are below...</strong></td></tr>
    <%
    ArrayList array = (ArrayList) session.getAttribute("array");
    for (int i=0;i<10;i++)
    {
    out.println("<tr><td>" + array.get(i) + "</td></tr>");
    }
    %>
    </body>
    </html>
     
  11. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    From the trace, it looks like you are trying to access an address in the array that doesn't exist. Before you iterate through the array for the values, check the size of the array. Make sure you are not going over the boundaries of the array.

    I hope that helps, because I've got to go. If you're still having problems just keep posting here. I'll check it when I get home tonight.
     
  12. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Oh, I see. ;) Try...

    Code:
    ArrayList array = (ArrayList) session.getAttribute("array");
    
    for (int i=0; i < array.size(); i++)
    {
        out.println("<tr><td>" + array.get(i) + "</td></tr>");
    }
    
     
  13. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    thanks, that worked too, for some reason its not adding anything to the arraylist though, its really weird....
     
  14. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    for some reason its not getting in my for loop ????

    public void RandomNumber(HttpServletRequest req, HttpServletResponse res,TicketClass aTicket) throws SQLException
    {
    /**
    * Returns random numbers based on the users input
    * @param
    * @return all random numbers
    */
    Logger.log("in random numbers");
    Random rand = new Random();
    TicketClass temp = new TicketClass();
    HttpSession session = req.getSession(true);
    int nArrayLimit = (temp.getticketNumber()) * 6;
    Logger.log("after the arraylimit");
    ArrayList array = new ArrayList();
    Logger.log("after the array declaration, before for loop");
    for (int i=0;i<nArrayLimit;i++)
    {
    Logger.log("in the for loop");
    array.add(i,Integer.toString(rand.nextInt(50)));
    }
    session.setAttribute("array", array);

    }
     
  15. XtremelyCanadian

    XtremelyCanadian I play hockey.....EH

    Joined:
    Jan 4, 2002
    Messages:
    1,801
    Likes Received:
    0
    Location:
    Edmonton,Canada
    nevermind, i found out that the getticketNumber() method was bringing back 0, so of course it wont go in the for loop. Thanks for all the help compiledmonkey :bigthumb:
     
  16. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Glad I could help. ;)
     
  17. carlin

    carlin Guest

    2 suggestions....
    1. stop calling u're variables after data structure names ... (i.e) dont call it "array"

    Code:
    for (int i = 0; i < nArrayLimit; i++)
    {
          array.add(i, Integer.toString(rand.nextInt(50)));
    }
    
    Code:
    for (int i = 0; i < nArrayLimit; i++)
    {
          array.add(i, rand.nextInt(50)+"");
    }
    
    2. both of the code snippets do the same the same thing, the 2nd one does it without creating unncessary object (i.e Integer object call)
     

Share This Page