在Tomcat6.0中关于JSP/Servlet表单乱码的一个解决方法
步骤一:编辑Tomcat的配置文件conf/server.xml在用于接受客户端语法的Connector<connector></connector>标签中添加URIEncoding="UTF-8"属性,该属性用来解决GET中的编码问题。
xml 代码
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
步骤二:在每个需要提交表单参数的JSP/Servlet之前加入下列代码来设置字符集,用于搞定POST请求:
java 代码
request.setCharacterEncoding("UTF-8");
这样基本就搞定了字符乱码问题了,实现上述问题的要求是所有的网页编码必须是UTF-8编码既。
在JSP中:
jsp 代码
<%@page contentType="text/html" pageEncoding="UTF-8"%>
在Servlet中:
java 代码
response.setContentType("text/html;charset=UTF-8");
在所有的网页中:
html代码
<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代码
Document : zc
Created on : 2007-12-22, 17:20:24
Author : 啊春
-->
>
<html>
<head>
<title>title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
head>
<body>
<form action="/wat1/t1" method="GET">
姓名:<input type="text" name="name" value="" size="20" />
密码:<input type="password" name="passwd" value="" size="20" />
<input type="submit" value="注册" />
form>
<br />
<form action="/wat1/t1" method="POST">
姓名:<input type="text" name="name" value="" size="20" />
密码:<input type="password" name="passwd" value="" size="20" />
<input type="submit" value="注册" />
form>
body>
html>
java 代码
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here
out.println("");
out.println("");
相关文档:
jsp乱码问题,求助
下了个几年前的jsp源码,我装上后一直有中文乱码问题,数据库我用mysql,
并用sql-front操作,在用sql-front的时候已把字符设置为gb2312,
而且在sql-front中能正确显示中文,但就是在jsp页面上用getstring方法
得到乱码,我也用过getbytes(iso-8859-1)等方法试过还是没有 ......
一、JSP的技术原理
二、JSP的注释和声明
JSP注释用于表明在程序开发过程中的开发提示,不会输出到客户端;
<%--注释内容--%> —— JSP注释
<!--注释内容--!> —&md ......
jsp乱码解决(转载)
jsp乱码解决(转载)
JAVA知识 2009-04-14 08:49 阅读935 评论5
GBK编码
GBK编码是中国制订的、等同于UCS的新的中文编码扩展国家标准。GBK工作小组于1995年10月,同年12月完成GBK规范。该编码标准兼容GB2312,共收录汉字21003个、符号883个,并提供1 ......
今天在如下代码中:
********************************************************
<p><font color="blue">操作步骤:1、选择输出可选项 2、查询 3、输出
<br>
************************************************** ......