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

所属分类: 网络编程 / JSP编程 阅读数: 1270
收藏 0 赞 0 分享

常见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中文乱码的问题。

更多精彩内容其他人还在看

Java Web实现的基本MVC实例分析

这篇文章主要介绍了Java Web实现的基本MVC,以完整实例形式较为详细的分析了JSP实现MVC架构的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

jsp中调用java代码小结

大多数情况下, jsp 文档的大部分由静态文本(html)构成, 为处理该页面而创建的 servlet 只是将它们原封不动的传递给客户端
收藏 0 赞 0 分享

jsp和servlet操作mysql中文乱码问题的解决办法

自己做测试的时候用到jsp/servlet 向mysql中写数据,但是中文总是乱码,今早纠结了半天才搞定,分享给大家我的解决办法
收藏 0 赞 0 分享

jsp跳转getRequestDispatcher()和sendRedirect()的区别

这篇文章主要介绍了jsp跳转getRequestDispatcher()和sendRedirect()的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

java操作mysql入门代码实例(含插入、更新和查询)

这篇文章主要介绍了java操作mysql代码实例,通过执行SQL语句实现,需要的朋友可以参考下
收藏 0 赞 0 分享

struts2中一个表单中提交多个请求的例子(多个提交按钮)

在很多Web应用中,为了完成不同的工作,一个HTML form标签中可能有两个或多个submit按钮,Struts2中提供了另外一种方法,使得无需要配置可以在同一个action类中执行不同的方法(默认执行的是execute方法)
收藏 0 赞 0 分享

jsp中使用javabean实例介绍

这篇文章主要介绍了jsp中使用javabean的实例,同时介绍了相关属性,需要的朋友可以参考下
收藏 0 赞 0 分享

servlet中session简介和使用例子

在servlet中,session是封装在javax.servlet.http.HttpSession这个接口中的,这个接口是构建在cookie或者URL重写的基础上,要得到一个HttpSession的实例,就可以通过HttpServletRequest的getSession()
收藏 0 赞 0 分享

JSP中用回车监听按钮事件兼容火狐 IE等主流浏览器

这篇文章主要介绍了用回车监听按钮事件并且兼容火狐、IE等主流浏览器 ,需要的朋友可以参考下
收藏 0 赞 0 分享

JAVA velocity模板引擎使用实例

这篇文章主要介绍了JAVA velocity模板引擎使用实例,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多