I am on a windows OS. Using tomcat 8. IDE - Netbeans 8. JDK1.8.0_05
I am trying to specify a hebrew url pattern for certain servlets. (I have tried both by setting the Urlpattern
attribute of the @webservlet
annotation and by placing in the web.xml
file).
The Hebrew mapping doesn't work. I check to see what the mappings look like while Tomcat is running (By using the MBeans tab of JConsole) and the Hebrew url is displayed as gibberish (specifically, question marks).
I have tried:
SetCharacterEncodingFilter
) Any suggestions on how I can map to a hebrew (UTF-8) url with tomcat, netbeans, java, windows setup?
Thanks.
You need to configure your application server to encode request parameters in utf-8. Since you are using Tomcat, that would be setting URIEncoding="UTF-8"
in your conf/server.xml file. Here's what it should look like:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
.......
URIEncoding="UTF-8"
/>
Follow these steps:
Write a Charset filter that will control all requests and responses:
Reference: 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(){} }`
Add this filter to 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>
Write some HTML code eg:
<?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' />
Use the following in your servlet:
request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8");
For getting String:
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);
For URL:
String str = "$ome UTF-8 text £900";
String url = "http://your-domain.com/url?p=" + URLEncoder.encode(str, "UTF-8");
Cheers!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.