UTL_HTTP

December 3, 2007

Well, we all know about simple web application architecture.  It will have a servlet/jsp calling an backend procedure or function.  Is it possible to reverse it? i.e. can you call a servlet from a database procedure/function.  Yes!  It is possible with UTL_HTTP package in oracle.  Here is a sample :

DECLARE
 REQ_1 UTL_HTTP.REQ_1;
 RESP_1 UTL_HTTP.RESP_1;
 VALUE VARCHAR2(1024);
BEGIN
  REQ_1 := UTL_HTTP.BEGIN_REQUEST('HTTP://www.yoursite.com:8988/servletpath/servlet');
  UTL_HTTP.SET_HEADER(REQ_1, 'USER-AGENT', 'MOZILLA/4.0');
  RESP_1 := UTL_HTTP.GET_RESPONSE(REQ_1);
  LOOP
  UTL_HTTP.READ_LINE(RESP_1, VALUE, TRUE);
  DBMS_OUTPUT.PUT_LINE(VALUE);
  END LOOP;
  UTL_HTTP.END_RESPONSE(RESP_1);
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
  UTL_HTTP.END_RESPONSE(RESP_1);
END;
 

One of my developer colleague wanted to test this functionality, so we tested this on 10g.  And it really worked nicely.