急啊,不改tomcat怎么解决url传中文参数问题
url传中文参数会出问题,不改tomcat的配置文件怎么作呢?我的myeclipse为utf-8
jsp是引用统一的页头界面也是utf-8,这都是不能改的。
我用了
String Words=request.getParameter( "searchValue ");
String sStr =new String(Words.getBytes( "8859_1 "), "gb2312 ");
和
String ys = new String(request.getParameter( "searchValue ").getBytes( "ISO-8859-1 "));都没用阿,url显示的是...searchValue=阿阿。
但到action里就是乱码,页面也是
------解决方案--------------------既然你页面用的是utf-8那你就应该统一转成utf-8编码
String Words=request.getParameter( "searchValue ");
String sStr =new String(Words.getBytes( "8859_1 "), "utf-8 ");
或者request.setCharacterEncoding( "utf-8 ");
------解决方案-------------------- <%request.setCharacterEncoding( "utf-8 ");%>
<%=request.getParameter( "text1 ") %>
------解决方案--------------------加过滤器可以解决的
------解决方案--------------------用native2ascii转码试试
url显示的是...searchValue=\u963f\u963f
------解决方案--------------------tomcat里的url中文参数问题一定要设置tomcat的server.xml文件
1) 打开tomcat的server.xml文件,找到区块,加入如下一行:
URIEncoding= "UTF-8 "
完整的应如下:
<Connector
port= "80 " maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
debug= "0 " connectionTimeout= "20000 "
disableUploadTimeout= "true "
URIEncoding= "UTF-8 "
/>
2)重启tomcat,就OK了。
执行如下jsp页页测试是否成功
tcnchar.jsp
<%@ page contentType= "text/html;charset=UTF-8 "%>
<%@ page import= "java.util.* "%>
<%
String q=request.getParameter( "q ");
q = q == null? "没有值 " : q;
%>
<HTML>
<HEAD> <TITLE> URL Test </TITLE>
<META http-equiv=Content-Type content= "text/html; charset=UTF-8 ">
<META http-equiv=pragma content=no-cache>
<body>
你提交了:
<%=q%>
<br>
<form action= "tcnchar.jsp " method= "post ">
输入中文: <input type= "text " name= "q "> <input type= "submit " value= "确定 ">
<br>
<a href= "tcnchar.jsp?q=中国 "> 通过get方式提交 </a>
</form>
</BODY> </HTML>
上面代码中的post方式可以通过Filter来处理
/**EncodingFilter.java*/
public class EncodingFilter implements Filter {
protected FilterConfig filterConfig;
private String targetEncoding = "UTF-8 ";
public void init(FilterConfig config) throws
ServletException {
this.filterConfig = config;
this.targetEncoding = config.getInitParameter( "encoding ");
}
public void doFilter(
ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain)
throws
IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)ser