JSP表單提交中文亂碼解決方案


分2種提交方式,解決方案不同:

1、form表單提交方式為get

亂碼:

解決方案:

因為get方法是參數在URL中顯示,因為tomcat的URL編碼默認是:IOS-8859-1所以要么改tomcat

第1種方法(治本):tomcat-config-sever.xml 

加URIEncoding="utf-8"或者useBodyEncodingForURI="true"

第2種方法(治標):要么要針對性的對亂碼的參數進行單獨轉碼

<%
String username = request.getParameter("username");
String name = new String(username.getBytes("ios-8859-1"),"utf-8");

String password = request.getParameter("password");
out.print("--用戶名是:"+name+"--密碼是:"+password);
%>

 

2、form表單提交方式為post

直接在b.jsp中加入:

對請求的中文亂碼處理:

request.setCharacterEncoding("utf-8");

對響應的中文亂碼處理:

1 response.setCharacterEncoding("utf-8");

但是我們通常只用寫Request就可以了,因為jsp頁面頭部charset就是對響應的編碼

<%@ 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">

所以代碼應該是這樣寫的:

<%
    request.setCharacterEncoding("utf-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    out.print("--用戶名是:"+username+"--密碼是:"+password);
 %>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM