在Tomcat6.0中关于JSP/Servlet表单乱码的一个解决方法

在Tomcat中,提交表单时的中文乱码是一件非常头痛的事,好久没搞Web了,最近又重拾Web仔细研究了一下,总结了如下方法:

步骤一:编辑Tomcat的配置文件conf/server.xml在用于接受客户端语法的Connector<connector></connector>标签中添加URIEncoding="UTF-8"属性,该属性用来解决GET中的编码问题。
xml 代码
  1. <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />


步骤二:在每个需要提交表单参数的JSP/Servlet之前加入下列代码来设置字符集,用于搞定POST请求:
java 代码
  1. request.setCharacterEncoding("UTF-8");


这样基本就搞定了字符乱码问题了,实现上述问题的要求是所有的 网页编码必须是UTF-8编码既。
在JSP中:
jsp 代码
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
在Servlet中:
java 代码
  1. response.setContentType("text/html;charset=UTF-8");
在所有的网页中:
html代码
  1. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">


以上测试在Tomcat 6.0.14、IE6.0、FireFox2.0.13及Opera9.25中测试通过,开发环境使用netBeans 6.0

附测试代码:
html代码
  1. Document : zc
  2. Created on : 2007-12-22, 17:20:24
  3. Author : 啊春
  4. -->
  5. >
  6. <html>
  7. <head>
  8. <title>title>
  9. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  10. head>
  11. <body>
  12. <form action="/wat1/t1" method="GET">
  13. 姓名:<input type="text" name="name" value="" size="20" />
  14. 密码:<input type="password" name="passwd" value="" size="20" />
  15. <input type="submit" value="注册" />
  16. form>
  17. <br />
  18. <form action="/wat1/t1" method="POST">
  19. 姓名:<input type="text" name="name" value="" size="20" />
  20. 密码:<input type="password" name="passwd" value="" size="20" />
  21. <input type="submit" value="注册" />
  22. form>
  23. body>
  24. html>

java 代码
  1. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. response.setContentType("text/html;charset=UTF-8");
  4. PrintWriter out = response.getWriter();
  5. try {
  6. /* TODO output your page here
  7. out.println("");
  8. out.println("");
  9. out.println("");
  10. out.println("");
  11. out.println("");
  12. out.println("

    Servlet t1 at " + request.getContextPath () + "

    ");
  13. out.println("");
  14. out.println("");
  15. */
  16. out.println("Hello 欢迎你的注册");
  17. request.setCharacterEncoding("UTF-8");
  18. String str = request.getParameter("name");
  19. out.println(str);
  20. out.println(request.getParameter("passwd"));
  21. } finally {
  22. out.close();
  23. }
  24. }


jsp代码
  1. <%--
  2. Document : index
  3. Created on : 2007-12-20, 20:06:04
  4. Author : 啊春
  5. --%>
  6. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  7. "http://www.w3.org/TR/html4/loose.dtd">
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <title>JSP Pagetitle>
  12. head>
  13. <body>
  14. <h2>Hello World!h2>
  15. body>
  16. <%
  17. request.setCharacterEncoding("UTF-8");
  18. String name = request.getParameter("name");
  19. String passwd = request.getParameter("passwd");
  20. %>
  21. 姓名:<%=name%><br />
  22. 密码:<%=passwd%>
  23. html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值