2009-06-11 7 views
2

私は現在、外部のページのコンテンツをインポートするには、JSPページでJSTLタグを使用しています:JSTLインポートタグ(<c:import>)のパラメータを投稿するにはどうすればよいですか?

<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が大きすぎます」というエラーが表示されています。方法はありますかPOST外部URLへのパラメータですか?たぶん別のタグ/タグライブラリを使用していますか?

答えて

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タグを書くのは簡単です。

1

これにはServletjava.net.URLConnectionが必要です。

Basicの例:

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`.