From: Sam Ruby Date: Sat, 1 Apr 2000 17:57:42 +0000 (+0000) Subject: Accomodate back level (JSDK 2.0) implementations X-Git-Tag: php-4.0RC2~523 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=34933225f63aba3598fd50d754e7dacd997e06d5;p=php Accomodate back level (JSDK 2.0) implementations --- diff --git a/sapi/servlet/servlet.java b/sapi/servlet/servlet.java index 817957d622..1ffc32bbc1 100644 --- a/sapi/servlet/servlet.java +++ b/sapi/servlet/servlet.java @@ -24,6 +24,8 @@ import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; +import java.lang.reflect.Method; + public class servlet extends HttpServlet { char slash=System.getProperty("file.separator").charAt(0); @@ -34,6 +36,7 @@ public class servlet extends HttpServlet { static int startup_count = 0; protected boolean display_source_mode = false; + private Method addHeader; /******************************************************************/ /* native methods */ @@ -78,6 +81,10 @@ public class servlet extends HttpServlet { } void header(String data) { + + // try to send the header using the most specific servlet API + // as possible (some servlet engines will add a content type + // header unless the setContentType method is called). try { if (data.startsWith("Content-type: ")) { response.setContentType(data.substring(data.indexOf(" ")+1)); @@ -86,22 +93,27 @@ public class servlet extends HttpServlet { } else { int colon = data.indexOf(": "); if (colon > 0) { - response.addHeader(data.substring(0,colon), - data.substring(colon+2)); + try { + addHeader.invoke(response, new Object[] + { data.substring(0,colon), data.substring(colon+2) } ); + } catch (Exception e) { + e.printStackTrace(System.err); + } } else { response.getWriter().println(data); } } } catch (IOException e) { - System.err.print(data); + e.printStackTrace(System.err); } + } void write(String data) { try { response.getWriter().print(data); } catch (IOException e) { - System.err.print(data); + e.printStackTrace(System.err); } } @@ -112,6 +124,24 @@ public class servlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); if (0 == startup_count++) startup(); + + // try to find the addHeader method (added in the servlet API 2.2) + // otherwise settle for the setHeader method + try { + Class c = Class.forName("javax.servlet.http.HttpServletResponse"); + Method method[] = c.getDeclaredMethods(); + for (int i=0; i