Home > Java > JavaBase > body text

Several solutions to garbled code problems in Java

Release: 2019-12-21 15:53:48
Original
2693 people have browsed it

Several solutions to garbled code problems in Java

Several solutions to java garbled situations:

1. Garbled characters appear when the data passed to the server through get is obtained in the Servlet;

 public class RegistServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("userName");
        byte[] bytes = name.getBytes("ISO8859-1");
        String newName = new String(bytes,"UTF-8");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
Copy after login

Analysis: Define a name variable in the doGet method to obtain the client data userName encapsulated in the server, then assign the name to the byte array bytes in the form of "ISO8859-1", and finally use the bytes array in the form of UTF-8 Assign a value to a newly created String variable newName. At this time, newName is the data that can be displayed normally. The reason why it is so troublesome is because there is no direct way to solve it in one line of code. You can regard this method as a fixed usage.

2. Garbled characters appear when obtaining data passed to the server through post in the Servlet;

public class RegistServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //注意:post方式提交数据的乱码解决方式,放在getParameXXX方法之前
        req.setCharacterEncoding("UTF-8");
        //得到前台input框中name="username"和password="password"的value值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
Copy after login

Analysis: Solving the garbled problem in the post delivery mode is very simple, just req.setCharacterEncoding("UTF -8”); This line of code, but please note that this sentence must be placed before obtaining the data.

3. Garbled characters appear when Servlet responds data to the client through the server;

public class RegistServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //方式一:
        resp.setContentType("text/html;charset=utf-8");
        //方式二:
        resp.setHeader("Content-type", "text/html");
        resp.setCharacterEncoding("UTF-8");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
Copy after login

Analysis: Note that the above two methods must be written before the output method when applied. In addition, the two The method has the same effect, because method one is simpler and is commonly used.

4. Garbled characters appear when HTML or JSP pages are displayed on the client.

<head>
        <meta charset="UTF-8">
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>form表单</title>
</head>
Copy after login

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Several solutions to garbled code problems in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template