问题描述:
tomcat8中response重定向时带json格式的参数 报错如下:
String json="{\"a\":\"b\",\"c\":\"d\"}";
response.sendRedirect("rsserver/jsp/ServerMonitor.jsp?resultMap="+json);
警告: Failed to redirect to [rsserver/jsp/ServerMonitor.jsp?resultMap={"durationTime":"0天0小时16分25秒","tcpServerURL":"192.163.20.79:9999","WSServerURL":"http://192.163.20.79:9899/RSServer","receiveTotal":"0","sendTotal":"0","receiveError":"0","switchStatus":"启动","sendFailed":"0"}]
java.lang.IllegalArgumentException:
index 41: rsserver/jsp/ServerMonitor.jsp?resultMap={"durationTime":"0天0小时16分25秒","tcpServerURL":"192.163.20.79:9999","WSServerURL":"http://192.163.20.79:9899/RSServer","receiveTotal":"0","sendTotal":"0","receiveError":"0","switchStatus":"启动","sendFailed":"0"}
at java.net.URI.create(Unknown Source)
......
解决方案:
需要对其中的“{“、”}”“\” ”、“,”、“:”进行转义。
String transferredResult =json.replaceAll("\\{","%7B")
.replaceAll("\\}","%7D")
.replaceAll("\"","%22")
.replaceAll("\\:","%3A")
.replaceAll("\\,","%2C");
response.sendRedirect("rsserver/jsp/ServerMonitor.jsp?resultMap="+transferredResult );
本文解决Tomcat 8中使用response.sendRedirect方法携带JSON格式参数时遇到的问题,通过URI编码确保参数正确传递。
8万+

被折叠的 条评论
为什么被折叠?



