Mr Morgan
Mr Morgan

Reputation: 2243

Passing a url in a form from page to servlet drops part of query string

If I have a form on a JSP like this:

<form action = "/myApp/myServlet?rssFeedURL=${rssFeedURL}' />" method = "post">
    <input type = "button" value = "See data for this RSS feed."/>
</form>   

What I find is that if the variable ${rssFeedURL} has no query string, then the server receives it properly, e.g.:

http://feeds.bbci.co.uk/news/rss.xml

But if a query string exists, e.g.:

http://news.google.com/news?ned=us&topic=m&output=rss  

I expect that it is to do with the encoding of the '&' character. Can anyone advise?

The server receives only:

http://news.google.com/news?ned=us

My pages are charset=UTF-8 encoded.

Upvotes: 0

Views: 148

Answers (1)

BalusC
BalusC

Reputation: 1108642

You need to URL-encode request parameters. Otherwise they will be interpreted as part of the initial request URL.

JSTL offers you the <c:url> for this.

<c:url var="formActionURL" value="/myApp/myServlet">
    <c:param name="rssFeedURL" value="${rssFeedURL}" />
</c:url>

<form action= "${formActionURL}" method="post">
    ...

An alternative is to create an EL function which delegates to URLEncoder#encode().

Upvotes: 2

Related Questions