Map Servlet to Hebrew URL Pattern (UTF-8)

I am on Windows. Using tomcat 8. IDE - Netbeans 8. JDK1.8.0_05

I am trying to specify a Jewish url pattern for specific servlets. (I tried setting the Urlpatternannotation parameter @webservletand putting it in a file web.xml).

Jewish mapping does not work. I check what the mappings look like while Tomcat is running (using the MBeans tab in the JConsole), and the Hebrew url is displayed as gibberish (in particular, question marks).

I tried:

  • Adding -J-Dfile.encoding = UTF-8 to the netbeans.conf file.
  • Change Windows locales to Hebrew.
  • Using the URLEncoded version of url in the template (this also appears as gibberish symbol in JConsole).
  • I also tried to enter the URL into my encoded form in the address bar (for example: localhost: 8080 / test /% D7% A2% D7% 91).
  • I checked the encoding of the servlet files in notepad, they are saved as UTF-8 (after making the first change described in this list).
  • I have a filter on all url samples (for example: "/ *") that sets the request character encoding to UTF-8 (Apache also tried SetCharacterEncodingFilter)

Any suggestions on how I can map the Jewish (UTF-8) url to tomcat, netbeans, java, windows setup?

Thank.

+4
source share
2 answers

utf-8. Tomcat, URIEncoding="UTF-8" conf/server.xml. :

<Connector port="8080" maxHttpHeaderSize="8192"
 maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
 .......
 URIEncoding="UTF-8"
/>
0

:

  • Charset, :
    : https://github.com/edendramis/freemarker-example/blob/master/src/main/java/com/edendramis/config/CharsetFilter.java

     package charsetFilter.classes;
    
     import java.io.IOException;
     import javax.servlet.Filter;
     import javax.servlet.FilterChain;
     import javax.servlet.FilterConfig;
     import javax.servlet.ServletException;
     import javax.servlet.ServletRequest;
     import javax.servlet.ServletResponse;
    
    public class CharsetFilter implements Filter{
        private String encoding;
    
        public void init(FilterConfig config) throws ServletException{
                encoding = config.getInitParameter("requestEncoding");
                if( encoding==null ) encoding="UTF-8";
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain       next)
        throws IOException, ServletException{
            // Respect the client-specified character encoding
            // (see HTTP specification section 3.4.1)
                if(null == request.getCharacterEncoding())
                request.setCharacterEncoding(encoding);
                /**
            * Set the default response content type and encoding
            */
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
                next.doFilter(request, response);
        }
    
            public void destroy(){}
    }`
    
  • web.xml

     <filter>
            <filter-name>CharsetFilter</filter-name>
            <filter-class>charsetFilter.classes.CharsetFilter</filter-class>
                <init-param>
                    <param-name>requestEncoding</param-name>
                    <param-value>UTF-8</param-value>
                </init-param>
    </filter>
    
    <filter-mapping>
            <filter-name>CharsetFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  • HTML, :

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

  1. :

    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    response.setCharacterEncoding("UTF-8"); 
    
  2. :
    String input = new String(request.getParameter("foo").getBytes("iso-8859-1"), "utf-8"); String input = URLDecoder.decode(request.getParameter("keyWord"), "UTF-8"); System.out.println(input);

URL:

String str = "$ome UTF-8 text £900";
String url = "http://your-domain.com/url?p=" + URLEncoder.encode(str, "UTF-8");

!!

-1

All Articles