中文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();
	}

}

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

 

后端代码:

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进行查询
		List<Student>rows =DemoDB.i.list(fromStr);
		
		JSONArray jary = new JSONArray(rows);
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		
		response.getWriter().write(jary.toString(2));
		
	}
}

前端代码

<!DOCTYPE html>
<html>
  <head>
    <title>Text.html</title>
    
   <meta charset="utf-8">

	<link rel="stylesheet" href="css/common.css"/> 
    <script type="text/javascript" src="JS/jquery-3.3.1.min.js"></script>
	
	<style>
		body{
			background-color: #F0F8FF;
		}
		/*主面板*/
		.main{
			width: 800px;
			margin: 20px auto;
			background-color: antiquewhite;
			min-height: 400px;
			padding: 10px;
			position: relative;
		}
		/*工具按钮区*/
		.main .toobar{
			margin: 10px 0px;
			text-align: center;
		}
		/*数据区*/
		.main .content{
			width: 400px;
			position: absolute;
			left: 200px;
			top: 80px;
			
		}
		/*无数据时的显示*/
		.main .content .empty{
			text-align: center;
			padding: 4px;
			display: block;
			border: 0px solid #888;
			border-width: 0px 1px 1px 1px;
		}
		/*列宽指定*/
		.main .table .c1{width: 80px;}
		.main .table .c2{width: 80px;}
		.main .table .c3{width: 80px;}
		.main .table .c4{width: 180px;}
	</style>

  </head>
  
  <body>
   <div class="main">
	   <div class="toobar">
		   <!--输入查询条件-->
		   <input type="text" class="from" placeholder="输入姓名" />
		   <button class="toobar" onclick="query()">查询</button>
	   </div>
   		
		<div class="content">
			<table class="table">
				<thead>
					<th class="c1"> 学号 </th>
					<th class="c2"> 姓名</th>
					<th class="c3"> 性别 </th>
					<th class="c4"> 手机号 </th>
				</thead>
				<tbody>
					
				</tbody>
			</table>
			<!--没有数据时显示这个-->
			<div class="empty">
				暂无数据
			</div>
		</div>
   </div>
  </body>
  <script>

	  
  		function query()
  		{
			
			/*请求参数*/
			var req = {};
			req.filter =$(".toobar .from").val().trim();
			
			console.log(req.from);
  			$.ajax({
				type:'GET',                   /*请求类型GET/POST*/
				url:'TestQuery',              /*服务URL,用相对地址*/
				dataType:"json",            /*期望服务器返回的数据类型*/
				data:req,                  /*附加请求参数*/
				success:function(resp){    /*已经将	服务器返回的数据转成JS对象*/
					 console.log(resp); 
					showResult(resp);
				}
			}); 
  		}
		
		function showResult(result)
		{
			var target = $(".main .content tbody")
			target.html("");  //清空
			for(var row of result)
			{
				var str ="<tr>"
				+"<td>" + row.id +"</td>"
				+"<td>" + row.name +"</td>"
				+"<td>" + (row.sex?"男":"女") +"</td>"
				+"<td>" + row.phone +"</td>"
				+"</tr>"
				;
				target.append(str);
			}
			
			//如果没有数据,则把下面的显示出来
			if(result.length>0)
				$(".main .content .empty").hide();
			else
				$(".main .content .empty").show();
			
		}
  </script>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值