package servlet;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;public class TestServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html;charset=gb2312"); //?显示页面的时候怎么不是中文
PrintWriter out=res.getWriter();
out.println("<html><head></head><body>");
out.println("name:"+req.getParameter("myname"));
out.println("<br>");
out.println("love:"+req.getParameter("love"));
out.println("</body></html>");
out.close();
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
doPost(req,res);
}
}
import javax.servlet.*;
import javax.servlet.http.*;public class TestServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html;charset=gb2312"); //?显示页面的时候怎么不是中文
PrintWriter out=res.getWriter();
out.println("<html><head></head><body>");
out.println("name:"+req.getParameter("myname"));
out.println("<br>");
out.println("love:"+req.getParameter("love"));
out.println("</body></html>");
out.close();
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException{
doPost(req,res);
}
}
response.setCharacterEncoding("gbk");
myname=new String(myname.getBytes("GBK"),"ISO-8859-1");
out.println(myname);
试试 肯定有用
以前因为这个问题被老师骂了好几次笨蛋!
req.setCharacterEncoding("GBK");]
out.println("name:"+req.getParameter("myname"));这句前面设置上面的语句!!!给分记的
PrintWriter out=res.getWriter();
String str=req.getParameter("myname");
String str1=req.getParameter("love");
str=new String(str.getBytes("ISO-8859-1",charset));
str1=new String(str1.getBytes("ISO-8859-1",charset));
out.println("<html><head></head><body>");
out.println("name:"+req.getParameter("myname"));
out.println("<br/>");
out.println("love:"+req.getParameter("love"));
out.println("</body></html>");
out.close();
所有的标单数据都会经过编码后以字节流方式发送,一般编码默认是用ISO-8859-1,编码方式可以通过浏览器查看,str1=new String(str1.getBytes("ISO-8859-1",charset));是把文本转化为原始的字节格式,然后用charset编码。
String myname=new String(req.getParameter("myname").getBytes("iso-8859-1"),"UTF-8");
response.setContentType("text/html; charset=GBK");
呵呵,那天我才刚调试过,绝对可以。不过给分哈,最近分比较紧张```:)