将UTF-8字符串编码到Servlet

时间:2018-08-30 17:20:22

标签: jsp java-ee

我无法在servlet上获得类型şüöİğıç的字符。

退出时,我有这个:

Infos:debuggg++++++++++++++++++++login=   ? ü ö ? ? ? ç
Infos:debuggg+++++++++++++++++password=   ? ü ö ? ? ? ç

这是我的页面内容:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Welcome to My Page</h1>

        request encoding: <%= request.getCharacterEncoding()%><br />
        response encoding: <%= response.getCharacterEncoding()%><br /><br />

        <p>Data posted to this form was  :  
            <%
                request.setCharacterEncoding("UTF-8");
                out.print(request.getParameter("login"));
            %>  
        </p>  

        <form name="login" action="ServletController" method="Post">

            Login:<br/>
            <input type="text" name="login" value=""/><br/>

            Password:<br/>
            <input type="password" name="password" value=""/><br/>
            <br/>
            <input type="submit" value="Login" /><br/>
        </form>

        <p>ğşçkıolşşçiğpşüöİğıç</p>

    </body>
</html>

在servlet的出口处,我得到的字符与入口处不同:

package servlets;

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

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

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");

        String  login =  request.getParameter("login");         
        String password = request.getParameter("password");        

        System.out.println("debuggg++++++++++++++++++++++++login=   " + login);
        System.out.println("debuggg++++++++++++++++++++++++password=   " + password);

        this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    }

}

0 个答案:

没有答案