Portál AbcLinuxu, 1. května 2025 01:16
(English summary at the end of the article) Uvažoval jsem, že upgradnu jetty, na kterém běží abíčko, z historické verze 4.1.20 na aktuální. Dlouho jsem se k tomu neměl, nejdříve kvůli chybě jsem měl opatchovanou verzi a nebyl jsem si jist, zda můj patch byl začleněn a pak u řady 5 chyběly startovací skripty. A vůbec, kdo by šťoural do něčeho, co funguje, že? Teď jsem se k tomu dostal, ale po odeslání formuláře jetty 6.0.2 byly znaky s diakritikou zničeny.
Chvíli jsem si s tím tedy hrál v debuggeru a skutečně metoda request.getParameter()
vracela deformovaná data, se kterými překódování pomocí new String(param.getBytes("ISO-8859-1"))
už nic nesvedlo. Zeptal jsem se v konferenci a dostal jsem pár tipů, například request.setEncoding("ISO-8859-2")
nebo System.setProperty("org.mortbay.util.URI.charset","ISO-8859-2")
. Nicméně tyto rady neměly žádný efekt.
Nakonec jsem si napsal testovací webovou aplikaci se servletem, který se dá konfigurovat, jaké kódování má requestu nastavit a dva jsp soubory s dvěma formuláři, které odesílají vstup (žížala) jednou přes POST podruhé přes GET. Jedno jsp bylo v ISO-8859-2 kódování, druhé v UTF-8. A jal jsem se testovat všechny možné kombinace, v jetty řady 4.1 i 6.0 a 6.1. Přitom jsem snifferem zjistil, že Firefox odesílá vstup jinak v závislosti na kódování HTML souboru (jak překvapivé, Watsone). Závěr je, že jetty 4 se chová jinak než jetty 6 a že pod jetty 6 spolehlivě fungují formuláře, pokud jsou ve stránce kódované pomocí UTF8, zato v ISO-8859-2 nefungují spolehlivě nikdy. Detaily najdete v příloze.
When I upgraded jetty from version 4.1.20 to 6.0.2 I found that it damages request parameters. The characters with caron from ISO-8859-2 encoding where replaced by ? character or disappeared at all. The trick with recoding new String(param.getBytes("ISO-8859-1"))
using did not work. I asked in jetty conference and tested few tips without luck: request.setEncoding("ISO-8859-2")
, System.setProperty("org.mortbay.util.URI.charset","ISO-8859-2")
. So I decided to test this topic thouroughly and wrote simple webapp consisting of two same jsp files and servlet dumping the request parameter. The jsp files contained two identical forms sending word 'žížala' either by GET or POST method to the servlet. One jsp file was encoded in UTF-8, the second in ISO-8859-2 encoding. I tested all combinations with both jetty versions and found difference between them. Jetty 6 can reliably accept accented characters if and only if the form was sent from HTML page encoded to UTF-8. When the page was encoded to ISO-8859-2 (latin2), then it is not possible to find setting suitable for both GET and POST methods. Test web application including complete results.
Tiskni
Sdílej:
QueryEncoding
(je to rozčíření Jetty), RequestCharacterEncoding
se u GETu vůbec nebere v úvahu. S následujícíém nastavením mi funguje testLatin2:
import org.mortbay.jetty.Request; ... protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException { if (requestEncoding != null) request.setCharacterEncoding(requestEncoding); (Request)request).setQueryEncoding("iso-8859-2"); String tmp = request.getParameter("note"); String tmp1 = new String(tmp.getBytes("ISO-8859-1")); // this is correct for request locale cs String tmp2 = new String(tmp.getBytes("ISO-8859-2")); String tmp3 = new String(tmp.getBytes("UTF-8")); response.setContentType("text/html; charset=ISO-8859-2"); Writer w = response.getWriter(); ...
decodeQueryTo
třídy org.mortbay.jetty.HttpURI
, musí tam být
public void decodeQueryTo(MultiMap parameters, String encoding) throws UnsupportedEncodingException { if (_query==_fragment) return; if (encoding==null) encoding=URIUtil.__CHARSET; if (StringUtil.isUTF8(encoding)) UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters); else UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding); }
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.