将Servlet映射到希伯来语(UTF-8)URL模式

4
我使用的是Windows操作系统,Tomcat 8作为服务器,Netbeans 8作为IDE,JDK1.8.0_05为开发环境。我正在尝试为某些Servlet指定希伯来语的URL模式。我已经尝试过通过设置@webservlet注释的Urlpattern属性和将其放置在web.xml文件中两种方法,但是都无法正常工作。当Tomcat运行时,我检查了映射的情况(使用JConsole的MBeans选项卡),发现希伯来语的URL显示为乱码(具体来说是问号)。我已经尝试了以下几种方法:1. 在netbeans.conf文件中添加-J-Dfile.encoding=UTF-8;2. 将Windows区域设置更改为希伯来语;3. 在模式中使用URLEncoded版本的URL(这也显示为JConsole中的乱码符号);4. 还尝试以编码形式将URL输入地址栏(例如:localhost:8080/test/%D7%A2%D7%91);5. 检查Servlet文件的编码方式是否为UTF-8(在执行此列表中描述的第一个更改后保存)。我在所有URL模式(即“/*”)上都有一个过滤器,该过滤器将请求的字符编码设置为UTF-8(还尝试了Apache的SetCharacterEncodingFilter)。请问如何在Tomcat、Netbeans、Java、Windows环境下映射到带有希伯来语(UTF-8)的URL?谢谢。

尝试配置Tomcat以理解UTF8(默认情况下我认为charset = ISO-8859-1)https://dev59.com/xHVC5IYBdhLWcg3w9F89 - simar
您也可以尝试为Tomcat配置JVM。在startup.bat中设置“-J-Dfile.encoding=UTF-8”。 - simar
2个回答

0

您需要配置应用服务器以在utf-8中编码请求参数。由于您正在使用Tomcat,因此需要在conf/server.xml文件中设置URIEncoding="UTF-8"。以下是它应该看起来的样子:

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

2
在Tomcat 8中,URIEncoding默认为UTF-8。 - theyuv
如果服务器配置了“严格的Servlet兼容性”,则连接器的URIEncoding属性的默认值为“ISO-8859-1”,与旧版本的Tomcat相同。我猜这不是这种情况。但您可能希望确保org.apache.catalina.STRICT_SERVLET_COMPLIANCE系统属性未设置为true。 - MA--

-1

请按照以下步骤进行:

  1. 编写一个字符集过滤器,控制所有请求和响应:
    参考链接: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(){}
    }`
    
  2. 将此过滤器添加到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>
    
  3. 编写一些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. 在你的servlet中使用以下代码:

    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");

干杯!!


我编辑了这个问题。我还尝试过(我已经设置好了一个)设置一个过滤器来设置HttpServletRequest的字符编码。 - theyuv
这些都与字符编码有关,用于显示内容。不是吗?不包括URL。我的UTF-8内容可以正常显示(包括查询字符串)。 - theyuv
你尝试过URLEncoding.encode吗?我已经更新了我的答案。 - Ghayel
您正在对参数进行编码。我对参数没有任何问题。我的问题在于URL本身,具体来说是Servlet路径。 - theyuv
@BalusC,请删除您对我的帖子发表的不良评论,并在将抄袭归功于其他作品之前进行一些搜索。 - Ghayel
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接