• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >JSP > JSP中文乱码常见3个例子及其解决方法

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了jsp中文乱码,jsp中文乱码问题,jsp页面中文乱码,jsp传递中文乱码,jsp解决中文乱码等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

实例一、JSP页面显示时

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

运行结果:

解决方法:为其指定中文字符集,<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>邮箱: <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> 
</div>

个人信息页面:

<%@ 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>邮箱: <% out.print(email); %><p/> 
      <p>昵称: <% out.print(nickname); %><p/> 
      <p>密码: <% out.print(password); %><p/> 
      <p>性别: <% out.print(sex); %><p/> 
      <p>个人介绍:<%out.print(introduction); %></p> 
    </center> 
  </body> 
</html> 
</div>

运行结果:

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

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

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

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

运行结果:

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • JSP乱码问题
  • JSP中文乱码常见3个例子及其解决方法
  • 分享JSP中文乱码解决方法
  • jsp下显示中文文件名及绝对路径下的图片解决方法
  • jsp超链接中文乱码的解决方法
  • jsp 重复提交问题
  • jsp中文乱码 jsp mysql 乱码的解决方法
  • JSP动态输出Excel及中文乱码的解决
  • 关于JSP中文问题的解决方法
  • JSP应用的安全问题

相关文章

  • 2017-05-11关于JSP的一点疑问小结
  • 2017-05-11JBuilder2005单元测试体验之测试配置
  • 2017-05-11JSP实现从数据库导出数据到Excel下载的方法
  • 2017-05-11打开页面就是全屏的方法
  • 2017-05-11jsp实现防盗链的方法
  • 2017-05-11Java Web项目中连接Access数据库的配置方法
  • 2017-05-11JSP 不能解析EL表达式的解决办法
  • 2017-05-11JSP JavaBean的setProperty属性
  • 2017-05-11Spring 中 @Service 和 @Resource 注解的区别
  • 2017-05-11JSP 中Spring组合注解与元注解实例详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • jsp中实现带滚动条的table表格实例代码
    • jsp文件操作之追加篇
    • java tapestry5 布局 参数的处理
    • 在JSP中处理虚拟路径
    • 我认为JSP有问题(下)
    • img的src地址是一个请求的方式来显示图片方法
    • JDBCTM 指南:入门3 - DriverManager
    • struts2+jsp实现文件上传的方法
    • JAVA/JSP学习系列之七(Orion下自定义Tag)
    • JBuilder2005单元测试之业务类介绍

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有