tags:

views:

1186

answers:

2

I'm currently using JSTL tag in a JSP page to import the content of an external page:

<c:import url="http://some.url.com/"&gt;
   <c:param name="Param1" value="<%= param1 %>" />
   ...
   <c:param name="LongParam1" value="<%= longParam1 %>" />
</c:import>

Unfortunately the parameters are now getting longer. Since they are encoded as GET parameters in the URL, I am now getting "414: Request-URL too Large" error. Is there a way to POST the parameters to the external URL? Maybe using a different tag / tag library?

+1  A: 

After looking thru http://www.docjar.com/html/api/org/apache/taglibs/standard/tag/common/core/ImportSupport.java.html and http://www.docjar.com/html/api/org/apache/taglibs/standard/tag/el/core/ImportTag.java.html , i ve come to the conclusion that you cannot do a POST request using the import tag.

I guess the only choice you have is to use a custom tag - it should be pretty easy to write an apache httpclient tag that takes some POST param and output the response text.

Chii
+1  A: 

You'll need a Servlet with java.net.URLConnection for this.

Basic example:

String url = "http://example.com";
String charset = "UTF-8";
String query = String.format("Param1=%s&LongParam1=%d", param1, longParam1);

URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");

OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset);
    writer.write(query);
} finally {
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}

InputStream result = urlConnection.getInputStream();
// Now do your thing with the result.
// Write it into a String and put as request attribute
// or maybe to OutputStream of response as being a Servlet behind `jsp:include`.
BalusC