中文URL编码

 用一段代码来看:

package my;

import java.net.URLDecoder;
import java.net.URLEncoder;

public class Test
{
	
	/*张 -> %E5%BC%A0*/
	public static void test1()
	{
		String str = "张";
		String query = URLEncoder.encode(str);
		System.out.println( query );
	}
	
	/*%E5%BC%A0 -> 张*/
	public static void test2()
	{
		String str = "%E5%BC%A0";
		String query = URLDecoder.decode(str);
		System.out.println(query);
	}
	
	public static void main(String[] args)
	{
		test2();
	}

}

再如前端对后端数据的查询

中文URL编码_第1张图片

 

后端代码:

package my;

import java.io.IOException;
import java.util.List;

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

import org.json.JSONArray;

/**
 * Servlet implementation class TestQuery
 */
@WebServlet("/TestQuery")
public class TestQuery extends HttpServlet 
{

	/**
	 * @see HttpServlet#doGet(

HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
												throws ServletException, IOException 
	{
		
		String fromStr = request.getParameter("filter");

		
		//根据from/to进行查询
		Listrows =DemoDB.i.list(fromStr);
		
		JSONArray jary = new JSONArray(rows);
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		
		response.getWriter().write(jary.toString(2));
		
	}
}

前端代码



  
    Text.html
    
   

	 
    
	
	

  
  
  
   
学号 姓名 性别 手机号
暂无数据

 

你可能感兴趣的:(网站基础(JavaEE))