如何发布JSTL导入标记的参数(<c:import>)?</c:import>

时间:2009-06-11 18:27:14

标签: jsp jstl taglib

我目前正在JSP页面中使用JSTL标记来导入外部页面的内容:

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

不幸的是,参数现在变得越来越长。由于它们在URL中编码为 GET 参数,因此我现在收到“414:Request-URL too Large”错误。有没有办法 POST 参数到外部URL?也许使用不同的标签/标签库?

2 个答案:

答案 0 :(得分:3)

通过http://www.docjar.com/html/api/org/apache/taglibs/standard/tag/common/core/ImportSupport.java.htmlhttp://www.docjar.com/html/api/org/apache/taglibs/standard/tag/el/core/ImportTag.java.html查看后,我得出的结论是,您无法使用import标记执行POST请求。

我想你唯一的选择是使用自定义标签 - 编写一个apache httpclient标签应该非常容易,该标签需要一些POST参数并输出响应文本。

答案 1 :(得分:1)

为此,您需要Servlet java.net.URLConnection

基本示例:

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`.
相关问题