JavaEE-動態(tài)網(wǎng)頁數(shù)據(jù)傳輸

1.? 表單數(shù)據(jù)傳送到后臺

String username = request.getParameter("username");

通過該段代碼,我們可以用key(name)取到value。所以搞清楚html文件標(biāo)簽的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

作用在一次會話中(打開瀏覽器訪問,應(yīng)該就算一次會話)

//把驗證碼發(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()">

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容