javaweb中的乱码问题大集合
package com.itheima.response; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //使用字节流输出中文内容 public class ResponseDemo1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test2(response); } //服务器端 UTF-8 private void test2(HttpServletResponse response) throws IOException { String data = "您吃了吗"; byte [] b = data.getBytes("UTF-8"); //服务器端 UTF-8 //通知浏览器,查UTF-8码表 ,下面 三句话等效,写任意一句都对。 //response.getOutputStream().write("<meta http-equiv=‘Content-Type‘ content=‘text/html;charset=UTF-8‘>".getBytes()); //response.setHeader("Content-Type", "text/html;charset=UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getOutputStream().write(b); } //服务器端和客户端都使用本地默认的编码GBK:没有乱码 private void test1(HttpServletResponse response) throws IOException { String data = "您吃了吗"; byte [] b = data.getBytes();//查GBK(本地默认的) response.getOutputStream().write(b); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2.用字符流输出中文,1是字节流输出中文
package com.itheima.response; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //用字符流输出中文 public class ResponseDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = "还没有吃吃吃吃吃吃"; // response.setCharacterEncoding("UTF-8");//该字符流默认的编码为UTF-8 // response.setContentType("text/html;charset=UTF-8");//通知浏览器使用UTF-8 //下面一句相当于上面的两句 response.setContentType("text/html;charset=UTF-8");//不仅能通知浏览器使用UTF-8,而且还会改字符流的编码为UTF-8 PrintWriter pw = response.getWriter();//在Servlet中,协议规定使用ISO-8859-1 pw.write(data); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
上面两个程序都是输出数据,以下是从表单(区别get和post两种方式,post方式处理起来比较简单)中获取中文数据,如何防止乱码呢?请看下面。。。
3.常用表单输入域的值获取
package com.itheima.request; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.itheima.domain.Student; //常用表单输入域的值获取 public class RequestDemo5 extends HttpServlet { //GET请求方式的参数编码 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");//码编错了 //取到你传给我的字节序列 byte b[] = name.getBytes("ISO-8859-1"); //重新编码 System.out.println(new String(b,"UTF-8")); } //POST请求方式的数据编码:建议使用 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");//只对POST请求方式有效 Student s = new Student(); try { BeanUtils.populate(s, request.getParameterMap());//发生类型的自动转换,但仅限基本数据类型 } catch (Exception e) { e.printStackTrace(); } System.out.println(s); } }
private void testDemo1(HttpServletRequest request) throws UnsupportedEncodingException { request.setCharacterEncoding("UTF-8"); //response.setContentType("text/html;charset=UTF-8");//把得到的数据给浏览器的话,就要加上这句。如果是在控制台输出,就不需要加这句。 String username = request.getParameter("username"); System.out.println(username); // response.getWriter().write(username); }
4,分清请求编码和响应输出的编码,两者并没有关系
package com.itheima.request; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //分清请求编码和响应输出的编码,两者并没有关系 public class RequestDemo6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收数据:请求 request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); //---------------------------------------------------- //响应数据:响应 response.setContentType("text/html;charset=GBK"); response.getWriter().write(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5.把表单中的数据封装到JavaBean,输入中文,也不报错。请看下面。。。
package com.itheima.request; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.itheima.domain.User2; public class RequestDemo0 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User2 user = new User2(); request.setCharacterEncoding("UTF-8"); System.out.println("封装前"+user.toString()); Map<String, String[]> map = request.getParameterMap(); response.setContentType("text/html;charset=UTF-8"); try { BeanUtils.populate(user, map); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("封装后"+user.toString()); response.getWriter().write(user.toString()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
JavaBean:
package com.itheima.domain; import java.util.Arrays; public class User2 { private String username; private String[] password; private String code; @Override public String toString() { return "User2 [username=" + username + ", password=" + Arrays.toString(password) + ", code=" + code + "]"; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String[] getPassword() { return password; } public void setPassword(String[] password) { this.password = password; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。