几种常见JSP中文乱码的解决方法

2025-10-29 05:22:17

1、JSP页面显示时

输入以下代码

<html>

   <head>

      <title>中文乱码——JSP页面显示时</title>

   </head>

   <body>

      <center>

              <br/>

              <h1>木兰辞拟古决绝词柬友</h1>

              人生若只如初见,何事秋风悲画扇。

      等闲变却故人心,却道故人心易变。

      骊山语罢清宵半,泪雨霖铃终不怨。

      何如薄幸锦衣郎,比翼连枝当日愿。

      </center>

   </body>

</html>

输出结果见图片

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

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

几种常见JSP中文乱码的解决方法

2、JSP页面传递中文参数时

输入以下代码

注册页面:

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

<html>

   <head>

      <title>中文乱码——JSP页面传递中文参数时</title>

   </head>

   <body>

      <h2>申请账号:</h2>

      <form     action="userMsg.jsp" method="POST">

              邮箱:&nbsp;<input     type="text"name="email"     id="email"/><p/>

              昵称:&nbsp;<input     type="text"name="nickname" id="nickname"/><p/>    

              密码:&nbsp;<input     type="password"name="password"     id="password"/><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>

              <input type="submit"value="提交申请">

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

              邮箱:&nbsp;<% out.print(email); %><p/>    

              昵称:&nbsp;<% out.print(nickname);     %><p/>

              密码:&nbsp;<% out.print(password);     %><p/>

              性别:&nbsp;<% out.print(sex); %><p/>

              个人介绍:<%out.print(introduction); %>

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

      邮箱: <% out.print(email);     %><p/>

      昵称: <% out.print(nickname);     %><p/>

      密码: <% out.print(password);     %><p/>

      性别: <% out.print(sex); %><p/>    

      个人介绍:<%out.print(introduction);     %>

   </body>

</html>

几种常见JSP中文乱码的解决方法

3、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">

              邮箱: <input     type="text"name="email"     id="email"/><p/>

              昵称: <input     type="text"name="nickname"     id="nickname"/><p/>

              密码: <input     type="password"name="password"     id="password"/><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>

          <input     type="submit"value="提交申请">

      </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(""+"邮箱:"+request.getParameter("email")+"<p/>");      

              out.print(""+"昵称:"+request.getParameter("nickname")+"<p/>");      

              out.print(""+"密码:"+request.getParameter("password")+"<p/>");      

              out.print(""+"性别:"+request.getParameter("sex")+"<p/>");      

              out.print(""+"个人介绍:"+request.getParameter("introduction")+"<p/>");      

              out.print("</html>");  

              out.print("</body>");  

     }  

}

输出结果见图

解决方法:

在doPost中加入:

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

几种常见JSP中文乱码的解决方法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢