get方法中的参数解码问题,JSP [英] Problem in decoding parameters in get method, JSP

查看:144
本文介绍了get方法中的参数解码问题,JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是如何调用jsp页面。

Here how jsp page is called.

eventMap.jsp?venue=%C4%B0ndigo

这是eventMap.jsp中的行,我得到venue参数:

This is the line in eventMap.jsp which I get venue parameter:

var venue="<%= URLDecoder.decode(request.getParameter("venue"),"UTF-8") %>";

不幸的是,这是最终的javascript中的结果:

Unfortunately this is the result in final javascript:

var venue="Ä°ndigo"; 

如何在jsp页面中正确使用编码,以获得正确的解码值(İngido)。

How can I use encoding properly in jsp page in order to get decoded value correctly (İngido).

编辑:
Whole eventMap.jsp

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结果:

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> 


推荐答案

GET查询字符串的解码由servletcontainer ,而不是由Servlet API。目前还不清楚你使用的servletcontainer,所以我不能给出详细的答案。在例如Tomcat中,可配置 > /conf/server.xml 中的< Connector> 元素中的> URIEncoding 。

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.

<Connector URIEncoding="UTF-8">

配置与其他servlet容器类似。

Configuration is similar in other servletcontainers.

然后你可以删除不必要的 URLDecoder 行。 getParameter()已返回解码的参数。

Then you can remove the unnecessary URLDecoder line. The getParameter() already returns the decoded parameter.

  • Unicode - How to get the characters right? - JSP/Servlet request

对于实际问题,我强烈建议使用EL替换 scriptlet ,并使用JSTL fn:escapeXml 来防止XSS攻击。

Unrelated to the actual problem, I strongly recommend to replace scriptlets by EL and use JSTL fn:escapeXml to prevent XSS attacks.

var lat = ${fn:escapeXml(param.lat)};
var lng = ${fn:escapeXml(param.lng)};
var venue = "${fn:escapeXml(param.venue)}";



另请参阅:




  • 如何避免JSP文件中的Java代码?

  • See also:

    • How to avoid Java code in JSP files?
    • 这篇关于get方法中的参数解码问题,JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆