所需jar包:https://pan.baidu.com/s/1hgUDCnAm_L9Rij5j-JXTHQ
1. jsp页面
<script type="text/javascript">
$(function(){
var content = "";
$.post(
"${pageContext.request.contextPath}/product?method=findCategoryList",
function(data){
//动态获取<li><a href="#">${cate.cname }</a></li>
for(var i=0;i<data.length;i++){
content+="<li><a href='${pageContext.request.contextPath}/product?method=findCategoryInfo&cid="+data[i].cid+"'>"+data[i].cname+"</a></li>";
}
//将拼接好的li放到ul中
$("#categoryUI").html(content);
},
"json"
);
});
</script>
<ul class="nav navbar-nav" id="categoryUI">
</ul>
2.servlet页面
//Ajax异步查询商品类别
public void findCategoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProductService dao = new ProductService();
List<Category> categoryList = dao.findCategoryList();
Gson gson = new Gson();
String json = gson.toJson(categoryList);
response.getWriter().write(json);
}
3.service层,dao层 略
------------------------------------------------------使用缓存技术--------------
1.jsp页面同上
2.servlet页面
public void findCategoryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//使用缓存技术
//先从缓存中查找categoryListJson,如果有直接使用,如果没有再从数据库中查找
//1 获得jedis对象 连接jedis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2 判断categoryListJson是否为空
if(categoryListJson == null) {
// System.out.println("从数据库中查询");
//没有接受的参数直接调用service层
ProductService service = new ProductService();
//查找商品类别
List<Category> categoryList = service.findCategoryList();
//json转换
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}
//解决中文乱码
response.setContentType("text/html;charset=UTF-8");
//将数据写回ajax
response.getWriter().write(categoryListJson);
}
3.工具类JedisPoolUtils.java
package com.wenhao.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolUtils {
private static JedisPool pool = null ;
static {
//加载配置文件
InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString())); //最大闲置个数
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString())); //最小闲置个数
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString())); //最大连接数
//1 创建Redis的连接池
pool = new JedisPool(poolConfig, pro.getProperty("redis.url"), Integer.parseInt(pro.get("redis.port").toString()));
}
//获得jedis资源的方法
public static Jedis getJedis() {
return pool.getResource();
}
public static void main(String[] args) {
Jedis jedis = getJedis();
System.out.println(jedis.get("username"));
}
}
4.redis.properties配置文件
redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=50
redis.url=192.168.75.134
redis.port=6379