深入体验JavaWeb开发内幕——Request中的乱码问题及解决
来源:互联网 发布:java流的分类 编辑:程序博客网 时间:2024/06/15 06:43
前面我们提到了Response对象中出现乱码问题及相应的解决措施,那么在Request中如何解决编码问题呢?
下面来看具体的例子:
例如我想将一个form表单中的信息提取到并在控制台输出如图:
假设在服务端并未Request对象给指定编码时那么你在客户端添加信息如:
填入的信息有中文
假设设置表单的提交方式为post方式提交
则在服务端输出如下:
代码如下:
Register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Register.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form action ="RequestLogin" method ="post"> 用户名:<input type="text" name = "username" ><br/> 密码: <input type="password" name = "password"><br/> 性别:<input type="radio" name = "sex" value ="male">男 <input type="radio" name = "sex" value ="female">女<br/> 籍贯: <select name ="city"> <option value ="HeBei">河北</option> <opton value ="HuBei">湖北</opton> <option value ="ShanXi">山西</option> </select><br/>简历:<br/> &nsp;<textarea rows="5" cols="20" name ="intro"></textarea><br/>爱好:<br/><input type="checkbox" name ="hobbies" value ="sing"/>唱歌<input type="checkbox" name ="hobbies" value ="dance"/>跳舞<input type="checkbox" name ="hobbies" value ="readbook"/>读书<input type="checkbox" name ="hobbies" value ="readnewspaper"/>看报<br/>上传头像:<br/><input type="file" value ="image" name ="browser"><br/><input type="submit" value ="提交"/> </form> </body></html>
RequestLogin.javapackage net.csdn.request;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RequestLogin extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ getInformation(request); }private void getParameter(HttpServletRequest request)throws UnsupportedEncodingException{private void getInformation(HttpServletRequest request)throws UnsupportedEncodingException {//取出参数值 String name = request.getParameter("username"); String pass = request.getParameter("password"); String sex = request.getParameter("sex"); String city = request.getParameter("city"); String intro = request.getParameter("intro"); String [] hobbies = request.getParameterValues("hobbies"); String hobby =""; //hobbies!=null对所取值为空时进行设置 for(int i=0;hobbies!=null&&i<hobbies.length;i++){ String hovalue = hobbies[i]; hobby += hovalue; } //获取头像信息 // String image = request.getParameter("image"); System.out.println("username:"+name); System.out.println("password:"+pass); System.out.println("sex:"+sex); System.out.println("city:"+city); System.out.println("intro:"+intro); System.out.println("hobby:"+hobby);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}
这是因为你在RegisterLogin.java中并没有给Request对象设置编码集,而Request对象的默认编码集是ISO8859-1是不支持汉字的,所以你只需要在此类中为其指明相应的编码即可:
改正:
request.setCharacterEncoding("utf-8");
即可输出:
但是这种方式只在提交方式为post时有效当提交方式为get时是不起作用的。
即;
<form action ="RequestLogin" method ="get">
时即便在
RequestLogin.java
中再设置
request.setCharacterEncoding("utf-8");
也不会起任何作用了如图:
这时就需要在
RequestLogin.java
中的含有中文的地方进行如下设置了即:
String username = new String(name.getBytes("iso8859-1"),"utf-8");
String introduction = new String(intro.getBytes("iso8859-1"),"utf-8");
System.out.println("username:"+username);
System.out.println("password:"+introduction);
此时再度测试时就OK了!如图
- 深入体验JavaWeb开发内幕——Request中的乱码问题及解决
- 深入体验JavaWeb开发内幕——关于Request对象
- 深入体验JavaWeb开发内幕——Request相关知识及应用总结
- 深入体验JavaWeb开发内幕——关于JEE中的response、resquest应答、请求头的编码问题的解决
- 深入体验JavaWeb开发内幕——关于JEE中的response、resquest应答、请求头的编码问题的解决
- 深入体验JavaWeb开发内幕——使用Request对象设置防盗链
- 深入体验JavaWeb开发内幕——图片、声音等文件的下载源码及可能出现的问题
- 深入体验JavaWeb开发内幕——Servlet入门
- 深入体验JavaWeb开发内幕——Cookie会话
- 深入体验JavaWeb开发内幕——EL表达式之一
- 深入体验JavaWeb开发内幕——配置TomCat数据源
- 深入体验JavaWeb开发内幕——EL表达式之一
- 深入体验JavaWeb开发内幕——EL表达式之一
- 深入体验JavaWeb开发内幕——打包自定义标签库及自定义分页标签
- 深入体验JavaWeb开发内幕——简述JSP中的自定义标签叫你快速学会
- 深入体验JavaWeb开发内幕——JDBC中的三大元数据
- 深入体验JavaWeb开发内幕——一个获取form表单中的相关组件值的应用
- 深入体验JavaWeb开发内幕——JSP技术简介之二JSP中的指令和out隐式对象
- 实时3D地形引擎——Chapter6_02——D3DApp框架阅读1
- Mysql数据库用户权限添加配置
- java反射技术破坏单例模式
- jBPM4.4:如何图示化显示当前的流程执行到了哪一步骤
- 软件的本质
- 深入体验JavaWeb开发内幕——Request中的乱码问题及解决
- Linux 系统调用的实现及其汇编代码的嵌入到C中
- 递归递归
- NS2中 #include <wireless-phy.h> 引发的问题
- struts动态加载多级菜单
- android activity 生命周期简介
- poj 1860
- 多线程优先级
- Linux系统调用列表