关于中文乱码?
1.第一个层次在jsp页面上输入中文,请求页面后不出现乱码
保持三个一致
pageEncoding="utf-8"和contentType=“text/html;charset=uft-8”一致同时和浏览器编码一致
<form action="hello.jsp" method="post">
<input type="text" name="username"/>
<input type="passwrod" name="password"/>
<input type="submit" value="提交" />
</form>
<br>
username
<%request.getParameter("username")%>
//乱码->解决
<%
request.setCharacterEncoding("utf-8");
%>
//对应post请求:在获取请求信息之前才能解决,即在
<%request.getParameter("username")%>之前调用该方法
//对应get请求(不能解决):
获取中文参数值:默认参数在传输过程中使用的编码为iso-8895-1
解决方案?
<br/><br/>
<%
String value=request.getParameter("username");
String username=new String (value.getBytes("iso-8895-1"),"utf-8");
%>
太麻烦?(取决于tomcat)
解决方案2-通过修改tomcat的config的server.xml文件
URIEncoding
useBodyEnoodingForURI
tomcat-config-server.xml-connect指定一个useBodyEnoodingForURI="true"
String username=new String (value.getBytes("utf-8"),"utf-8");