Eclipse+Tomcat URL传中文字符报错400

今天在jsp中用href传值(中文字符),在Eclipse内置浏览器出错,但在火狐可以正常运行,搞了一天终于解决了,记录一下。

先看下代码:

<a href = "Practice_Choose?name=<%=name%>&idCollege=<%=idCollege%>&idStudent=<%=idStudent%>">在线练习</a><br>

其中name是中文字符,如果不传name可以正常运行。控制台信息:

Error parsing HTTP request header

Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

看到说有无效字符,跟着百度在Servers/server.xml下找Connector(不太清楚是否能自定义一个,我找的是端口=8080的,添加属性maxHttpHeaderSize=81920(80K),修改protocol="org.apache.coyote.http11.Http11Protocol"(有人说协议要写全))


在D:\Apache Tomcat 7.0\webapps\docs\config\http.html文档中看到了connector的属性:


问题依旧存在。改完了之后个人感觉上面那些并没有什么用。


下面用js的encodeURI函数完美解决了:

<a href = "javascript:location.href=encodeURI('Practice_Choose?name=<%=name%>&idCollege=<%=idCollege%>&idStudent=<%=idStudent%>')">在线练习走一走</a>

javascript:调用js函数,location.href=跳转,encodeURI对字符串编码,Practice_Choose是Servlet名,后面是我的三个参数。

界面可以成功跳转,把encodeURI括号里面的字符串编码成了这样:

Practice_Choose?name=%E5%BC%A0%E5%AD%90%E5%81%A5&idCollege=53&idStudent=1

就这么一个非常简单的用法,因为不了解困扰了一天,路漫漫其修远。


P.S.

1.Connector


2.另外百度了一番,看到了我这个的下一步,也就是如何正确接收中文字符(避免乱码):

String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
对于2.我在Server.xml中的Connector(port=8080)添加URIEncoding="UTF-8",就不再需要了,直接用request.getParameter("name")就可以了。


猜你喜欢

转载自blog.csdn.net/qq_31293215/article/details/79621889