Search This Blog

Thursday, 5 January 2012

Applet - Servlet Communication

/************* Servlet ***************/
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CustomServlet extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
     try
    {
      ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());
      ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
    
      if (ois == null)
      {
           return;
      }
      String reqObj = (String) ois.readObject();     
  }
}


/************* Applet *****************/

package connection;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.Verma;

public class ServletCom {
    HttpURLConnection con = null;
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;
    OutputStream os = null;
    URL url = null;

    // this function create the ois
    private boolean openOIS() {
        try {
            // Stream to write to server
            ois = new ObjectInputStream(con.getInputStream());
            if (ois == null) {
                closeConnection();
                return false;
            }
            return true;
        } catch (Exception e) {
            closeConnection();
            return false;
        }
    }

    // this function create the oos
    private boolean openOOS() {
        try {
            // Stream to write to server
            oos = new ObjectOutputStream(con.getOutputStream());
            if (oos == null) {
                closeConnection();
                return false;
            }
            os = con.getOutputStream();

            if (os == null) {
                closeConnection();
                return false;
            }
            return true;
        } catch (Exception e) {
            closeConnection();
            return false;
        }
    }

    // This function creating connection and initialize all required connection
    // variables
    private boolean createConnection(URL urlCodeBase) {
        try {
            if (con != null) {
                return true;
            }
           
            String argString = ""; // default
            url = new URL(urlCodeBase.toExternalForm() + argString);
            con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);

            // Turn off caching
            con.setUseCaches(false);
            con.setDefaultUseCaches(false);

            return true;
        } catch (Exception e) {
            closeConnection();
            return false;
        }
    }

    // this function send the request vector to servlet
    private boolean sndReq(Verma custObj) {

        try {
            oos.reset();
            oos.writeObject(custObj);
            oos.flush();

            return true;
        } catch (Exception e) {
            closeConnection();
            return false;
        }
    }

    // this function send the rcv the response from servlet
    private Object rcvRes() {

        try {
            Object resultObj = ois.readObject();
            if (resultObj instanceof String) {
                return null;
            }

            return resultObj;
        } catch (Exception e) {
            closeConnection();
            return null;
        }
    }

    // This method to connect with servlet each request type will be set in the
    // vector parameter
    public Object startPointToGetData(URL urlCodeBase,
            Verma custObj) {

        Object resultObj = null;

        try {
            if (!(createConnection(urlCodeBase))) {
                return null;
            }

            if (!openOOS())
                return null;

            if (!(sndReq(custObj)))
                return null;

            if (!openOIS())
                return null;
            resultObj = rcvRes();

            closeConnection();

            return resultObj;
        } catch (Exception e) {
            closeConnection();
            return null;
        }
    }

    // To close all the connection with servlet and streams
    public void closeStreams() {
        try {
            if (oos != null) {
                oos.close();
                oos = null;
            }
            if (ois != null) {
                ois.close();
                ois = null;
            }
            if (os != null) {
                os.close();
                os = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // To close all the connection with servlet and streams
    public void closeConnection() {
        try {
            url = null;
            if (con != null) {
                con.disconnect();
                con = null;
            }
            closeStreams();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}






No comments:

Post a Comment