URI encoding for custom servlets

156 views
Skip to first unread message

Nikolay Samofatov

unread,
Oct 15, 2008, 3:58:03 AM10/15/08
to Google Web Toolkit
Hi, All!

GWT 1.5.2/Windows.

We are trying to implement servlets (e.g.: public class ExcelServlet
extends HttpServlet) to serve generated Excel files for GWT
applications.

We pass certain parameters for servlet via URI encoding, e.g.

link.append("<a target=\"_blank\" href=\"./excelServlet?");

link.append("session=").append(URL.encodeComponent(ServerCall.getSessionGuid()));

link.append("&guid=").append(URL.encodeComponent(componentMeta.guid));

link.append("&view=").append(URL.encodeComponent(viewPanel.viewMeta.caption)); //
good stuff here
link.append("&archive=").append(isArchive);

link.append("&params=").append(URL.encodeComponent(createParametersString().toString()));
link.append("\">");
....
link.append("</a>");
...
setHTML...
...


The problem appears with non-Latin1 (e.g. Cyrillic) code points
encoded in URI components, e.g.:

public void doGet(
HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException
{
//request.setCharacterEncoding("UTF8");

String view = request.getParameter("view"); // Garbage here


When Servlet receives them in Hosted mode they become garbage, because
Servlet interpreted them as ISO Latin1, instead of UTF8. Using
setCharacterEncoding doesn't help either, even with filters.

In regular Tomcat I can set URIEncoding attribute for connector and
everything works correctly:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

What is the proper way to set URIEncoding connector attribute for
hosted mode Tomcat?
I couldn't figure out where should I put server.xml and how to set it
up.

The following workarounds would work:
1) do - new String(view.getBytes(...)...) to repair scrambled string
2) parse getQueryString manually (long live CGI!)

Using Mbeans to manipulate connector may also work, but I didn't try.

But I'd prefer not to use dirty hacks to work around the problem.
Shouldn't basic Servlet functionality also work in hosted mode?

Nikolay Samofatov

Nikolay Samofatov

unread,
Oct 16, 2008, 11:51:30 AM10/16/08
to Google Web Toolkit
Hi, All!

Never mind my question. I so far have found decent workarounds for GWT
hosted mode servlet character encoding issues.
This is how the file generator code looks like:

public class ExcelServlet extends HttpServlet {

public void doGet(
HttpServletRequest request,
HttpServletResponse response
) ....

// Unfortunately, these two are broken in GWT Hosted mode
// request.setCharacterEncoding("utf-8");
// response.setCharacterEncoding("utf-8");
....

// Under GWT Hosted mode request.getParameter family returns
gibberish
// Parse query string manually using String.split("\\&") +
URLDecoder.decode(value, "utf-8")
Map<String,String> params =
getInputQueryParameters(request.getQueryString(), "\\&", true);
...
// Avoid using UTF-8 in MIME headers, because GWT hosted mode
Tomcat doesn't let us
// specify their encoding. Use percent-encoded form instead:

// Use UTF-8 for filename encoding
if (userAgent.contains("MSIE") || userAgent.contains("Chrome"))
{
// non-standard plain encoding scheme works on IE 6/7 and
Chrome (tested 0.2.149.30)

sendFile.append("filename=").append(URLEncoder.encode(sendFileName,
"utf-8"));
}
else {
// This is RFC2231. In theory should work everywhere, but in
practice IE6 and Google Chrome are
// not compliant. Opera 9.6 and FF3 tested ok.

sendFile.append("filename*=UTF-8''").append(URLEncoder.encode(sendFileName,
"utf-8"));
}
response.setHeader("Content-Disposition", sendFile.toString());
....

Thanks!

Nikolay Samofatov


On Oct 15, 3:58 am, Nikolay Samofatov
> In regular Tomcat I can setURIEncodingattribute for connector and
> everything works correctly:
>     <Connector port="8080" protocol="HTTP/1.1"
>                connectionTimeout="20000"
>                redirectPort="8443"URIEncoding="UTF-8"/>
>
> What is the proper way to setURIEncodingconnector attribute for
Reply all
Reply to author
Forward
0 new messages