Character encoding JSP -displayed wrong in JSP but not in URL: “á » á é » é”

后端 未结 10 1732
忘掉有多难
忘掉有多难 2020-12-03 08:24

I have this Web Application in JSP running on JBoss Application Server. I am using Servlets for friendly urls. I\'m sending search parameters through my JSP\'s and Servlets.

相关标签:
10条回答
  • 2020-12-03 08:39

    The problem is that the information sent by the browser hasn't got a well-defined encoding and there's no way in HTTP to specify it.

    Luckily most browsers will use the encoding of the page that contains the form. So if you use UTF-8 in all your pages, then most browsers will send all data in UTF-8 encoding as well (and your examples show that that's exactly how it is sent).

    Unfortunately the most common Java application servers don't really handle the case (can't blame them, it's mostly guesswork anyway).

    You can tell your application server to treat any input as UTF-8, by calling

    request.setCharacterEncoding("UTF-8");
    

    Based on your coding style and the frameworks you use, it might be to late when the control flow reaches your code, so it might be possible to do that in a javax.servlet.Filter.

    0 讨论(0)
  • 2020-12-03 08:41

    Just a wild guess. Try this inside your JSP/Servlet:

    if(request.getCharacterEncoding() == null) {
       request.setCharacterEncoding("UTF-8");
    }
    

    You need to be sure that the correct encoding is passed to your servlet.

    0 讨论(0)
  • 2020-12-03 08:41

    There are three layers to configure. From what you've described, it sounds like your problem lies in the database configuration.

    1. Browser Display and Form Submission

    JSP

    <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
    

    HTML

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    1. Web Server Processing

    JSP

    <%
      request.setCharacterEncoding("UTF-8");
      String name = request.getParameter("NAME");
    %>
    

    Same type of thing in a Servlet. See JBoss specific solution as well as complete server independent solution in this answer.

    1. Database Settings

    You may be losing the character information at the database level. Check to make sure your database encoding is UTF-8 also, and not ASCII.

    For a complete discussion of this topic, refer to Java article Character Conversions from Browser to Database.

    0 讨论(0)
  • 2020-12-03 08:46

    response.setCharacterEncoding("UTF-8");

    0 讨论(0)
  • 2020-12-03 08:48

    We had a similar problem. It was solved when all JSPs have been saved with the UTF-8 BOM.

    0 讨论(0)
  • 2020-12-03 08:51

    Check out the connecter setting in your tomcat config. There is an option (URIEncoding) you can set to treat URIs as UTF-8. By default they are treated as ISO-8859-1.

    0 讨论(0)
提交回复
热议问题