<small id='Eo8CX'></small><noframes id='Eo8CX'>

      <i id='Eo8CX'><tr id='Eo8CX'><dt id='Eo8CX'><q id='Eo8CX'><span id='Eo8CX'><b id='Eo8CX'><form id='Eo8CX'><ins id='Eo8CX'></ins><ul id='Eo8CX'></ul><sub id='Eo8CX'></sub></form><legend id='Eo8CX'></legend><bdo id='Eo8CX'><pre id='Eo8CX'><center id='Eo8CX'></center></pre></bdo></b><th id='Eo8CX'></th></span></q></dt></tr></i><div id='Eo8CX'><tfoot id='Eo8CX'></tfoot><dl id='Eo8CX'><fieldset id='Eo8CX'></fieldset></dl></div>
    1. <legend id='Eo8CX'><style id='Eo8CX'><dir id='Eo8CX'><q id='Eo8CX'></q></dir></style></legend>
    2. <tfoot id='Eo8CX'></tfoot>
        <bdo id='Eo8CX'></bdo><ul id='Eo8CX'></ul>

        request.getParameter() 在 java servlet 中无法正确显示字符编码

        时间:2023-09-25
      1. <i id='iWQxh'><tr id='iWQxh'><dt id='iWQxh'><q id='iWQxh'><span id='iWQxh'><b id='iWQxh'><form id='iWQxh'><ins id='iWQxh'></ins><ul id='iWQxh'></ul><sub id='iWQxh'></sub></form><legend id='iWQxh'></legend><bdo id='iWQxh'><pre id='iWQxh'><center id='iWQxh'></center></pre></bdo></b><th id='iWQxh'></th></span></q></dt></tr></i><div id='iWQxh'><tfoot id='iWQxh'></tfoot><dl id='iWQxh'><fieldset id='iWQxh'></fieldset></dl></div>

        <tfoot id='iWQxh'></tfoot>

        <small id='iWQxh'></small><noframes id='iWQxh'>

              <legend id='iWQxh'><style id='iWQxh'><dir id='iWQxh'><q id='iWQxh'></q></dir></style></legend>
                <bdo id='iWQxh'></bdo><ul id='iWQxh'></ul>

                  <tbody id='iWQxh'></tbody>
                  本文介绍了request.getParameter() 在 java servlet 中无法正确显示字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Java servlet 文件中的 UTF-8 存在一些问题.当我在 URL 中获取参数值时,我对 UTF-8 字符有一些问题.无法正确显示日文字符.

                  I have some problem with UTF-8 in java servlet file. When I get the parameter value in the URL, I have some problem with UTF-8 characters. It does not display properly Japanese Characters.

                  jsp头已经有

                  <%@ page language="java" contentType="text/html; charset=UTF-8"
                  pageEncoding="UTF-8"%>
                  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                  

                  我将连接器中的 URIEncoding 设置添加到 server.xml 中的 UTF-8.

                  I added the URIEncoding setting in the connector to UTF-8 in server.xml.

                  <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
                  

                  我在jsp中写了如下代码

                  I wrote the code as the following in jsp.

                  <s:textfield key="txt_name" name="txt_name" id="txt_name"
                  maxlength="64"></s:textfield>
                  
                  <a href="javascript:showModalWindow('PopUpFile!init.action?<%=Common.PASSWORD%>=<%=Common.encript(ID, Code)%>','',940,650);">
                  <s:property value="PopUp Link" />
                  </a>
                  
                  <script>
                      function showModalWindow(x_URL, x_ARG, x_WIDTH, x_HEIGHT) {
                          var x_OPT = "dialogHeight: " + x_HEIGHT + "px; " + "dialogWidth: "
                                  + x_WIDTH + "px; "
                                  + "edge: Raised; center: Yes; resizable: Yes; status: Yes;";
                          x_URL += "&name="+document.getElementById("txt_name").value;
                          var retValue = window.showModalDialog(x_URL, x_ARG, x_OPT);
                          if (retValue != null) {
                              document.forms.frm.action = "ParentFile!getUser.action";
                              document.forms.frm.submit();
                          }
                      }
                  </script>
                  

                  然后,我在java servlet中编写了如下代码.

                  And then, I wrote the code as the following in java servlet.

                  if(g_request.getParameter("name") != null){
                      g_session.setAttribute(NAME, g_request.getParameter("name"));
                  }
                  

                  我还在 java servlet 中使用 request.setCharacterEncoding() 方法进行了测试,但它并没有真正起作用.虽然我尝试了很多方法来回答其他人在stackoverflow中与servlet中字符编码相关的问题,但我无法解决我的问题.

                  I also tested with request.setCharacterEncoding() method in java servlet but it doesn't really work. Although I tried many ways from answers of the other people's problem related with character encoding in servlet in stackoverflow, I can't solve my problem until.

                  如何正确显示字符编码?提前致谢.

                  What can I do to display correctly the character encoding? Thanks in advance.

                  推荐答案

                  大多数服务器,包括 Apache Tomcat 服务器,默认配置为使用 ISO-8859-1 进行参数编码.我认为除非您拥有私有专用服务器实例,否则您不会更改此设置.因此,程序员的技术是手动编码/解码这些参数.因为您使用的是 javascript,所以有 encodeURI()encodeURIComponent() 内置函数.请参阅如何在 JavaScript 中编码 URL.代码应该改变

                  Most servers, including Apache Tomcat server, are configured to parameter encoding with ISO-8859-1 by default. I think you won't change this unless you have a private dedicated server instance. So, the programmer's technique is to encode/decode those parameters manually. Because you are using javascript, there's encodeURI() or encodeURIComponent() built-in function. See How to encode a URL in JavaScript. The code should change

                  x_URL += "&name="+encodeURI(document.getElementById("txt_name").value);
                  

                  在Java中使用URLDecoder将参数解码回来.

                  in the Java use the URLDecoder to decode parameter back.

                  java.net.URLDecoder.decode(((String[])request.getParameterMap().get("name"))[0], "UTF-8"));
                  

                  注意,如果您使用的是 Struts2 dispatcher 结果类型,那么您不需要解码查询字符串中的参数.这些参数通过 UrlHelper 解析.

                  Note, if you are using Struts2 dispatcher result type then you don't need to decode parameters in the query string. Those parameters are parsed via UrlHelper.

                  但是,我不记得我什么时候解码这些参数是在 Struts2 中自动解码的.

                  However, I don't remember when I decode those parameters are automatically decoded in Struts2.

                  作为一项规则,您应该知道,如果您在 URL 中传递参数,它们应该是 URL 编码的.如果您提交表单,则无需这样做,因为表单是 x-www-form-urlencoded,请参阅 17.13.4 表单内容类型.

                  As a rule you should know that if you pass parameters in the URL they should be URL encoded. If you submit the form there's no need to do it because the form is x-www-form-urlencoded, see 17.13.4 Form content types.

                  这篇关于request.getParameter() 在 java servlet 中无法正确显示字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Maven 3 工件问题 下一篇:Struts 2 中的验证概念理解

                  相关文章

                    <legend id='M3wHG'><style id='M3wHG'><dir id='M3wHG'><q id='M3wHG'></q></dir></style></legend>
                    1. <tfoot id='M3wHG'></tfoot>

                      <i id='M3wHG'><tr id='M3wHG'><dt id='M3wHG'><q id='M3wHG'><span id='M3wHG'><b id='M3wHG'><form id='M3wHG'><ins id='M3wHG'></ins><ul id='M3wHG'></ul><sub id='M3wHG'></sub></form><legend id='M3wHG'></legend><bdo id='M3wHG'><pre id='M3wHG'><center id='M3wHG'></center></pre></bdo></b><th id='M3wHG'></th></span></q></dt></tr></i><div id='M3wHG'><tfoot id='M3wHG'></tfoot><dl id='M3wHG'><fieldset id='M3wHG'></fieldset></dl></div>

                      <small id='M3wHG'></small><noframes id='M3wHG'>

                      • <bdo id='M3wHG'></bdo><ul id='M3wHG'></ul>