日期:2014-05-20  浏览次数:20789 次

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....%> 的一个属性可以设置,就没有问题了