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

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

实例一、JSP页面显示时

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

运行结果:

 

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

[html]  view plain copy
  1. <%@ page contentType="text/html;charset=gb2312" %>  


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

注册页面:

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

个人信息页面:

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

运行结果:

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

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


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

注册页面:

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

UserMsg.java(Servlet)

[java]  view plain copy
  1. package test;  
  2.    
  3. importjava.io.IOException;  
  4. importjava.io.PrintWriter;  
  5. importjava.io.UnsupportedEncodingException;  
  6.    
  7. importjavax.servlet.http.HttpServlet;  
  8. importjavax.servlet.http.HttpServletRequest;  
  9. importjavax.servlet.http.HttpServletResponse;  
  10. public classUserMsg extends HttpServlet{  
  11.       public void doGet(HttpServletRequestrequest,  
  12.                  HttpServletResponse response){  
  13.            doPost(request, response);  
  14.       }  
  15.       public void doPost(HttpServletRequestrequest,  
  16.                  HttpServletResponse response){  
  17.            try {  
  18.                  request.setCharacterEncoding("gb2312");  
  19.            } catch (UnsupportedEncodingExceptione) {  
  20.                  e.printStackTrace();  
  21.            }  
  22.            PrintWriter out = null;  
  23.            try {  
  24.                  out = response.getWriter();  
  25.            } catch (IOException e1) {  
  26.                  e1.printStackTrace();  
  27.            }  
  28.            out.print("<html>");  
  29.            out.print("<body>");  
  30.            out.print("<h2>" +"用户信息:""</h2>");  
  31.            out.print("<p>"+"邮箱:"+request.getParameter("email")+"<p/>");  
  32.            out.print("<p>"+"昵称:"+request.getParameter("nickname")+"<p/>");  
  33.            out.print("<p>"+"密码:"+request.getParameter("password")+"<p/>");  
  34.            out.print("<p>"+"性别:"+request.getParameter("sex")+"<p/>");  
  35.            out.print("<p>"+"个人介绍:"+request.getParameter("introduction")+"<p/>");  
  36.            out.print("</html>");  
  37.            out.print("</body>");  
  38.       }  
  39. }  
运行结果:


解决方法:在doPost中加入:

[java]  view plain copy
  1. response.setContentType("text/html; charset=gb2312");  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决 Java Web 项目中文乱码的问题,可以考虑以下几个方法: 1. 设置字符编码:在 JSP 页面,可以通过设置 `pageEncoding` 属性来指定页面编码,例如 `<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>`。在 Java 代码,可以通过 `request.setCharacterEncoding("UTF-8")` 来设置请求的编码。 2. 设置响应头:在 Servlet ,可以通过 `response.setContentType("text/html; charset=UTF-8")` 来设置响应的编码。 3. 使用过滤器:可以通过编写过滤器来统一处理编码问题,例如: ```java public class EncodingFilter implements Filter { private String encoding = "UTF-8"; public void init(FilterConfig config) throws ServletException { if (config.getInitParameter("encoding") != null) { encoding = config.getInitParameter("encoding"); } } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { req.setCharacterEncoding(encoding); resp.setContentType("text/html; charset=" + encoding); chain.doFilter(req, resp); } public void destroy() { } } ``` 然后在 web.xml 配置过滤器: ```xml <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.example.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 4. 采用统一编码:可以将项目的所有文件都采用统一的编码,例如 UTF-8。在 Eclipse ,可以通过设置文件编码来实现。 以上是一些常见解决方法,具体选择哪种方法取决于具体情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值