当前位置: 首页 > 编程笔记 >

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

傅星光
2023-03-14
本文向大家介绍JSP中文乱码常见3个例子及其解决方法,包括了JSP中文乱码常见3个例子及其解决方法的使用技巧和注意事项,需要的朋友参考一下

常见3个例子及其解决方法如下

实例一、JSP页面显示时

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

运行结果:

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

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

实例二、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> 

个人信息页面:

<%@ 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> 

运行结果:

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

<%@ 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> 

实例三、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> 

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>"); 
   } 
} 

运行结果:

解决方法:在doPost中加入:

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

以上就是几种常见JSP中文乱码例子及其解决方法,希望能够帮助大家解决JSP中文乱码的问题。

 类似资料:
  • 本文向大家介绍常见php与mysql中文乱码问题解决办法,包括了常见php与mysql中文乱码问题解决办法的使用技巧和注意事项,需要的朋友参考一下 乱码问题1:用PHPmyAdmin操作MySQL数据库汉字显示正常,但用PHP网页显示MySQL数据时所有汉字都变成了?号。 症状:用PHPmyAdmin输入汉字正常,但当PHP网页显示MySQL数据时汉字就变成了?号,并且有多少个汉字就有多少个?号。

  • 不太会使用 Env 工具的请先看一遍 《Env 用户手册》(不长的,看完费不了几分钟) 提示 Env 工具和 源码 所处的目录都不能有中文或空格请先检查!! code 是一个命令 点 ‘.’ 是一个参数表示当前目录,中间有一个空格。 romfs ramfs 文件系统中的文件名和c的变量的命名一样,只能由英文字母开头且仅包含数字和下划线。 修改 qemu.bat 里面的参数时,要注意那是一行参数中间

  • 本文向大家介绍php写入mysql中文乱码的实例解决方法,包括了php写入mysql中文乱码的实例解决方法的使用技巧和注意事项,需要的朋友参考一下 php写入mysql出现中文乱码的解决办法是:在建立数据库连接之后,将该连接的编码方式改为中文。 代码如下: 继续上面的问题,查看一下Apache目录下的PHP源文件,发现页面的编码方式是ANSI的,于是乎,我改成了utf8格式的。然后在执行SQL语句

  • 本文向大家介绍php解决DOM乱码的方法示例代码,包括了php解决DOM乱码的方法示例代码的使用技巧和注意事项,需要的朋友参考一下 前言 DOM是php比较新的xml和html处理类,可以像javascript那样方便的操作DOM树,网上更多的是介绍它处理XML的情况,今天这篇文章就介绍下php解决DOM乱码的方法,下面话不多说,直接看下面的解决方法。 解决方法如下 总结 以上就是这篇文章的全部内

  • 本文向大家介绍jsp之间传参数接受中文有乱码问题解决方法,包括了jsp之间传参数接受中文有乱码问题解决方法的使用技巧和注意事项,需要的朋友参考一下 tomcat 的配置文件 server.xml里面找到: 看看有没有 URIEncoding="UTF-8",没有的话加上试试

  • 本文向大家介绍JSP利用过滤器解决request中文乱码问题,包括了JSP利用过滤器解决request中文乱码问题的使用技巧和注意事项,需要的朋友参考一下 本文为大家分享了JSP用过滤器解决request中文乱码问题,具体内容如下 (1)客户端的数据一般是通过HTTP  GET/POST方式提交给服务器,在服务器端用request.getParameter() 读取参数时,很容易出现中文乱码现象。