servlet返回的中文乱码

pchobby 2004-11-11 10:51:31
Tomcat/5.0.12
搜索了一下,还是没有解决,代码如下

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

public class HelloServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
String clintName=request.getParameter("clintName");
if(clintName!=null)
clintName=new String(clintName.getBytes("ISO-8859-1"),"GB2312");
else
clintName="我的朋友";

//生成HTTP响应
PrintWriter out=response.getWriter();
String title="HelloServlet";
String head1="This is output from HelloServlet by doGet:";
response.setContentType("text/html;charset=GB2312");
out.println("<html><head><title>"+title+"</title>");
out.println("</head><body>");
out.println(head1);
out.println("<h1><p>"+clintName+":您好</h1>");
out.println("</body></html>");
out.close();
}

}
...全文
197 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
rootcn 2004-11-12
  • 打赏
  • 举报
回复
javac HelloServlet.java -encoding GBK
j2nix 2004-11-11
  • 打赏
  • 举报
回复
先搞清楚原理吧:

http://community.csdn.net/Expert/topic/3530/3530812.xml
freshman520 2004-11-11
  • 打赏
  • 举报
回复
没办法

只能换格式载入

选UNICODE
yifonger 2004-11-11
  • 打赏
  • 举报
回复
你看看试一试这样可不可以!:
public class HelloServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException
{
String clintName="";
String clintName1=new String(request.getParameter("clintName").getBytes("8859_1"));
if(clintName1!=null)
clintName=clintName1;
else
clintName="我的朋友";

//生成HTTP响应
PrintWriter out=response.getWriter();
String title="HelloServlet";
String head1="This is output from HelloServlet by doGet:";
response.setContentType("text/html;charset=GB2312");
out.println("<html><head><title>"+title+"</title>");
out.println("</head><body>");
out.println(head1);
out.println("<h1><p>"+clintName+":您好</h1>");
out.println("</body></html>");
out.close();
}

}
pchobby 2004-11-11
  • 打赏
  • 举报
回复
up

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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