在做页面开发时使用了Javascript的escape()函数进行URL编码,结果在JSP或Servlet中用request.getParameter()时取不到参数,只返回一个空值。经过Baidu的搜索终于找到了问题所在及解决的办法。
本来Javascript中的escape()是将中文按ISO-8859-1字符集进行URL编码的,那样通过 request.getParameter()是能直接获取到请求参数的,但后来的Javascript将escape()换成了Unicode字符集编 码,如此一来,在JSP和Servlet中就没法直接拿到请求参数了,具体原因我也不知道。
解决办法:
1、首先对中文字符进行两次escape()编码,如要传参数name,值为“你好”,则url的格式为....name=escape(escape("你好")),这样一来,在request.getParameter()就能取到编码后的参数了。
2、由于取到的参数是 %25u4F60%25u597D 格式的,没法用常规的URLDecoder.decode()来进行解码,还好,这世上的牛人够多,在网上直接找到了一个工具类,能实现 Javascript中escape()及unescape()式的编解码,源代码如下:
public
class
Escape
...
{
private
final
static
String[] hex
=
...
{
"
00
"
,
"
01
"
,
"
02
"
,
"
03
"
,
"
04
"
,
"
05
"
,
"
06
"
,
"
07
"
,
"
08
"
,
"
09
"
,
"
0A
"
,
"
0B
"
,
"
0C
"
,
"
0D
"
,
"
0E
"
,
"
0F
"
,
"
10
"
,
"
11
"
,
"
12
"
,
"
13
"
,
"
14
"
,
"
15
"
,
"
16
"
,
"
17
"
,
"
18
"
,
"
19
"
,
"
1A
"
,
"
1B
"
,
"
1C
"
,
"
1D
"
,
"
1E
"
,
"
1F
"
,
"
20
"
,
"
21
"
,
"
22
"
,
"
23
"
,
"
24
"
,
"
25
"
,
"
26
"
,
"
27
"
,
"
28
"
,
"
29
"
,
"
2A
"
,
"
2B
"
,
"
2C
"
,
"
2D
"
,
"
2E
"
,
"
2F
"
,
"
30
"
,
"
31
"
,
"
32
"
,
"
33
"
,
"
34
"
,
"
35
"
,
"
36
"
,
"
37
"
,
"
38
"
,
"
39
"
,
"
3A
"
,
"
3B
"
,
"
3C
"
,
"
3D
"
,
"
3E
"
,
"
3F
"
,
"
40
"
,
"
41
"
,
"
42
"
,
"
43
"
,
"
44
"
,
"
45
"
,
"
46
"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3F
,
0x3F
,
0x3F
,
0x3F
,
0x3F
,
0x0A
,
0x0B
分享到:
相关推荐
在JavaScript中进行GB2312编码和解码可以帮助我们正确地在不同环境间传输和展示中文字符。 首先,让我们理解GB2312编码的工作原理。GB2312编码是基于两个字节的,每个汉字或符号由两个连续的字节表示。对于一个GB...
在本资源中,我们将详细介绍JavaScript和JavaWeb对汉字编码的策略,包括escape、encodeURI、encodeURIComponent等编码函数的使用,以及对应的解码函数unescape、decodeURI、decodeURIComponent的使用。 一、escape...
标题中的知识点主要涉及的是在PHP中如何正确地解码JavaScript的`escape`编码。JavaScript的`escape`函数用于对字符串进行编码,以便在URL、HTML或其他非文本上下文中使用。编码后的字符串会将某些特殊字符替换为...
在Java和JavaScript中,Base64编码和解码是两个重要的功能,用于处理这种编码格式。下面将详细阐述这两个编程语言中实现Base64编码和解码的方法。 首先,让我们关注Java。在Java 8及更高版本中,已经内置了`java....
- **escape**:这指的是JavaScript中的`escape()`函数,它用于对字符串进行编码,特别是在发送数据到服务器或在URL中使用时。 - **javascript**:JavaScript是Web开发中常用的客户端脚本语言,与ASP一起,可能被这个...
通过上述讨论,我们不仅了解了如何在Java中实现JavaScript的`escape`和`unescape`功能,还深入了解了编码与解码的基本原理。这对于跨语言的数据处理和网络通信具有重要的实践意义。掌握这些技巧,能够帮助开发者更加...
JavaScript 对 URL 编码解码 JavaScript 中对 URL 编码和解码涉及到六个函数:escape、encodeURI、...理解和正确使用 JavaScript 中的编码和解码函数是非常重要的,以便在 web 开发中正确地传递和处理数据。
`unescape()`可以解码由`escape()`函数编码的字符串,但这两个函数在现代JavaScript中已不推荐使用,取而代之的是更安全的`encodeURI()`、`encodeURIComponent()`、`decodeURI()`和`decodeURIComponent()`。...
Escape编码的核心是使用反斜杠(\)作为前缀,接着是字符的特定表示,比如在JavaScript中,非ASCII字符、特殊字符如换行符(\n)、制表符(\t)、双引号(\")等都会被转义。在其他编程语言中,如Python,可能会使用类似"\...
- **一次转码**:使用`encodeURI()` 对URL进行编码,然后在服务器端使用适当的解码方法,如Java中的`new String(name.getBytes("ISO-8859-1"), "UTF-8")`,确保从ISO-8859-1转回UTF-8。 - **二次转码**:先使用`...
而在Java中,我们通常使用`java.net.URLDecoder.decode()`来解码URL编码的字符串,将`%`符号后面跟随的十六进制数字转换回相应的字符。 `EscapeUnescape.java`文件可能包含了演示`escape`和`unescape`功能的示例...
总结起来,当遇到"java.io.CharConversionException: isHexDigit"异常时,应该检查前端传递的URL参数是否正确编码,并确保在服务器端使用正确的解码方法和字符集。使用`encodeURI`或`encodeURIComponent`函数代替`...
在JavaScript编程中,`escape()` 和 `unescape()` 是两个常用的字符串处理函数,它们主要用于对字符串进行编码和解码。然而,在处理汉字等多字节字符时,如果不注意编码环境,可能会导致乱码问题。本篇文章将深入...
在Java中,我们可以使用`String`类的`getBytes()`方法配合指定的字符集进行编码转换,如`getBytes("UTF-8")`。编码的目的是为了将不可见的二进制数据转换成可读的字符形式,或者为了适应特定环境或平台的字符表示...
- **JavaScript编码**:可能包含使用JavaScript内置的`encodeURI()`、`encodeURIComponent()`、`escape()`等函数进行编码的示例,这些函数常用于处理URL或特殊字符。 - **Unicode与ASCII**:JavaScript中的字符串...
本文将详细介绍JavaScript中的字符集,包括字符集的定义、字符与字节的关系、常见字符集类型、字符集编码与解码的过程、以及如何在浏览器中应用字符集编码。 一、字符集基础 字符集是由一套字符所组成的集合,每个...
在发送请求之前,可以先将中文字符串转换为GBK编码,然后使用"escape"函数进行编码。在接收端,再将其解码回GBK,从而避免乱码。 以下是一个简单的示例,展示了如何使用JavaScript处理AJAX请求中的中文乱码问题: ...
JavaScript中的`escape`和`unescape`函数是处理字符串编码和解码的两个关键方法,尤其是在处理包含非ASCII字符(如中文字符)的字符串时。这两个函数在处理字符串时遵循特定的规则,对于理解JavaScript字符串编码...