I am not sure about your scenario. In Java, String is Unicode, deals only with charset conversion, when you need to convert from / to String to / from binary representation. In your example, when getSimpleParamValue ("some_input_param_from_get") is called, inputData should already have the "correct" string, the conversion from the byte stream (which was moved from the client browser to the web server) to the string should already be done part (web server responsibility + web tier of your application). To do this, I use UTF-8 for webcasting by placing a filter in web.xml (before Struts), for example:
public class CharsetFilter implements Filter { public void destroy() {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; req.setCharacterEncoding("UTF-8"); chain.doFilter(req, res); String contentType = res.getContentType(); if( contentType !=null && contentType.startsWith("text/html")) res.setCharacterEncoding("UTF-8"); } public void init(FilterConfig filterConfig) throws ServletException { } }
If you cannot do this, and if your getSimpleParamValue () is “mistaken" in the encoding conversion (for example: it suggested that the byte stream was UTF-8 and was Windows-1250), now you have the “wrong” line, and you should try to restore it by canceling and converting the byte to string conversion - in this case you should know the wrong AND correct encoding - and, even worse, deal with the possibility of missing characters (if it was interpreted as UTF8, I maight found an illegal char sequence ) If you have to deal with this in the Struts2 action, I would say that you have problems, you should deal with it explicitly before / after it (in the top web layer - either in the database driver or in the file encoding or whatever something else)
leonbloy
source share