Using HttpURLConnection inside Stateless Session Bean


EJB programming & troubleshooting: Using HttpURLConnection inside Stateless Session Bean

  1. Hi,

    As per the EJB Specifications, we are not allowed to use "" package.

    I need to execute a servlet existing on a different Server through a stateless session bean.
    I am planning to do this by opening a HttpURLConnection and getting the InputStream from the connection established.

    Am I violating the EJB spec? What are the inherent problems in the same? Any workarounds?



  2. Did you try writing a utility class that calls the URL and does post/get and returns a String result or custom Response object? You should be able to call a class like that. Wrap* Exceptions.

  3. Prasad,
    The EJB specification is a great resource, but you *have* to read it with caution and notice the details if you are going to use it. The EJB spec sais:
    "An enterprise bean must not use the package to attempt to access files and directories in the file system."
    This restriction is ment to ensure an enterprise bean does not become dependent on the particular box it is running on, because the App server may load it on several different boxes. It does *not* mean that you are not allowed to use at all.
    An enterprise bean is allowed to connect to a remote host, and therefore openning an HTTP connection is allowed.

    This is a common mistake. The EJB specification does not seperate an enterprise bean and the classes that it uses. Utility classes are subject to all the normal restrictions that are put on enterprise beans. The restrictions are on the *runtime environment*, not the bean's class.

  4. Thanks for the clarification!!