2017-11-25 98 views
0

當我從JSP頁面中的表單提交EUR(€)符號到Servlet時,我得到一個額外的2個隨機字符。 (我的IDE是IntelliJ)。爲什麼提交了其他兩個角色,並且有什麼我失蹤?在表單中提交歐元符號並獲取額外的2個字符

我在JSP頁面的格式如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Euro Form</title> 
</head> 
<body> 
<form action="/servlet" method="post"> 
    <input type="text" name="txtInput1" value=""> 
    <button type="submit">Submit</button> 
</form> 
</body> 
</html> 

這是由以下的Servlet處理:

package servlets; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 

@WebServlet(name = "Servlet", urlPatterns = {"/servlet"}) 
public class Servlet extends HttpServlet { 

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
     request.getRequestDispatcher("/WEB-INF/jsp/form.jsp").include(request, response); 
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String txtInput1 = request.getParameter("txtInput1"); 

     char[] chars = txtInput1.toCharArray(); 
     // ... 

     request.setAttribute("txtInput1", txtInput1); 
     request.getRequestDispatcher("/WEB-INF/jsp/form.jsp").include(request, response); 
    } 

} 

當我從表格提交歐元(€)符號Servlet,我收到一個隨機的字符集3,實際上,如下所示,但是當我將其打印回HTML頁面時,它顯示爲€符號...:

enter image description here

+1

這似乎是與一個unicode翻譯http://utf8-chartable.de/unicode-utf8-table.pl?start=8320&number=128&names=-&utf8=dec – Christian

+0

很好閱讀https://www.joelonsoftware.com/2003/10/08/the-絕對最低限度 - 每個軟件開發人員 - 絕對肯定 - 必須知道 - 關於unicode和字符集 - 無藉口/ – Christian

回答

0

它是與UNICODE字符集,事實上在UTF-8(這是你所使用的字符集)歐元符號由三重編碼做226 130 172

+0

我應該使用不同的字符集嗎? – Christian

+0

我認爲[UTF-8](https://en.wikipedia.org/wiki/UTF-8)很好,它是一個_variable寬度的字符encoding_,所以它的完全正常的一些字符是用1個字節編碼的, €符號)用3個字節編碼 – gtosto