對於Servlet的get請求和post請求的兩種數據請求的編碼格式

@WebServlet(name = "EncodingServlet",urlPatterns = "/e/es")
public class EncodingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println(request.getCharacterEncoding());
        //這裏的參數是在jsp文件中設置的參數屬性 在獲取參數之前要使用上面的轉碼
        System.out.println("POST---username:"+request.getParameter("username"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //同理  這裏的參數也是jsp文件設置的參數屬性
        String uname = request.getParameter("username");
        //通過iso編碼,將字符串uname回退到字節數組狀態
        byte[] bytes = uname.getBytes("iso-8859-1");
        System.out.println(request.getCharacterEncoding());
        //根據該字節數組,創建一個符合utf-8編碼的字符串
        uname = new String(bytes,"utf-8");
        System.out.println("GET---username:"+uname);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章