servlet乱码
所有文件如下
package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest requst, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType( "text/html ");
PrintWriter out = response.getWriter();
out.println( "Beer Selection Advice <br> ");
String s = requst.getParameter( "color ");
out.println( " <br> Get beer color " + s);
}
}
<?xml version= "1.0 " encoding= "ISO-8859-1 "?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN "
"http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<servlet>
<servlet-name> Beer </servlet-name>
<servlet-class> com.example.web.BeerSelect </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> Beer </servlet-name>
<url-pattern> /Select.do </url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN ">
<html>
<head>
<title> form.html </title>
<meta http-equiv= "keywords " content= "keyword1,keyword2,keyword3 ">
<meta http-equiv= "description " content= "this is my first page ">
<meta http-equiv= "content-type " content= "text/html; charset=GBK ">
<!-- <link rel= "stylesheet " type= "text/css " href= "./styles.css "> -->
</head>
<body>
<h1 align=center>
啤酒选择页面
</h1>
<form method=POST action=Select.do>
选择啤酒属性:
<select name=color size=1>
<option>
light
<option>
amber
<option>
brown
<option>
red
</select>
<br>
<br>
<center>
<input type=submit value=提交>
</center>
</form>
</body>
</html>
在TOMCAT中运行
提交后的页面是一堆“????”
这是怎么回事?
改怎么弄?
[解决办法]
我也刚遇到这个问题
你在response.setContentType( "text/html ");后面加上
request.setCharacterEncoding( "gbk "); 试试
[解决办法]
在response.setContentType( "text/html ");后面加上
request.setCharacterEncoding( "utf-8 "); 试试
[解决办法]
request.setCharacterEncoding( "gbk ");在所有语句的最前面
[解决办法]
我也一直被困扰着
我是在LINIX中运行的
我感觉这和浏览器的显示编码方式有关
在JSP中 <%@ page....%> 的一个属性可以设置,就没有问题了
[解决办法]
在request.setCharacterEncoding( "gbk ");
在控制台上打印以下看是不是乱码;
基本上了 如果使用了这个语句
有在JSP中加了 <@ page ...../> 就应该没什么问题了
[解决办法]
建议,看看tomcat的配置文件。是不是那编码的问题。
[解决办法]
发送数据--> 到服务器--> 数据库--> 服务器--> 客户端都采用统一编码
[解决办法]
页面设置pageEncoding
设置filter
response.setContentType()时带上charset= 'xx '
统一编码 -> 木有乱码
[解决办法]
response.setContentType( "text/html ");==> response.setContentType( "text/html;charset=gb2312 ");
[解决办法]
加个filter 在web.xml里配置一下
直接把下面的复制粘贴就行了
注意:你的页面编码要支持中文
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter( "encoding ");
String value = filterConfig.getInitParameter( "ignore ");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase( "true "))
this.ignore = true;
else if (value.equalsIgnoreCase( "yes "))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
