解决客户端重定向,URL地址中文乱码的问题。nameString = URLEncoder.encode(nameString, "utf-8");

先通过表单向后台递交数据



  
    MyHtml.html
	
    
    
    
    
    

  
  
  
   
姓名:
密码:

数据递交到AddUser这个servlet之后,获取username,将username的值作为参数放在重定向地址的后面。按照下面的代码来写的话,,如果username是中文,重定向的url是:http://localhost:8080/servlet_06/AddUserSuccess?username=??

解决的办法是:将地址后面的nameString先做一下处理。加入一句:nameString = URLEncoder.encode(nameString, "utf-8");即可

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String nameString=request.getParameter("username");
		String contextPath=request.getContextPath();
		System.out.println(contextPath);
		System.out.println(nameString);

		//nameString = URLEncoder.encode(nameString, "utf-8");
		response.sendRedirect(contextPath+"/AddUserSuccess?username="+nameString);
	}

 

你可能感兴趣的:(解决客户端重定向,URL地址中文乱码的问题。nameString = URLEncoder.encode(nameString, "utf-8");)