1.? 表單數(shù)據(jù)傳送到后臺
String username = request.getParameter("username");
通過該段代碼,我們可以用key(name)取到value国瓮。所以搞清楚html文件標簽的name和value很重要马昨。
2.? 亂碼問題
2.1 doGet的解決辦法
response.setContentType("text/html;charset=UTF-8");
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
2.2 doPost的解決辦法
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
3.? 頁面跳轉(zhuǎn)
設(shè)置跳轉(zhuǎn):response.setHeader("refresh", "3;url=login.html");//在3秒后跳轉(zhuǎn)到login.html
4.? 3個作用域
ps:能用作用域小的盡量用小的
4.1 ServletContext
全局都可以獲取的到捣卤,所以基本用處不大。感覺用這個還不如用全局變量理疙。
4.2 Session
作用在一次會話中(打開瀏覽器訪問从媚,應該就算一次會話)
//把驗證碼發(fā)到登錄處理器去驗證
HttpSession session = request.getSession();
session.setAttribute("code", code);
HttpSession session = request.getSession();
//獲取通過Session傳來的驗證碼
String code = (String) session.getAttribute("code");
ps:在jsp中痹扇,不用定義就有session對象悯舟;
4.3 Request
一般來說是在一個網(wǎng)頁界面中傳遞
//發(fā)送
request.setAttribute("code", code);
//接受
String code = (String) request.getAttribute("code");
5.? 重定向與轉(zhuǎn)發(fā)
重定向或是轉(zhuǎn)發(fā)過后,頁面都會實現(xiàn)跳轉(zhuǎn)
5.1 重定向
response.sendRedirect("index.html");
5.2 轉(zhuǎn)發(fā)
request.getRequestDispatcher("login.jsp").forward(request, response);
關(guān)于request:
關(guān)于session:
6.? 驗證碼
6.1 步驟
(1)獲取畫筆
int width = 100;
int height = 25;
//設(shè)置圖像? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 寬? ? ? 高? ? ? ? ? ? ? ? 圖像類型
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//通過圖像獲取畫筆
Graphics graphics = image.getGraphics();
(2)填充區(qū)域
//? ? ? ? ? ? ? ? ? ? ? ? 位置? ? ? ? 寬高
graphics.fillRect(0, 0, width, height);
(3)寫字
//設(shè)置字體樣式? ? ? ? ? ? ? ? ? ? ? name? ? 字體樣式? 大小
graphics.setFont(new Font("宋體", Font.BOLD, 20));
//畫字
graphics.drawString(string , x , y);
//畫線
graphics.drawLine(x1, y1, x2, y2);
(4)輸出
ImageIO.write(image, "jpg", response.getOutputStream());
6.2? 點擊更換驗證碼
<img alt="驗證碼" src="Verification" onclick="this.src = 'Verification?'+new Date().getTime()">