- 歷史記錄可以session,可以cookie也可以redis,,在cookies時候是拼一個字符串記錄所在商品id用符號隔開,然后在返回原來頁面取出來,需要的參數沒有在上級找,再沒有繼續向上
----取出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);
// 定義一個記錄歷史商品信息的集合
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);
}
-------------在詳情頁放入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 本次訪問商品pid是8----->8-1-3-2
// 1-3-2 本次訪問商品pid是3----->3-1-2
// 1-3-2 本次訪問商品pid是2----->2-1-3
// 將pids拆成一個數組
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]
// 判斷集合中是否存在當前pid
if (list.contains(pid)) {
// 包含當前查看商品的pid
list.remove(pid);
list.addFirst(pid);
} else {
// 不包含當前查看商品的pid 直接將該pid放到頭上
list.addFirst(pid);
}
// 將[3,1,2]轉成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中異步加載數據$.post
- 動態引入時候在加載時候就將數據返回給頁面其余加載時候便會統一加載到數據(使用ajax加載header中的目錄條)
- 不經常改變的可以放在緩存,讀取快比如分類
-----------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();
}
// 獲得池子對象
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()));// 最大連接數
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綁定數據到jsp
<script type="text/javascript">
//header.jsp加載完畢后 去服務器端獲得所有的category數據
$(function(){
var content = "";
$.post(
"${pageContext.request.contextPath}/categoryList",
function(data){
//[{"cid":"xxx","cname":"xxxx"},{},{}]
//動態創建<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中的返回數據操作---gson轉json
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ProductService service = new ProductService();
//先從緩存中查詢categoryList 如果有直接使用 沒有在從數據庫中查詢 存到緩存中
//1、獲得jedis對象 連接redis數據庫
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2、判斷categoryListJson是否為空
if(categoryListJson==null){
System.out.println("緩存沒有數據 查詢數據庫");
//準備分類數據
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找錯可以先在servlet中查看進去沒有以區別錯誤位置
- select * from product order by pdate desc limit 0,9排序(降)要9條
- 重定向response,轉發requset
- jstl需要導包 prefix:c
- 默認歡迎界面在xml中只留一個,然后單獨建頁面使<%使用response跳轉%>
Paste_Image.png
Paste_Image.png
- 自定義validate
------只能使用同步,異步會是返回值flag 無法賦值
<script src="js/jquery.validate.min.js" type="text/javascript"></script>
//自定義校驗規則
$.validator.addMethod(
//規則的名稱
"checkUsername",
//校驗的函數
function(value, element, params) {
//定義一個標志
var flag = false;
//value:輸入的內容
//element:被校驗的元素對象
//params:規則對應的參數值
//目的:對輸入的username進行ajax校驗
$.ajax({
"async" : false,
"url" : "${pageContext.request.contextPath}/checkUsername",
"data" : {
"username" : value
},
"type" : "POST",
"dataType" : "json",
"success" : function(data) {
flag = data.isExist;
}
});
//返回false代表該校驗器不通過
return !flag;
}
);
1.解決也面向servlet傳中文-method=post,request.setCharacterEncoding("UTF-8");連用
2.validate導包前臺驗證注冊(看內存源碼)錯誤信息會先看原來代碼中有沒有(以class=error和for=sex來確定是為name=sex準備的)
<label class="error" for="sex" style="display:none ">您沒有第三種選擇</label>