2

我目前在 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:请求 URL 太大”错误。有没有办法将参数发布到外部 URL?也许使用不同的标签/标签库?

4

2 回答 2

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 请求。

我想您唯一的选择是使用自定义标签 - 编写一个带有一些 POST 参数并输出响应文本的 apache httpclient 标签应该很容易。

于 2009-10-20T07:59:16.167 回答
1

你需要一个Servletjava.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`.
于 2009-12-15T16:25:02.347 回答