[Jweb] Tomcat 解决编码, 乱码问题
来源:互联网 发布:简单的php项目 编辑:程序博客网 时间:2024/04/28 13:10
Know其他 : tomcat 解决乱码问题
b.jsp 源码
<%@ page contentType="text/html;charset=GBK"%> <!-- 解决 get 方式乱码问题,这句话 -->
<%request.setCharacterEncoding("GBK");%> <!-- 解决 post 方式乱码问题,这句话 -->
<!-- 以上两句话加上,非常好,可以兼顾不出乱码 -->
<%=request.getParameter("user") %>
<%String s = "哈哈哈";%>
<%=s%>
b.jsp 源码
<%@ page contentType="text/html;charset=GBK"%> <!-- 解决 get 方式乱码问题,这句话 -->
<%request.setCharacterEncoding("GBK");%> <!-- 解决 post 方式乱码问题,这句话 -->
<!-- 以上两句话加上,非常好,可以兼顾不出乱码 -->
<%=request.getParameter("user") %>
<%String s = "哈哈哈";%>
<%=s%>
具体核心 :见 csdn, robby_chan 本博。
a1.html
<form action=b.jsp method=get ><input name=user value=我们><input type=submit value=get></form><form action=b.jsp method=post ><input name=user value=他们><input type=submit value=post></form><a href="/my/charset//b.jsp?user=你们">this is a link</a>TestB.java 张志宇带你详细讲解 编码,乱码问题。
public class TestB { static String zhongwen = "中文"; static byte[] bytes = null; public static void main(String[] args) throws Exception { // GBK // main1(); // utf-8 // main2(); // GBK-->utf-8-->GBK // main3(); // GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK main4(); } // GBK public static void main1() throws Exception { bytes = zhongwen.getBytes("GBK"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } System.out.println(new String(bytes, "GBK")); } // utf-8 public static void main2() throws Exception { bytes = zhongwen.getBytes("utf-8"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } System.out.println(new String(bytes, "utf-8")); } // GBK-->utf-8-->GBK public static void main3() throws Exception { bytes = zhongwen.getBytes("GBK"); System.out.println("--------GBK----------"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } bytes = zhongwen.getBytes("utf-8"); byte[] bytes_copy = bytes; System.out.println("-------utf-8--------"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // 错误的组装方法 System.out.println("-------error GBK--------"); zhongwen = new String(bytes, "GBK"); System.out.println(zhongwen); bytes = zhongwen.getBytes("GBK"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // 正确的组装方法 System.out.println("-------GBK--------"); System.out.println(new String(bytes_copy, "utf-8")); } // GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK public static void main4() throws Exception { // 最开始是GBK bytes = zhongwen.getBytes("GBK"); System.out.println("--------GBK----------"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // 被组装成了iso8859-1,new出的字符串对象有问题,但是字节内容没变 System.out.println("-------iso8859-1--------"); zhongwen = new String(zhongwen.getBytes("GBK"), "iso8859-1"); System.out.println(zhongwen); bytes = zhongwen.getBytes("iso8859-1"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // 转换成utf-8在网络上传输,一个byte转换为两个字节,所以一共8个字节 bytes = zhongwen.getBytes("utf-8"); System.out.println("-------utf-8--------"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // server端接收到utf-8,首先组装成iso8859-1,new出的字符串对象有问题,显示4个问号 System.out.println("-------server--iso8859-1------"); zhongwen = new String(bytes, "utf-8"); System.out.println(zhongwen); bytes = zhongwen.getBytes("iso8859-1"); for (int i = 0; i < bytes.length; i++) { System.out.println(Integer.toHexString(bytes[i])); } // 以iso8859-1得到字节,并组装成GBK System.out.println("-------GBK--------"); System.out.println(new String(bytes, "GBK")); } public static void ____________________main() throws Exception { System.out.println(System.getProperty("file.encoding")); System.out.println(System.getProperty("user.language")); System.out.println(System.getProperty("user.region")); }}SayHelloBean.html
<html><head><title>数据输入</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF"><div align="center"> <p>请输入数据</p> <form method="post" action="SayHelloBean.jsp" > <p>姓名 <input type="text" name="name"> 性别 <select name="sex"> <option value="先生">先生</option> <option value="女士">女士</option> </select> </p> <p> <input type="submit" name="Submit" value="提交"> </p> </form> <p> </p> <p> </p></div></body></html>SayHelloBean.jsp
<%@ page language="java" import="bean.HelloBean;" %><%@ page contentType="text/html;charset=gb2312" %><%//request.getParameter("name");//request.setCharacterEncoding("gb2312");%><jsp:useBean id="hello" class="bean.HelloBean" scope="request" > <jsp:setProperty name="hello" property="*" /></jsp:useBean><html><head><title>HelloBean</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF"><p> </p><p align="center"><font size="4">欢迎<font color="#0000FF"><b> <%-- <%=new String(hello.getName().getBytes("ISO8859_1"),"GBK")%>--%></b></font><%--<%=new String(hello.getSex().getBytes("ISO8859_1"),"GBK")%>--%></font></p><jsp:getProperty name="hello" property="name"/><jsp:getProperty name="hello" property="sex"/></body></html>
0 0
- [Jweb] Tomcat 解决编码, 乱码问题
- 解决tomcat编码后乱码问题
- tomcat编码乱码问题
- tomcat编码乱码问题
- tomcat编码问题--乱码
- 修改Tomcat默认编码解决中文传值乱码问题
- 解决表单乱码问题之一,修改tomcat默认编码格式
- 修改Tomcat的字符编码配置解决乱码问题
- tomcat 设置UTF编码 解决中文接收乱码问题
- tomcat解决乱码问题
- Tomcat 解决乱码问题
- 解决编码解码乱码问题
- 编码解码,解决乱码问题
- 解决Tomcat中文乱码问题
- tomcat乱码问题一次性解决
- Tomcat设置解决乱码问题
- tomcat 解决中文乱码问题
- 解决 apache tomcat 编码问题
- 设计模式初探-单例模式
- Android内存泄漏就这样产生了
- OllyDbg通用快捷键
- ntp校时源码-转载
- Activity专题
- [Jweb] Tomcat 解决编码, 乱码问题
- 微信图文消息回复
- userenv和sys_context函数
- 4Sum
- 第17周周赛(大一) --A - Parallelepiped
- oracle执行计划-Nested Loops (nl)-3
- 在一个JSP页面中包含另一个JSP页面的三种方式
- Android内存优化
- 77. 从说“是”开始