不管裡搞什麼開發,都離不開亂碼處理問題。
下面分享一下在表單提交過程中的亂碼問題。
頁面一:login.html 提交用戶名和年齡。
servler :獲取提交的數據並打印。代碼如下
Javapublic class chuliServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//代碼一
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
//代碼二
name=new String(name.getBytes("ISO8859-1"),"UTF-8");
String age=req.getParameter("age");
System.out.print("name:"+name+"age"+age);
//網頁端
resp.setHeader("content-type", "text/html; charset=UTF-8");
resp.getWriter().write("name:"+name+"age:"+age);
}
顯示結果