如何在Servlet中得到传过来的中文字

change 2002-04-17 02:33:01
我有一个页面,里面有个form,post到一个servlet.
form中有一个text input.
我想在text中输入中文,并在Servlet中把它回显到页面中。
但是现在我从servlet的 request.getParameter(xxx)中得到的 String是乱码。
这是怎么回事?
<HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<BODY>
<FORM Action="http://localhost:8100/servlet/MyServlet" method=post>
<input type="text" name="val" value="aaab">
<input type="submit" name="submit" value="submit">
</FORM>
</BODY>

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

public class MyServlet extends HttpServlet {
public MyServlet() {
}

public void init(ServletConfig config) throws ServletException {

}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
String parm = req.getParameter("val");
System.err.println(parm);

res.setContentType("text/html");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Pragma","no-cache");
res.setDateHeader("Expires", 0);

OutputStream output = res.getOutputStream();

PrintWriter writer = new PrintWriter(new OutputStreamWriter(output,"UTF8"));
writer.println("<HTML><Head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></Head><Body>");
writer.println(parm);
writer.println("</Body></HTML>");
writer.flush();

}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
...全文
94 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
expert2000 2002-04-17
  • 打赏
  • 举报
回复
有时候是操作系统和应用软件的问题,不是你的程序的问题.我在WINDOWS下用DEPHI编写的程序在LINUX下用KYLIX重新编写就得不到同样的结果.尤其在界面和字体显示上,不同的LINUX 也有不同的表现.
change 2002-04-17
  • 打赏
  • 举报
回复
OK
I got it.
Thanks a very lot.
sunshine_shen 2002-04-17
  • 打赏
  • 举报
回复
String str = request.getParameter("text");
str = new String(str.getBytes(),"ISO8859_1");
iloveyouonlyonce 2002-04-17
  • 打赏
  • 举报
回复
需要转码,肯定可以显示,你再检查一下你的代码
change 2002-04-17
  • 打赏
  • 举报
回复
还是不行呀
pengji 2002-04-17
  • 打赏
  • 举报
回复
String str=new String(request.getParameter(xxx).getBytes("8859_1"), "gb2312");
sharetop 2002-04-17
  • 打赏
  • 举报
回复
你需要转码:
String str = request.getParameter("text");
str = new String(str.getBytes(),"GB2312");

81,122

社区成员

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

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