Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ArrayList over http??

Options
  • 03-03-2005 3:59pm
    #1
    Closed Accounts Posts: 133 ✭✭


    Hi

    I'm trying to send an arraylist over http, but i've confused myself :rolleyes:

    First up is this possible?

    I have a pda running a thick client java program, it is using http to do a lookup on an EJB on my local machine. That is working fine.

    I'm trying to invoke a method that will return an arraylist.

    currently i can pass strings through a printwriter from the pda to a jsp. I now wish to to pass an arraylist.

    can anyone give me some pointers to some example or does anyone have any ideas?

    Cheers


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Could you convert the arraylist into XML and pass that?


  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    good question.

    how do i do that??

    do you know of any examples anywhere?


  • Registered Users Posts: 4,276 ✭✭✭damnyanks


    Check XMLEncoder int he JAVA API.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Well what are the objects in the ArrayList?
    Are they just Strings?

    You could just send something like this over HTTP:
    <arrayList>
       <element index="0">String Value Here</element>
       <element index="1">String Value Here</element>
       <element index="2">String Value Here</element>
       <element index="3">String Value Here</element>
    </arrayList>
    

    It sounds like there's a more fundamental problem though with your design. Does your PDA client call a JSP page that calls the EJB? And then you want you PDA to be able to access the ArrayList? Can't you just skip the JSP part and do the EJB lookup directly from the PDA?


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    What's wrong with ObjectOutputStream?


  • Advertisement
  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    Enygma wrote:
    Can't you just skip the JSP part and do the EJB lookup directly from the PDA?

    Ideally this is what i'd like to do, My EJB are in JBoss and the PDA is using IBM's j9 VM, There is a scaled down rmi package in it, but i think is so scaled down that I can't use an initial context in my look up?? I'm a little out of my depth!


    I currently have a login that calls a jsp that does the login, the username and password are passed in the URL.

    I can leave the arrayList as an array of strings if necessary.

    Thanks for the advice so far


  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    What's wrong with ObjectOutputStream?


    This is what i'm currently trying, banging my head off a brick wall!!


  • Closed Accounts Posts: 248 ✭✭comanche


    This is what i'm currently trying, banging my head off a brick wall!!

    should be able to serizlize it alright. if it is not a complex object, why not use the http headers!?


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    This is what i'm currently trying, banging my head off a brick wall!!
    Show some code, what part are you having difficulty with? It should be very easy stuff it's only a couple of lines of code. It shouldn't be a Serializable problem because it implements it so it may just be something to do with the socket output stream handling, it can only be used in one type or reader by the way you can't keep on re-using it so make sure you use ObjectOutput(Input)Streams at both end and only those, nothing else.


  • Closed Accounts Posts: 324 ✭✭madramor


    currently i can pass strings through a printwriter from the pda to a jsp. I now wish to to pass an arraylist.

    when you say strings you mean text, because strings are java objects just
    like arraylists are java objects.

    in order to keep your application open and expandable you should only communicate using text using a printwriter.

    other wise if you use a client running on a pda using c++ it will not be able to
    read the java object arraylist but it will be able to read the xml format stated
    previously
    Enygma wrote:
    Can't you just skip the JSP part and do the EJB lookup directly from the PDA?
    same issue here if you did a direct ejb lookup it would restrict you to using
    java only.
    where as http protocol can be implemented by any language


    so you should process the arraylist on the server side and send it to the
    client as xml/text (see Enygma post)


  • Advertisement
  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    Just to clarify waht i'm trying to do.

    I have a PDA thar is running a java VM, I'm developing a thick client on the PDA in java (J2ME CDC - J9) . I have an existing set of EJB's running on my JBOSS server, I want to resue these EJB's by writing a JSP page that can invoke a method that returns an arraylist. I have a login working where I use a printwriter to login, passing username and password, the jsp invokes the login methods and returns the userID.

    I'm now trying to return an arraylist, but i'm not really sure what to do, I think i should be using some ObjectOutput(Input)Streams but can't seem to find a good example of this.

    I haven't really used ObjectOutput(Input)Streams or the like before so its a bit of a learning curve. Its prob something simple that I'm missing out on.

    I'm not sure how to serialize the array into the appropriate stream and deserialize again.

    Any help would be appreciated, thanks for the advice so far.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    You should post the code you think is causing the problem, then people will have a much better idea of what you're trying to achieve.


  • Registered Users Posts: 5,290 ✭✭✭Ardent


    I'm with Phil, I'd have gone down the XML route, easiest solution.

    Anyhow, have a look at this - it may well address the issues you are experiencing:

    http://www.microjava.com/articles/J2ME_J2EE_Integration_Strategies.pdf


  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    Hi

    Appologies for taking so long to post the code, but the machine I was working on didn't have internet access!

    I apologise for the mess, but I was spiking on this technology and I haven't had time to clean it up much.

    Also I now have it working (partially) I am receiveing the array, but the server is throwing getOutputStream has already been called??


    The JSP page

    <html>
    <head>
    <%@ page
    import = "javax.servlet.http.HttpServletRequest"
    import = "javax.servlet.http.HttpServletResponse"
    import = "javax.rmi.PortableRemoteObject"
    import = "java.rmi.*"
    import = "java.util.Hashtable"
    import = "javax.ejb.CreateException"
    import = "javax.naming.InitialContext"
    import = "java.io.*"
    import = "java.util.ArrayList"
    %>
    <title>Create User Profile</title>
    </head>
    <body bgcolor="#FFFFFF">

    <%
    System.out.println ("1");
    String remoteAddress = request.getRemoteAddr().toString();
    System.out.println ("");
    String localAddress = request.getRemoteAddr().toString();
    System.out.println ("3");
    String contextPath = request.getContextPath().toString();
    System.out.println ("4");
    String protocal = request.getProtocol().toString();
    System.out.println ("5");
    String remoteHost = request.getRemoteHost().toString();
    System.out.println ("6");

    String scheme = request.getScheme().toString();
    System.out.println ("7");
    String serverName = request.getServerName().toString();
    System.out.println ("8");
    String sessionString = request.getSession().toString();
    System.out.println ("9");


    System.out.println ("remoteAddress: " + remoteAddress);
    System.out.println ("localAddress: " + localAddress);

    System.out.println ("contextPath: : " + contextPath);
    System.out.println ("protocal" + protocal);


    System.out.println ("remoteHost: " + remoteHost);

    System.out.println ("scheme: " + scheme);
    System.out.println ("serverName: " + serverName);
    System.out.println ("sessionString: " + sessionString);

    Hashtable props = new Hashtable();
    String message = "";



    props.put(
    InitialContext.INITIAL_CONTEXT_FACTORY,
    "org.jnp.interfaces.NamingContextFactory");
    props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

    InitialContext initialContext = new InitialContext(props);

    Object obj = initialContext.lookup(ie.dit.bb.session.BestBetAccessHome.JNDI_NAME);

    ie.dit.bb.session.BestBetAccessHome bestBetAccessHome = (ie.dit.bb.session.BestBetAccessHome) PortableRemoteObject.narrow(obj,ie.dit.bb.session.BestBetAccessHome.class);

    try {

    ie.dit.bb.session.BestBetAccess myMeetingManagerSessionBean = (ie.dit.bb.session.BestBetAccess) bestBetAccessHome.create();

    ArrayList temp = myMeetingManagerSessionBean.getRaces();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(baos);

    System.out.println("sss" + temp.size());
    os.writeObject(temp);

    byte[] data = baos.toByteArray();

    response.setContentType()

    OutputStream output = response.getOutputStream();
    output.write(data);
    output.close();

    } catch (RemoteException e) {
    e.printStackTrace();
    } catch (CreateException e) {
    e.printStackTrace();
    } catch (ClassCastException e) {
    e.printStackTrace();
    }

    %>

    </body>
    </html>



    here is my cllient :


    package client;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.util.ArrayList;

    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;


    public class Communications {
    public Communications() {

    }


    public ArrayList validateLoginServlet(String userName, String password) throws IOException
    {

    loginDetails = loginServlet();
    return loginDetails;
    }

    public ArrayList loginServlet() throws IOException {
    ArrayList newList = new ArrayList();
    System.out.println("1");
    HttpConnection http = null;
    String loginString = "";
    InputStream iStrm = null;
    String url = "http://localhost:8080/BestBet/validatelogin.jsp


    try
    {

    System.out.println("3");
    http = (HttpConnection) Connector.open(url);
    System.out.println("4");
    http.setRequestMethod(HttpConnection.GET);
    System.out.println("5");
    if (http.getResponseCode() == HttpConnection.HTTP_OK )
    {

    System.out.println("6");
    ObjectInputStream ois = new ObjectInputStream(http.openInputStream());
    System.out.println("7");
    newList = (ArrayList) ois.readObject();
    System.out.println("8");
    ois.close();

    System.out.println(newList.get(1).toString());

    System.out.println("9");

    }
    }

    catch (Exception e)
    {
    System.out.println (e);
    }
    finally
    {

    }
    return newList;
    }



    }





    Sorry about the mess of the code:

    I have a nagging feeling that my jsp should be a servlet - haven't written one before but i suppose there is a first time for everything.

    Thanks for your help so far.

    Any ideas whats causing the error??


  • Closed Accounts Posts: 133 ✭✭Jim Kernsey


    Hey I got that working

    couldn't see the wood for the trees!

    here's the code i used in the end

    http = (HttpConnection) Connector.open(url);
    http.setRequestMethod(HttpConnection.GET);
    if (http.getResponseCode() == HttpConnection.HTTP_OK ) {
    ObjectInputStream ois = new ObjectInputStream(http.openInputStream());
    races = (ArrayList) ois.readObject();
    ois.close();


Advertisement