8 EL、JSTL、MVC、購物車、驗證碼
阿新 • • 發佈:2018-02-12
dispatch test war msg 存在 .get 生成隨機數 框架 輸入驗證碼 1.EL表達式
目的:從作用域中取出共享數據
${p.屬性名稱}
使用EL獲取當前應用的上下文路徑
${pageContext.getRequest().getContextPath()}
判斷集合是否為空:
${empty list}:表示判斷List既不能等於null,並且有元素,才會返回false
2.JSTL標簽(消除jsp中的java代碼)
拷貝jar包:jstl.jar standard.jar <%@ taglib uri="" prefix=""%> 常用標簽庫: 判斷語句標簽: <c:if test="${num==10}">等於10</c:if> 循環叠代標簽: <c:forEach items="${numList}" var="item"> ${item} </c:forEach> 日期格式化標簽: <fmt:formatDate value="${d}" pattern="yyyy-MM-dd HH:mm:ss"/>
3.MVC思想
Model1:jsp+javaBean(職責不分明)
Model2:JSp+Servlet+JavaBean(責任分離)
MVC:
M:Model:數據模型對象(封裝數據,處理業務邏輯):javaBean
V:View :展示界面,顯示數據
C:控制器(接收所有請求和界面跳轉):Servlet
MVC框架:Struts2/SpringMVC
4.簡單的購物車設計:
1):使用Session來完成.把購物車對象存放於Session中. 缺陷:瀏覽器關閉,session被銷毀,購物車就消失了. 2):可以使用Cookie完成.把購物車對象存儲於Cookie中. 缺陷:Cookie中的數據只能存在在當前電腦的當前瀏覽器中. 3):Cookie+數據庫. 如果登錄: 就直接把購物車對象信息存到到數據庫中(持久化存儲). 沒有登錄: 先把數據存儲在Cookie中,一旦登錄,就直接吧Cookie中的數據同步到數據庫中
5.驗證碼
一次性驗證碼的主要目的就是為了限制人們利用工具軟件暴力猜測密碼
1.將隨機數據存儲到Session中,用於和用戶提交的隨機碼進行比對
2.將隨機碼繪制成圖片通過Servlet響應到瀏覽器
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <script type="text/javascript"> function change(){ var img = document.getElementById("randomCodeImg"); img.src="/randomCode?"+new Date().getTime(); } </script> <body> <h1>登錄頁面</h1> <div style="color: red">${errorMsg}</div> <form action="/login" method="post"> 用戶:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> 驗證碼:<input type="text" name="randomCode" size="5" maxlength="5"> <img id="randomCodeImg" src="/randomCode" onclick="change()" style="cursor: pointer;" title="看不清,換一張"><br> <input type="submit" value="登錄"> </form> </body> </html>
@WebServlet("/randomCode")
public class RandomCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//生成隨機數
String randomCode = UUID.randomUUID().toString().substring(0, 5);
//把隨機數放進Session中
req.getSession().setAttribute("RANDOMCODE_IN_SESSION", randomCode);
//創建圖片對象
int width = 80;
int height = 40;
int imageType = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(width, height, imageType);
//畫板
Graphics g = image.getGraphics();
g.setColor(Color.YELLOW);
//繪制一個實心的矩形
g.fillRect(1, 1, width - 2, height - 2);
//把隨機數畫進圖片中
g.setColor(Color.BLACK);//設置隨機數的顏色
Font font = new Font("宋體", Font.BOLD + Font.ITALIC, 20);
g.setFont(font);//設置隨機數的字體和大小
g.drawString(randomCode, 10, 28);
//幹擾線
g.setColor(Color.GRAY);
Random r = new Random();
for (int i = 0; i < 100; i++) {
g.fillRect(r.nextInt(width), r.nextInt(height), 1, 2);
}
//關閉
g.dispose();
//把圖片對象以流的方式保存出去
ImageIO.write(image, "jpg", resp.getOutputStream());
}
}
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//接受請求參數
String username = req.getParameter("username");
String password = req.getParameter("password");
String randomCode = req.getParameter("randomCode");
//先校驗驗證碼是否正確
//獲取Sesion中的驗證碼
String sessionInCode = (String)req.getSession().getAttribute("RANDOMCODE_IN_SESSION");
if(!randomCode.equals(sessionInCode)){
req.setAttribute("errorMsg", "親,您輸入驗證碼不正確");
req.getRequestDispatcher("/randomcode/login.jsp").forward(req, resp);
return;
}
//做其他的業務(驗證賬號密碼)
System.out.println("驗證碼驗證通過,可以進行其他業務了");
}
}
8 EL、JSTL、MVC、購物車、驗證碼