按提交鍵后亂碼問題又出現了!之前添加request.setCharacterEncoding("utf-8");之后是沒有的。上一節增加URIEncoding="utf-8"并沒有去除按url提交的亂碼問題。我將上一節在service.xml文件增加的URIEncoding="utf-8"刪除了也沒有用。
按提交鍵后亂碼問題又出現了!之前添加request.setCharacterEncoding("utf-8");之后是沒有的。上一節增加URIEncoding="utf-8"并沒有去除按url提交的亂碼問題。我將上一節在service.xml文件增加的URIEncoding="utf-8"刪除了也沒有用。
<%@?page?language="java"?contentType="text/html;?charset=utf-8" ????pageEncoding="utf-8"%> <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>Insert?title?here</title> </head> <body> <h1>request內置對象</h1> <%?request.setCharacterEncoding("utf-8"); request.setAttribute("password","1234567asdfas");? %> 用戶名:<%=request.getParameter("username")?%> 愛好:<% if(request.getParameterValues("favorite")!=null){ String[]?favorites=request.getParameterValues("favorite"); for(int?i=0;i<favorites.length;i++){ out.println(favorites[i]+" "); } } %> <br> 密碼:<%=request.getAttribute("password")%><br> 請求體的MIME類型:<%=request.getContentType()?%><br> 協議類型及版本號:<%=request.getProtocol()?%><br> 服務器主機名:<%=request.getServerName()?%><br> 服務器端口號:<%=request.getServerPort()%><br> 請求文件的長度:<%=request.getContentLength()?%><br> 請求的真實路徑:<%=request.getRealPath("request.jsp")%> </body> </html> <%@?page?language="java"?contentType="text/html;?charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>Insert?title?here</title> </head> <body> <h1>快快注冊</h1> <hr> <form?action="request.jsp"?name="regFrom"?method="get"> <table> <tr> <td>用戶名:</td> <td><input?type="text"?name="username"></td> </tr> <tr> <td>愛好:</td> <td><input?type="checkbox"?name="favorite"?value="讀書">讀書 <input?type="checkbox"?name="favorite"?value="看書">看書?<input type="checkbox"?name="favorite"?value="寫書">寫書?<input type="checkbox"?name="favorite"?value="抄書">抄書?<input type="checkbox"?name="favorite"?value="收書">收書</td> </tr> <tr> <td><input?type="submit"?value="提交"></td> </tr> </table> </form> </body> <a?href="request.jsp?username=三八佬">測試URL傳參數</a> </html>
2015-12-24
你提交的方法是get,因為get方法在tomcat中默認使用ISO-8859-1來傳輸數據,當你的jsp接收到請求后卻用utf-8來解碼,這樣就不可能得到正確的內容,如果配置server.xml確實能夠解決這個問題,但是你皮質之后問題沒有解決,可能是你配置錯誤,或者是eclipse沒有進行相應的配置導致的,出現中文亂碼的情況有多種,多多嘗試就能解決