开发者

using HttpServlet to generate html-img source image -> how to request via POST?

I use a HttpServlet to generate an html image dependent on multiple IDs like this:

<img src="./someServlet?ids=123,124,125,126[...]" alt=""/>

someServlet extends from javax.servlet.http.HttpServlet overwriting the doGet()and doPost() methods. It sets the 开发者_高级运维response contenttype to img/png and uses the response outputstream to commit the generated image to the view.

The servlet mapping is done in web.xml:

<servlet>        
    <servlet-name>SomeServlet</servlet-name>
    <servlet-class>my.package.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>        
    <servlet-name>SomeServlet</servlet-name>
    <url-pattern>/someServlet</url-pattern>
</servlet-mapping>

My question: How do I send the request above via POST instead of GET? I tried surrounding it by the <form> tag setting the method to POST, but as expected, it didn't work.

EDIT: I need this because my request (with 5-10 UUIDS) exceeds the limit of a GET request


You cannot change an <img> element to send a POST request instead of GET. This makes no sense.

As per the comment on the question the query string length limit seems to be the main reason:

I want to use POST, because the get parameter length is limited. I commit 5-10 UUIds as a parameter, which exceeds the length of a GET request.

Just pass it as part of URL instead, as path info. So, instead of

<img src="someServlet?id1=123&id2=234&id3=345&id4=456&id5=567" alt=""/>

use

<img src="someServlet/123/234/345/456/567" alt=""/>

You only need to change your servlet's URL pattern to

<url-pattern>/someServlet/*</url-pattern>

and change the way to obtain the IDs as follows

String[] ids = request.getPathInfo().substring(1).split("/");
// ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜