常见JSP中文乱码例子及其解决方法

开发 后端
当我们在学习JSP开发时,中文乱码是个比较常见的问题,其根源是:Web容器默认的字符处理编码是ISO-8859-1。下面我们来详细看看如何去解决这个问题。

JSP开发应用是,中文乱码是个比较常见的问题,其根源是:Web容器默认的字符处理编码是ISO-8859-1。

实例一、JSP页面显示时

<html> 
    <head> 
       <title>中文乱码——JSP页面显示时</title> 
    </head> 
    <body> 
       <center> 
           <br/> 
           <h1>木兰辞拟古决绝词柬友</h1> 
           <p>人生若只如初见,何事秋风悲画扇。</p> 
         <p>等闲变却故人心,却道故人心易变。</p> 
         <p>骊山语罢清宵半,泪雨霖铃终不怨。</p> 
         <p>何如薄幸锦衣郎,比翼连枝当日愿。</p> 
       </center> 
    </body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

运行结果:

解决方法:为其指定中文字符集,<html>前加入

<%@ page contentType="text/html;charset=gb2312" %> 
  • 1.

实例二、JSP页面传递中文参数时

注册页面:

<%@ page contentType="text/html;charset=gb2312" %> 
<html> 
    <head> 
       <title>中文乱码——JSP页面传递中文参数时</title> 
    </head> 
    <body> 
       <h2>申请账号:</h2> 
       <form action="userMsg.jsp" method="POST"> 
           <p>邮箱:&nbsp;<input type="text"name="email" id="email"/><p/> 
           <p>昵称:&nbsp;<input type="text"name="nickname" id="nickname"/><p/> 
           <p>密码:&nbsp;<input type="password"name="password" id="password"/><p/> 
           <p>性别:&nbsp;<input type="radio"name="sex" id="sex"value="男" /> 男  
                         <input type="radio" name="sex"id="sex" value="女" /> 女<p/> 
           <textarea  name="introduction"id="introduction" rows="5" cols="27">一句话介绍自己...</textarea> 
           <p><input type="submit"value="提交申请"></p> 
       </form> 
    </body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

个人信息页面:

<%@ page contentType="text/html;charset=gb2312" %> 
<html> 
    <head> 
       <title>中文乱码——JSP页面传递中文参数时 </title> 
    </head> 
    <body> 
       <center> 
           <h2>用户信息:</h2> 
           <% String email = request.getParameter("email"); %> 
           <% String nickname = request.getParameter("nickname"); %> 
           <% String password = request.getParameter("password"); %> 
           <% String sex = request.getParameter("sex"); %> 
           <% String introduction = request.getParameter("introduction");%> 
           <p>邮箱:&nbsp;<% out.print(email); %><p/> 
           <p>昵称:&nbsp;<% out.print(nickname); %><p/> 
           <p>密码:&nbsp;<% out.print(password); %><p/> 
           <p>性别:&nbsp;<% out.print(sex); %><p/> 
           <p>个人介绍:<%out.print(introduction); %></p> 
       </center> 
    </body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

运行结果:

解决方法:修改个人信息页面如下

<%@ page contentType="text/html;charset=gb2312" %> 
<html> 
    <head> 
       <title>中文乱码——JSP页面传递中文参数时 </title> 
    </head> 
    <body> 
       <h2>用户信息:</h2> 
       <% String email = newString(request.getParameter("email").getBytes("ISO-8859-1"), "gb2312");%> 
       <% String nickname = newString(request.getParameter("nickname").getBytes("ISO-8859-1"), "gb2312");%> 
       <% String password = newString(request.getParameter("password").getBytes("ISO-8859-1"), "gb2312");%> 
       <% String sex = newString(request.getParameter("sex").getBytes("ISO-8859-1"), "gb2312");;%> 
       <% String introduction = newString(request.getParameter("introduction").getBytes("ISO-8859-1"), "gb2312");;%> 
       <p>邮箱: <% out.print(email); %><p/> 
       <p>昵称: <% out.print(nickname); %><p/> 
       <p>密码: <% out.print(password); %><p/> 
       <p>性别: <% out.print(sex); %><p/> 
       <p>个人介绍:<%out.print(introduction); %></p> 
    </body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

实例三、Servlet处理中文参数时

注册页面:

<%@ page contentType="text/html;charset=gb2312" %> 
<%@ page import="test.UserMsg"%> 
<html> 
    <head> 
       <title>中文乱码——JSP页面传递中文参数时</title> 
    </head> 
    <body> 
       <h2>申请账号:</h2> 
       <form action="./UserMsg" method="POST"> 
           <p>邮箱: <input type="text"name="email" id="email"/><p/> 
           <p>昵称: <input type="text"name="nickname" id="nickname"/><p/> 
           <p>密码: <input type="password"name="password" id="password"/><p/> 
           <p>性别: <input type="radio"name="sex" id="sex"value="男" /> 男  
                         <input type="radio" name="sex"id="sex" value="女" /> 女<p/> 
           <textarea  name="introduction"id="introduction" rows="5" cols="27">一句话介绍自己...</textarea> 
           <p><input type="submit"value="提交申请"></p> 
       </form> 
    </body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

UserMsg.java(Servlet)

package test;  
   
importjava.io.IOException;  
importjava.io.PrintWriter;  
importjava.io.UnsupportedEncodingException;  
   
importjavax.servlet.http.HttpServlet;  
importjavax.servlet.http.HttpServletRequest;  
importjavax.servlet.http.HttpServletResponse;  
public classUserMsg extends HttpServlet{  
      public void doGet(HttpServletRequestrequest,  
                 HttpServletResponse response){  
           doPost(request, response);  
      }  
      public void doPost(HttpServletRequestrequest,  
                 HttpServletResponse response){  
           try {  
                 request.setCharacterEncoding("gb2312");  
           } catch (UnsupportedEncodingExceptione) {  
                 e.printStackTrace();  
           }  
           PrintWriter out = null;  
           try {  
                 out = response.getWriter();  
           } catch (IOException e1) {  
                 e1.printStackTrace();  
           }  
           out.print("<html>");  
           out.print("<body>");  
           out.print("<h2>" +"用户信息:""</h2>");  
           out.print("<p>"+"邮箱:"+request.getParameter("email")+"<p/>");  
           out.print("<p>"+"昵称:"+request.getParameter("nickname")+"<p/>");  
           out.print("<p>"+"密码:"+request.getParameter("password")+"<p/>");  
           out.print("<p>"+"性别:"+request.getParameter("sex")+"<p/>");  
           out.print("<p>"+"个人介绍:"+request.getParameter("introduction")+"<p/>");  
           out.print("</html>");  
           out.print("</body>");  
      }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

运行结果:

解决方法:在doPost中加入:

response.setContentType("text/html; charset=gb2312"); 
  • 1.

原文链接:http://blog.csdn.net/cannel_2020/article/details/7375768

【编辑推荐】

  1. 告别无止境的增删改查:Java代码生成器
  2. Java通过SSH2协议运行远程程序
  3. Java Bean属性命名规范问题分析
  4. Java Socket编程:初识TCP Socket
  5. Java Socket编程:如何识别网络主机
责任编辑:林师授 来源: Cannel_2020的博客
相关推荐

2009-07-02 13:26:32

JSP中文乱码

2009-09-07 18:40:28

PHP中文乱码

2009-07-02 13:32:56

JSP中文乱码

2009-07-01 18:14:36

JSP乱码

2011-03-01 15:38:44

Fireftp乱码

2009-02-18 14:28:23

编码乱码JSP

2013-01-30 16:54:21

2013-06-20 09:14:43

2010-07-15 14:01:12

telnet乱码

2011-06-14 13:41:27

muleWSDL

2023-04-06 15:21:34

IIoT自动化

2010-10-13 17:22:12

MySQL查询乱码

2010-08-24 09:07:16

无线路由器

2024-11-08 13:47:35

中文乱码配置

2009-07-06 17:50:13

Java JSP

2024-05-24 12:06:26

SQL数据库

2021-03-26 11:38:29

云计算

2021-03-25 11:25:43

云计算云计算产业云应用

2009-07-01 18:05:54

JSP中文乱码

2010-05-14 17:27:40

MySQL中文乱码
点赞
收藏

51CTO技术栈公众号