java - servlet中,用浏览器打印HTTP表单,出现乱码的问题,下面是相关的代码
巴扎黑
巴扎黑 2017-04-17 17:48:05
0
3
286

用chrome 和 IE都试过(也把编码换成 UTF-8),网址输入为 :
http://localhost:8080/mydoodling/info?username=张
动态生成的网页内容为: 你好寮?
请问中文代码到底是哪个?
获取浏览器表单参数的代码为:

public class ServletInfo extends HttpServlet        
{
        public void doGet ( HttpServletRequest req , HttpServletResponse resp) throws IOException 
        {
                String username = req . getParameter ( "username") ;
                if (null != username)
                {
                        username = new String ( username.getBytes ( "ISO-8859-1")  , "GB2312") ;
                }
                resp . setContentType ( "text/html;charset=GB2312") ; 
                PrintWriter out = resp . getWriter ( ) ;
                out . println ("<html><body>"+"你好" + username+"</body></html>") ; 
                out . close ( ) ;
        }                
}
巴扎黑
巴扎黑

全部回覆(3)
洪涛
  1. 檢查request.setCharacterEncoding()設定或是過濾器中字元編碼過濾器,保持解碼格式跟設定的編碼格式一致

  2. 對於GET請求Tomcat會使用ISO-8859-1編碼之後使用UTF-8編碼

洪涛

查看一下jsp頁面上的字元編碼

PHPzhong

典型的url中文亂碼啊,以下擇一用之。
1.tomcat的設定檔server.xml中標籤裡加上URIEncoding="UTF-8「
2.方法第一行增加request.setCharacterEncoding(“UTF-8”)#🎜 🎜#

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板