iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >常见JSP中文乱码的场景及其解决方法
  • 623
分享到

常见JSP中文乱码的场景及其解决方法

2023-06-17 10:06:46 623人浏览 八月长安
摘要

本篇内容介绍了“常见jsP中文乱码的场景及其解决方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!JSP开发应用是,中文乱码是个比较常见的问

本篇内容介绍了“常见jsP中文乱码的场景及其解决方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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

实例一、JSP页面显示时

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

运行结果:

常见JSP中文乱码的场景及其解决方法

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

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

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

注册页面:

<%@ page contentType="text/html;charset=gb2312" %> <html>     <head>        <title>中文乱码&mdash;&mdash;JSP页面传递中文参数时</title>     </head>     <body>        <h3>申请账号:</h3>        <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>中文乱码&mdash;&mdash;JSP页面传递中文参数时 </title>     </head>     <body>        <center>            <h3>用户信息:</h3>            <% 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>

运行结果:

常见JSP中文乱码的场景及其解决方法

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

<%@ page contentType="text/html;charset=gb2312" %> <html>     <head>        <title>中文乱码&mdash;&mdash;JSP页面传递中文参数时 </title>     </head>     <body>        <h3>用户信息:</h3>        <% 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>中文乱码&mdash;&mdash;JSP页面传递中文参数时</title>     </head>     <body>        <h3>申请账号:</h3>        <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("<h3>" +"用户信息:"+ "</h3>");             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>");        }  }

运行结果:

常见JSP中文乱码的场景及其解决方法

解决方法:在doPost中加入:

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

“常见JSP中文乱码的场景及其解决方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: 常见JSP中文乱码的场景及其解决方法

本文链接: https://www.lsjlt.com/news/288443.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么保证除完还是小数
    在 c 语言中,整数除法只能得到整数结果,要得到小数结果,需将操作数显式转换为浮点数:将一个操作数转换为浮点数,如 float result = num1 / (float)num2;将...
    99+
    2024-05-14
    c语言
  • c语言怎么让结尾不输出空行字符
    要阻止 c 语言程序结尾输出空行字符,可以使用以下方法:将 main 函数的返回值类型改为 void;在 main 函数中显式返回 0;调用 fflush(stdout) 函数刷新标准输...
    99+
    2024-05-14
    c语言
  • c语言怎么让结尾不输出空行数据
    在 c 语言中,可通过以下方法抑制 printf() 函数在程序结束时打印末尾空行:调用 fflush() 函数刷新缓冲区,立即输出所有数据;使用 setvbuf() 函数关闭缓冲,使数...
    99+
    2024-05-14
    c语言
  • c语言怎么让结尾无空行
    在 c 中去除结尾空行的方法:使用 fflush() 刷新缓冲区。使用 setvbuf() 将缓冲模式设置为 _ionbf。使用 printf 宏,它默认禁用缓冲。 如何在 C 语言中...
    99+
    2024-05-14
    c语言
  • c语言怎么输入实数赋值
    c语言中使用scanf()函数输入实数并赋值给变量:格式:scanf("%lf", &amp;variable);%lf是格式说明符,指定输入双精度浮点数;&...
    99+
    2024-05-14
    c语言
  • c语言怎么表达负数
    c语言中,负数以减号 (-) 表示,放在数字或变量前。负数运算规则包括:绝对值取正数;加正数或负数,结果取决于绝对值大小;乘或除以正数或负数,结果由符号奇偶性决定。负数的平方始终为正数,...
    99+
    2024-05-14
    c语言
  • c语言怎么输入Jac数列
    jacobi 数列的输入和生成方法分别有:1. 直接输入法:使用 scanf() 函数逐项输入数列。2. 递归生成法:使用递归公式生成数列,需初始化数列的前两项,然后按公式生成后续项。 ...
    99+
    2024-05-14
    c语言
  • c语言怎么把数组变成字符串
    在 c 语言中,将数组转换成字符串的方法包括:使用 sprintf() 将数组格式化为字符串。使用 strcpy() 将数组复制到字符串。使用 strncpy() 将指定长度的数组复制到...
    99+
    2024-05-14
    c语言
  • c语言怎么批量注释
    批量注释 c 语言代码的方法有:使用代码编辑器:使用快捷键或菜单命令自动添加 // 注释符号。使用注释工具:如 doxygen 和 cutter,批量添加行注释、块注释和文档注释。使用脚...
    99+
    2024-05-14
    python sublime c语言
  • c语言怎么把选中的全部注释
    c语言中注释选中内容可通过以下步骤实现:选中要注释的代码。根据使用的编辑器或ide,执行注释操作,例如在visual studio中右键单击并选择“注释所选内容”。添加注释内容。保存更改...
    99+
    2024-05-14
    sublime c语言
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作