Java Servlet 解决中文问题

由于在C#中习惯了没有contentType的属性顺序,在java中还是跟在C#中一样的学法,结果忙了半天还是没搞定,
一开始:
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO 自动生成方法存根

String num=arg0.getParameter("num");


PrintWriter out=arg1.getWriter();
                arg1.setCharacterEncoding("GBK");
arg1.setContentType("text/html; charset=GBK");

out.println("<html>");
out.println("<head>");
out.println("<title>1+2+3+...+n=?</title>");
out.println("</head>");
out.println("<body>");
if(num==null||num.trim()=="") {out.print("请输入要计算的值");}
try
{
int count=Integer.parseInt(num),sum=0;
for(int i=1;i<=count;i++)
{
sum+=i;
if(i!=1)out.print("+");
out.print(i);
}
out.print("="+sum);
}
catch(Exception ex)
{}
out.println("</body>");
out.println("</html>");
}
页面上显示了是??????? 任我怎么改编码都是一样!
后来把代码改成
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// TODO 自动生成方法存根

String num=arg0.getParameter("num");



                arg1.setCharacterEncoding("GBK");
arg1.setContentType("text/html; charset=GBK");
                PrintWriter out=arg1.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>1+2+3+...+n=?</title>");
out.println("</head>");
out.println("<body>");
if(num==null||num.trim()=="") {out.print("请输入要计算的值");}
try
{
int count=Integer.parseInt(num),sum=0;
for(int i=1;i<=count;i++)
{
sum+=i;
if(i!=1)out.print("+");
out.print(i);
}
out.print("="+sum);
}
catch(Exception ex)
{}
out.println("</body>");
out.println("</html>");
}
就ok了,
看来要先渲染一下,再输出哈哈,开始又成功了一项了

你可能感兴趣的:(java,C++,c,servlet,C#)