How do I retain the encoding in argument when using MessageFormat in Java

I am trying to use MessageFormat as follows,

String downloadsUrl = "http://host/downloads?tags={0}";
Object[] formatArgs = {"sequence%20diagram"};
String url = new MessageFormat(downloadsUrl).format(formatArgs);

However, when I look at the final url string, it is, http://host/downloads?tags=sequence diagram

Is there someway to retain the %20 and not have MessageFormat replace it with a space?

Answers


The code you provided does not add the space the code above returns "http://host/downloads?tags=sequence%20diagram"

Your target servlet is doing the substitution. Whatever "/downloads" is mapped to is parsing the tags parameter and performing url decoding. You can reconstruct possible encodings as follows. You will need to handle the UnsupportedEncodingException in the following.

String encoded = URLEncoder.encode( request.getParameter( name ), "UTF8" );

Unfortunately this is only a possible encoding and by default will convert spaces to "+". To get the "%20" back you will need to resort to

encoding = encoding.replaceAll( "+", "%20" );

This may work for you or not. In general it is more advisable to normalize on the decoded value instead of the encoded value as there are many possible encodings per decoded value.


Based on this I'm going to guess putting single quotes around the value would work...


Need Your Help

QPainter not active

c++ qt qpainter

The following code results in a bunch of errors:

Flex Modules Over MXML Component Architecture

flex

I am actually in the process of Architectural of our project. We have choosen Mate framework. The project is not so quite complex, but what advantages i would get over MXML when i choose Modules.

Modeling Recurring Transactions in Core Data

database-design core-data entity-relationship

I'm having a hard time coming up with a conceptual model for a recurring transaction scheme in Core Data. So far I have:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.