急了,为什么在Tomcat5.0.16中还是显示乱码?

laoduan518 2004-03-17 11:02:16
程序应该是没有问题了,为什么还是显示乱码?
但在Tomcat4.0没有这个情况。
//////html 文件/////
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JSP-Servlet测试</title>
</head>

<body>
<form name="form1" method="get" action="/test/servlet/TestServlet">
<p>这是一个JSP和Servlet的测试</p>
<p><br>请输入你的名字:<input type="text" name="name"></p>
<p><br>请输入你的电子邮件:<input type="text" name="email"></p>
<p><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置">
</p>
</form>
</body>
</html>

/////////TestServlet.java///////
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html;charset=GBK";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("GBK");/////
response.setContentType(CONTENT_TYPE);///

PrintWriter out = response.getWriter();
String na=request.getParameter("name");
String em=request.getParameter("email");
out.println("<html>");
out.println("<head><title>HelloServlet</title></head>");
out.println("<body bgcolor=\"#ff0000\">");
out.println("<center>");
out.println("<p>Hello,You come from:"+request.getRemoteAddr()+"</p>");
out.println("<p>You come from:"+request.getRemoteHost()+"<p>");
out.println("<p>You are Name:"+request.getServerName()+"<p>");
out.println("<p>你的端口:"+request.getServerPort()+"<p>");
out.println("<p>你的姓名:"+na);
out.println("<p>你的电子邮件地址:"+em);
out.println("<center>");
out.println("</body></html>");
}
public void destroy() {
}
}
...全文
55 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
clarepuma 2004-03-18
  • 打赏
  • 举报
回复
呵呵,这样的啊。。。
laoduan518 2004-03-18
  • 打赏
  • 举报
回复
经过我的不泄努力,问题终于解决了。原来问题是出在处理参数传递的方法上:如果在servlet中用doGet(HttpServletRequest request, HttpServletResponse response)方法进行处理的话前面即使是写了:
request.setCharacterEncoding("GBK");
response.setContentType(CONTENT_TYPE);
也是不起作用的,返回的中文还是乱码!!!
如果把这个函数改成doPost(HttpServletRequest request, HttpServletResponse response)一切就OK了。
同样,在JSP页面中处理表单输入之所以能显示中文不仅要写上这个
<%request.setCharacterEncoding("GBK");%>语句还要
用post方法传递的,改成get方法依旧不行。
由此可见在servlet中用doGet()方法或是在JSP中用get方法进行处理要注意。这毕竟涉及到要通过浏览器传递参数信息,很有可能引起常用字符集的冲突或是不匹配。
以上问题在Tomcat5.0.16(或者说是在5.0.14、5.0.18、5.0.19)中解决通过!
有兴趣的朋友可以试一下,看和我说的一样吧。
clarepuma 2004-03-18
  • 打赏
  • 举报
回复
你的servlet里面没写,你试试。。。
laoduan518 2004-03-18
  • 打赏
  • 举报
回复
不行的,我上面的程序已经写了
clarepuma 2004-03-18
  • 打赏
  • 举报
回复
response.setContentType("text/html; charset=GB2312");
request.setCharacterEncoding("gb2312");
laoduan518 2004-03-18
  • 打赏
  • 举报
回复
是Servlet里的
clarepuma 2004-03-18
  • 打赏
  • 举报
回复
是JSP里的页里面乱码还是Servlet里的页产生的乱码?
laoduan518 2004-03-17
  • 打赏
  • 举报
回复
怎么没人顶吗?

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧