Applet-Servlet Communication

Discussions

Web tier: servlets, JSP, Web frameworks: Applet-Servlet Communication

  1. Applet-Servlet Communication (4 messages)

    Can anyone please help me about applet-servlet communication with atleast a full detailed example and its explanation.Also please do write about any changes,if necessary in the .policy files or elsewhere!!!
    You may also reply at raj_chat99 at yahoo dot com

    Waiting anxiously for any reply on the same.

    Thanking you in advance,

    Raj Chatterjee

    Threaded Messages (4)

  2. Applet-Servlet Communication[ Go to top ]

    These are the steps for communication between servlet and applet.

    Servlet to Applet
    1. In your applet class create an URL object by giving servlets address(url)
    2. On URL object call openConnection method to get URLConnection object.
    3. On the URLConnection object call getInputStream method to get InputStream object. From here you can read the data very easily.

    Applet to Servlet
     repeat the steps 1 and 2
    3. On the URLConnection object call getOutputStream method to get OutputStream object. write the data on this outputStream object and call flush method on this object.

    Hope this will give you fair enough idea.

    /Srinivas
  3. Applet-Servlet Communication[ Go to top ]

    visit www.j-nine.com/pubs

  4. Applet-Servlet Communication[ Go to top ]

    Hi hope you understand this method..

    public void callMe_SaveAction(String sAD,String sDate,String sTime,String shift,javax.swing.table.DefaultTableModel table) {
    java.lang.String tempObj = null;
    java.lang.String tempObj1 = null;
    String operaTion = "S";
    java.util.Vector inVec = null;
    java.util.Vector outVec = new java.util.Vector();
    System.out.println("Entered Save method");
    if(sAD.equals("") || sDate.equals("") || sTime.equals("") || shift.equals(""))
    {
    getJOptionPane1().showMessageDialog(this,"Please enter all the values", "Please Check", 2);
    return;
    }
    int rows = table.getRowCount();
    // hdcrims.ForCast addForCast = new hdcrims.ForCast();
    java.util.Vector vec = new java.util.Vector();
    java.sql.Date aDate = null;
    java.sql.Time aTime = null;
    java.sql.Time aExp_time = null;
    try {
    StringTokenizer Str_Token1 = new StringTokenizer(sDate, "/");
    int day1 = Integer.parseInt(Str_Token1.nextToken());
    int month1 = Integer.parseInt(Str_Token1.nextToken());
    int year1 = Integer.parseInt(Str_Token1.nextToken());
    aDate = new java.sql.Date(year1 - 1900, month1 - 1, day1);
    }
    catch (java.lang.Exception e) {
    getJOptionPane1().showMessageDialog(this, "Date Format : " + e.getMessage(), "Date Format Exception ", 0);
    return;
    }
    try {
    StringTokenizer arr_tm_Token = new StringTokenizer(sTime, ":");
    int h1 = Integer.parseInt(arr_tm_Token.nextToken());
    int m1 = Integer.parseInt(arr_tm_Token.nextToken());
    aTime = new java.sql.Time(h1, m1, 0);
    }catch (java.lang.Exception le) {
    getJOptionPane1().showMessageDialog(this, "Time Format :" + le.getMessage(), "Time Format Error", 0);
    return;
    }
    for (int i=0;i < rows;i++)
    {
    hdcrims.ForCast addForCast = new hdcrims.ForCast();
    for(int j=0;j < 6;j++)
    {
    switch (j)
    {
    case 0: addForCast.setTrainName((String)getDTM().getValueAt(i,j));
    break;
    case 1: addForCast.setLocation((String)getDTM().getValueAt(i,j));
    break;
    case 2: try {
    tempObj = (String)getDTM().getValueAt(i,j);
    StringTokenizer arr_tm_Token = new StringTokenizer(tempObj, ":");
    int h1 = Integer.parseInt(arr_tm_Token.nextToken());
    int m1 = Integer.parseInt(arr_tm_Token.nextToken());
    aExp_time = new java.sql.Time(h1, m1, 0);
    addForCast.setExStartTm(aExp_time);
    }catch (java.lang.Exception le) {
    getJOptionPane1().showMessageDialog(this, "Time Format :" + le.getMessage(), "Time Format Error", 0);
    return;
    }
    break;

    case 3: System.out.println("Entered 3rd case");
    addForCast.setCommoditycd(((Integer)descCommo.get((String)getDTM().getValueAt(i,j))).intValue());
    System.out.println("Exiting 3rd case");
    break;
    case 4: addForCast.setPartyCode((String)descParty.get((String)getDTM().getValueAt(i,j)));
    break;
    case 5: tempObj1 = (String)getDTM().getValueAt(i,j);
    addForCast.setFlag(tempObj1.charAt(0));
    break;
    }
    }
    addForCast.setArrDep(sAD.toString().charAt(0));
    addForCast.setFcDate(aDate);
    addForCast.setFcTime(aTime);
    addForCast.setFcShift(shift.charAt(0));
    vec.addElement(addForCast);
    }
    URLConnection servletConnection=null;
    try {
    java.net.URL servletURL = new URL(getCodeBase(),getParameter("urlString"));
       //URL servletURL = new URL(getCodeBase(), "http://localhost:8000/HDCJ2root/AddRakeJ2");
    servletConnection = servletURL.openConnection();
    System.out.println("Inside savePanel"+ getParameter("urlString"));
    System.out.println("Servlet Connect URL");
    servletConnection.setDoInput(true);
    servletConnection.setDoOutput(true);
    servletConnection.setUseCaches(false);
    servletConnection.setDefaultUseCaches(false);

    //servletConnection.setRequestProperty("Content-type",
    // "java-internal/" + java.util.Vector.class.getName());
    //servletConnection.setRequestProperty("Content-type",
    // "java-internal/" + RakeInfo.class.getName());
    ObjectOutputStream out1 = new ObjectOutputStream(servletConnection.getOutputStream());
    outVec.addElement(operaTion);
    outVec.addElement(vec);
    //out.writeObject(operaTion);
    out1.writeObject(outVec);
    System.out.println(out1.toString());
    out1.flush();
    out1.close();
    }
    catch(java.io.IOException io) {
    getJOptionPane1().showMessageDialog(this,io.getClass().getName(), "Please Check", 2);
    }
    catch(java.lang.Exception ex){
    getJOptionPane1().showMessageDialog(this,ex.getClass().getName(), "Please Check", 2);
    }
    // Get response from servlet
    try {
    ObjectInputStream in = new ObjectInputStream(servletConnection.getInputStream());
    inVec = (java.util.Vector) in.readObject();
    String oka = (String) inVec.elementAt(0);
    if (oka.equals("OKS")) {
    //rakeinfo = (RakeWagon.RakeInfo) inVec.elementAt(1);
    //System.out.println(rakeinfo.RakeNo+" "+rakeinfo.ArrDepFlg+" "+rakeinfo.TrnName);
    getJOptionPane1().showMessageDialog(this,"Data successfully saved", "Message", 2);
    getMyButtons1().setPrevEnabled(true);
    getMyButtons1().setNextEnabled(true);
    gettname().setEditable(true);
    getlocation().setEditable(true);
    getest().setEditable(true);
    clearAll();
      
    while (getDTM().getRowCount() >0)
    {
    getDTM().removeRow(0);
    }
    }
    else
    {
    getJOptionPane1().showMessageDialog(this,"Error : "+(String)inVec.elementAt(1), "Error !", 2);
    }
    }
    catch(java.io.IOException io) {
    getJOptionPane1().showMessageDialog(this,io.getClass().getName(), "Please Check", 2);
    }
    catch(java.lang.Exception ex){
    getJOptionPane1().showMessageDialog(this,ex.getClass().getName(), "Please Check", 2);
    }
    return;

    }



    Here we are sending a object to a servlet and we are recieving a reply from the servlet ...

    i hope it works for u bbye
    Sukhvinder
  5. Re: Applet-Servlet Communication[ Go to top ]

    Here is the code which you can use to communication between applet and servlet/JSP following code can be used at applet side : /* *URL to servlet */ URL serverURL = new URL(servletPath); URLConnection connection = serverURL.openConnection(); /* * Connection will be used for both input and output */ connection.setDoInput(true); connection.setDoOutput(true); /* * Disable caching */ connection.setUseCaches(false); /* *connection will be used for transfaring serialized java objects. */ connection.setRequestProperty("Content-Type", "application/octet-stream"); ObjectOutputStream outputStream = new ObjectOutputStream(connection.getOutputStream()); outputStream.writeObject(request); outputStream.flush(); outputStream.close(); ObjectInputStream inputStream = new ObjectInputStream(connection.getInputStream()); response = (Map)inputStream.readObject(); inputStream.close(); Here response is the response Map returned from servlet. following code can be used in your servlet to send serializable objects to applet. write this code in doXXX method of servlet. ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream()); Map responseMap = new HashMap(); /* put watever objects you wants to return to applet. This objects must be serializable. responseMap.put("a", "a"); */ out.writeObject(responseMap); out.flush(); out.close(); response.setStatus(HttpServletResponse.SC_NO_CONTENT); SS http://www.jyog.com