How can I post parameters for JSTL import tag (<c:import>)?

旧巷老猫 提交于 2019-12-02 01:46:39

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.

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`.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!