JSP get方法中参数解码问题
这里jsp页面是如何调用的。
eventMap.jsp?venue=%C4%B0ndigo
这是 eventMap.jsp 中我获得场地参数的行:
var venue="<%= URLDecoder.decode(request.getParameter("venue"),"UTF-8") %>";
不幸的是,这是最终 javascript 的结果:
var venue="Ä°ndigo";
How can I use encodingproperly in jsp page in order to getcoded value正确(engido)。
编辑: 整个 eventMap.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.net.URLDecoder"%>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Event on Map></title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" charset="UTF-8">
function initialize() {
<% request.setCharacterEncoding("UTF-8"); %>
var lat=<%= request.getParameter("lat") %>;
var lng=<%= request.getParameter("lng") %>;
var venue="<%= URLDecoder.decode(request.getParameter("venue"),"UTF-8") %>";
var myLatlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello"
});
var infowindow = new google.maps.InfoWindow({ content: venue, size: new google.maps.Size(50,50)});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
Firebug 结果:
<!DOCTYPE html>
2
3
4<html>
5<head>
6<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
7<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
8<title>Event on Map</title>
9<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
10<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
11<script type="text/javascript" charset="UTF-8">
12 function initialize() {
13 var lat=41.062786;
14 var lng=28.981934;
15 var venue="Ä°Tà Kültür Sanat BirliÄi (KSB) Binası Küçük Salon (Maslak)";
16
17 var myLatlng = new google.maps.LatLng(lat,lng);
18 var myOptions = {
19 zoom: 15,
20 center: myLatlng,
21 mapTypeId: google.maps.MapTypeId.ROADMAP
22 }
23 var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
24
25 var marker = new google.maps.Marker({
26 position: myLatlng,
27 map: map,
28 title:"Hello"
29 });
30
31 var infowindow = new google.maps.InfoWindow({ content: venue, size: new google.maps.Size(50,50)});
32 google.maps.event.addListener(marker, 'click', function() {
33 infowindow.open(map,marker);
34 });
35
36 }
37</script>
38</head>
39<body onload="initialize()">
40 <div id="map_canvas"></div>
41</body>
42
43</html>
Here how jsp page is called.
eventMap.jsp?venue=%C4%B0ndigo
This is the line in eventMap.jsp which I get venue parameter:
var venue="<%= URLDecoder.decode(request.getParameter("venue"),"UTF-8") %>";
Unfortunately this is the result in final javascript:
var venue="Ä°ndigo";
How can I use encoding properly in jsp page in order to get decoded value correctly (İngido).
Edit:
Whole eventMap.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.net.URLDecoder"%>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Event on Map></title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" charset="UTF-8">
function initialize() {
<% request.setCharacterEncoding("UTF-8"); %>
var lat=<%= request.getParameter("lat") %>;
var lng=<%= request.getParameter("lng") %>;
var venue="<%= URLDecoder.decode(request.getParameter("venue"),"UTF-8") %>";
var myLatlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello"
});
var infowindow = new google.maps.InfoWindow({ content: venue, size: new google.maps.Size(50,50)});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
Firebug Result:
<!DOCTYPE html>
2
3
4<html>
5<head>
6<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
7<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
8<title>Event on Map</title>
9<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
10<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
11<script type="text/javascript" charset="UTF-8">
12 function initialize() {
13 var lat=41.062786;
14 var lng=28.981934;
15 var venue="Ä°Tà Kültür Sanat BirliÄi (KSB) Binası Küçük Salon (Maslak)";
16
17 var myLatlng = new google.maps.LatLng(lat,lng);
18 var myOptions = {
19 zoom: 15,
20 center: myLatlng,
21 mapTypeId: google.maps.MapTypeId.ROADMAP
22 }
23 var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
24
25 var marker = new google.maps.Marker({
26 position: myLatlng,
27 map: map,
28 title:"Hello"
29 });
30
31 var infowindow = new google.maps.InfoWindow({ content: venue, size: new google.maps.Size(50,50)});
32 google.maps.event.addListener(marker, 'click', function() {
33 infowindow.open(map,marker);
34 });
35
36 }
37</script>
38</head>
39<body onload="initialize()">
40 <div id="map_canvas"></div>
41</body>
42
43</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GET 查询字符串的解码由 servlet 容器处理,而不是由 Servlet API 处理。目前还不清楚你使用的是哪个servletcontainer,所以我无法给出详细的答案。例如,在 Tomcat 中,它可以通过
URIEncoding 可配置
属性。/conf/server.xml
中
元素中的其他 servlet 容器中的配置类似。
然后您可以删除不必要的
URLDecoder
行。getParameter()
已返回解码后的参数。另请参阅:
与实际问题无关,我强烈建议用 EL 替换 scriptlet 并使用 JSTL
fn:escapeXml
来防止 XSS 攻击。另请参阅:
Decoding of the GET query string is handled by the servletcontainer, not by the Servlet API. It's unclear which servletcontainer you're using, so I can't give a detailed answer. In for example Tomcat, it's configureable by
URIEncoding
attribute in the<Connector>
element in/conf/server.xml
.Configuration is similar in other servletcontainers.
Then you can remove the unnecessary
URLDecoder
line. ThegetParameter()
already returns the decoded parameter.See also:
Unrelated to the actual problem, I strongly recommend to replace scriptlets by EL and use JSTL
fn:escapeXml
to prevent XSS attacks.See also:
您的 JSP 标头是什么样的?
确保它显示如下内容:
您还可以检查调用 JSP 时获得的 HTTP 标头。确保您的响应是 UTF-8 编码的。
What does your JSP header look like?
Make sure it says something like:
You can also check HTTP headers you are getting when invoking your JSP. Make sure your response is UTF-8 encoded.
也许这个方法覆盖了错误的编码:
request.setCharacterEncoding("UTF-8")
也许您应该检查 JSP 标头或请求标头中要使用的编码。
查理
Maybe this method is overriding the wrong encoding:
request.setCharacterEncoding("UTF-8")
Probably you should check the encoding to be used in your JSP header or in the request header.
Charlie