- 歷史記錄可以session陆爽,可以cookie也可以redis,扳缕,在cookies時(shí)候是拼一個(gè)字符串記錄所在商品id用符號(hào)隔開(kāi)慌闭,然后在返回原來(lái)頁(yè)面取出來(lái),需要的參數(shù)沒(méi)有在上級(jí)找躯舔,再?zèng)]有繼續(xù)向上
----取出cookie---
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 獲得cid
String cid = request.getParameter("cid");
String currentPageStr = request.getParameter("currentPage");
if (currentPageStr == null)
currentPageStr = "1";
int currentPage = Integer.parseInt(currentPageStr);
int currentCount = 12;
ProductService service = new ProductService();
PageBean pageBean = service.findProductListByCid(cid, currentPage, currentCount);
request.setAttribute("pageBean", pageBean);
request.setAttribute("cid", cid);
// 定義一個(gè)記錄歷史商品信息的集合
List<Product> historyProductList = new ArrayList<Product>();
// 獲得客戶端攜帶名字叫pids的cookie
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("pids".equals(cookie.getName())) {
String pids = cookie.getValue();// 3-2-1
String[] split = pids.split("-");
for (String pid : split) {
Product pro = service.findProductByPid(pid);
historyProductList.add(pro);
}
}
}
}
// 將歷史記錄的集合放到域中
request.setAttribute("historyProductList", historyProductList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
-------------在詳情頁(yè)放入cookie---------------
// 獲得客戶端攜帶cookie---獲得名字是pids的cookie
String pids = pid;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("pids".equals(cookie.getName())) {
pids = cookie.getValue();
// 1-3-2 本次訪問(wèn)商品pid是8----->8-1-3-2
// 1-3-2 本次訪問(wèn)商品pid是3----->3-1-2
// 1-3-2 本次訪問(wèn)商品pid是2----->2-1-3
// 將pids拆成一個(gè)數(shù)組
String[] split = pids.split("-");// {3,1,2}
List<String> asList = Arrays.asList(split);// [3,1,2]
LinkedList<String> list = new LinkedList<String>(asList);// [3,1,2]
// 判斷集合中是否存在當(dāng)前pid
if (list.contains(pid)) {
// 包含當(dāng)前查看商品的pid
list.remove(pid);
list.addFirst(pid);
} else {
// 不包含當(dāng)前查看商品的pid 直接將該pid放到頭上
list.addFirst(pid);
}
// 將[3,1,2]轉(zhuǎn)成3-1-2字符串
StringBuffer sb = new StringBuffer();
for (int i = 0; i < list.size() && i < 7; i++) {
sb.append(list.get(i));
sb.append("-");// 3-1-2-
}
// 去掉3-1-2-后的-
pids = sb.substring(0, sb.length() - 1);
} } }
Cookie cookie_pids = new Cookie("pids", pids);
response.addCookie(cookie_pids);
request.getRequestDispatcher("/product_info.jsp").forward(request, response);
}
- 腳本:在jsp中寫<%java代碼%>驴剔?;ajax粥庄,在script中異步加載數(shù)據(jù)$.post
- 動(dòng)態(tài)引入時(shí)候在加載時(shí)候就將數(shù)據(jù)返回給頁(yè)面其余加載時(shí)候便會(huì)統(tǒng)一加載到數(shù)據(jù)(使用ajax加載header中的目錄條)
- 不經(jīng)常改變的可以放在緩存丧失,讀取快比如分類
-----------redis工具包---
-----配置
redis.maxIdle=30
redis.minIdle=10
redis.maxTotal=100
redis.url=localhost
redis.port=6379
------
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();
}
// 獲得池子對(duì)象
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.maxIdle").toString()));// 最大閑置個(gè)數(shù)
poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));// 最小閑置個(gè)數(shù)
poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));// 最大連接數(shù)
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("xxx"));
}
}
------------------ajax綁定數(shù)據(jù)到j(luò)sp
<script type="text/javascript">
//header.jsp加載完畢后 去服務(wù)器端獲得所有的category數(shù)據(jù)
$(function(){
var content = "";
$.post(
"${pageContext.request.contextPath}/categoryList",
function(data){
//[{"cid":"xxx","cname":"xxxx"},{},{}]
//動(dòng)態(tài)創(chuàng)建<li><a href="#">${category.cname }</a></li>
for(var i=0;i<data.length;i++){
content+="<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>";
}
//將拼接好的li放置到ul中
$("#categoryUl").html(content);
},
"json"
);
});
</script>
--------------------categoryListservlet中的返回?cái)?shù)據(jù)操作---gson轉(zhuǎn)json
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
//先從緩存中查詢categoryList 如果有直接使用 沒(méi)有在從數(shù)據(jù)庫(kù)中查詢 存到緩存中
//1、獲得jedis對(duì)象 連接redis數(shù)據(jù)庫(kù)
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2惜互、判斷categoryListJson是否為空
if(categoryListJson==null){
System.out.println("緩存沒(méi)有數(shù)據(jù) 查詢數(shù)據(jù)庫(kù)");
//準(zhǔn)備分類數(shù)據(jù)
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
- Long is=0L;
- servlet找錯(cuò)可以先在servlet中查看進(jìn)去沒(méi)有以區(qū)別錯(cuò)誤位置
- select * from product order by pdate desc limit 0,9排序(降)要9條
- 重定向response布讹,轉(zhuǎn)發(fā)requset
- jstl需要導(dǎo)包 prefix:c
- 默認(rèn)歡迎界面在xml中只留一個(gè),然后單獨(dú)建頁(yè)面使<%使用response跳轉(zhuǎn)%>
Paste_Image.png
Paste_Image.png
- 自定義validate
------只能使用同步训堆,異步會(huì)是返回值flag 無(wú)法賦值
<script src="js/jquery.validate.min.js" type="text/javascript"></script>
//自定義校驗(yàn)規(guī)則
$.validator.addMethod(
//規(guī)則的名稱
"checkUsername",
//校驗(yàn)的函數(shù)
function(value, element, params) {
//定義一個(gè)標(biāo)志
var flag = false;
//value:輸入的內(nèi)容
//element:被校驗(yàn)的元素對(duì)象
//params:規(guī)則對(duì)應(yīng)的參數(shù)值
//目的:對(duì)輸入的username進(jìn)行ajax校驗(yàn)
$.ajax({
"async" : false,
"url" : "${pageContext.request.contextPath}/checkUsername",
"data" : {
"username" : value
},
"type" : "POST",
"dataType" : "json",
"success" : function(data) {
flag = data.isExist;
}
});
//返回false代表該校驗(yàn)器不通過(guò)
return !flag;
}
);
1.解決也面向servlet傳中文-method=post描验,request.setCharacterEncoding("UTF-8");連用
2.validate導(dǎo)包前臺(tái)驗(yàn)證注冊(cè)(看內(nèi)存源碼)錯(cuò)誤信息會(huì)先看原來(lái)代碼中有沒(méi)有(以class=error和for=sex來(lái)確定是為name=sex準(zhǔn)備的)
<label class="error" for="sex" style="display:none ">您沒(méi)有第三種選擇</label>