Can the Oracle database send requests to a Servlet?

  • A+
Category:Languages

Usually we are sending requests from the browser, however I'm wondering if a database can also do the same thing. Let say I have a servlet on my server and an Oracle database.

Is it possible that by using the Schedule option, the database will be able to send requests to the servlet?


Using UTL_HTTP

Yes, you can send HTTP requests from the Oracle database. Here's a nice blog post that summarises how you can do it using the UTL_HTTP package: https://oracle-base.com/articles/misc/utl_http-and-ssl

An example from the Oracle manual:

SET SERVEROUTPUT ON SIZE 40000  DECLARE   req   UTL_HTTP.REQ;   resp  UTL_HTTP.RESP;   value VARCHAR2(1024); BEGIN   UTL_HTTP.SET_PROXY('proxy.my-company.com', 'corp.my-company.com');   req := UTL_HTTP.BEGIN_REQUEST('http://www-hr.corp.my-company.com');   UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');   resp := UTL_HTTP.GET_RESPONSE(req);   LOOP     UTL_HTTP.READ_LINE(resp, value, TRUE);     DBMS_OUTPUT.PUT_LINE(value);   END LOOP;   UTL_HTTP.END_RESPONSE(resp); EXCEPTION   WHEN UTL_HTTP.END_OF_BODY THEN     UTL_HTTP.END_RESPONSE(resp); END; 

Alternative using Oracle AQ

If you want some intermediary layer, you might also use Oracle AQ, which I personally find more powerful: https://docs.oracle.com/database/121/ADQUE/aq_intro.htm

Using Oracle AQ, you could for instance bypass the HTTP layer and access whatever the Servlet is calling internally, directly.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: