Cookie功能沒有關閉
url重寫
第一次提交請求,在請求訊息沒有包含session的cookie,產生應答的時候用url重寫的技術
以後再提交請求的時候,請求訊息中就會包含session的cookie
Cookie功能沒有被禁掉:產生的應答訊息都會用url重寫
Session執行個體:一次性驗證碼
loginForm.html
<img src=”servlet/checkCode”>
CheckCodeServlet:產生驗證碼 放在session對象裡面
LoginFormServlet:輸入的驗證碼和圖片中顯示的驗證碼是否一致
實現代碼:
CheckCodeServlet部分
private static final long serialVersionUID = 1L;
private static int WIDTH = 60;
private static int HEIGHT = 20;// 圖片高為20像素
doGet方法中
response.setContentType("image/jpeg");// 應答內容的類型
ServletOutputStream out = response.getOutputStream();
HttpSession session = request.getSession();// 會話對象的擷取
// 不能在用戶端緩衝驗證碼圖片
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Controll", "no-cache");
response.setIntHeader("Expires", 0);// 設定失效時間
// 畫圖——在記憶體中開闢一片緩衝區————背景
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);// 制定畫紙的寬度和高度
Graphics g = bi.getGraphics();// 通過畫紙調用方法得到畫筆
drawBackground(g);
// 隨機產生驗證碼
char[] rands = generateCheckCode();// 隨機產生4位的驗證碼
// 輸出,畫在畫紙上
drawRands(g, rands);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bi, "JPEG", bos);// 按照什麼樣的形式轉換JEPG
// 定義一個位元組數組
byte[] buf = bos.toByteArray();// 將bi對象裡面內容轉換成了一個位元組數組
out.write(buf);
// 設定應答內容的長度
response.setContentLength(buf.length);
session.setAttribute("checkCode", new String(rands));
out.flush();
out.close();
}
drawRands方法
private void drawRands(Graphics g, char[] rands) {
// 畫筆顏色
g.setColor(Color.black);
// 字元輸出的字型,並加粗傾斜,字型大小為18
g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
// 把字元輸出
g.drawString("" + rands[0], 1, 15);// 第1個字元
g.drawString("" + rands[1], 16, 14);// 第2個字元
g.drawString("" + rands[2], 31, 18);// 第3個字元
g.drawString("" + rands[3], 46, 12);// 第4個字元
System.out.println("=============");
System.out.println(new String(rands));
}
generateCheckCode方法
private char[] generateCheckCode() {
String chars = "0123456789abcdefghijklmnopqrstuvwxyz";// 把所有可能的字元放在一個數組裡
// 定義一個用來存放隨機數的數組
char rands[] = new char[4];
for (int i = 0; i < 4; i++) {
int random = (int) (Math.random() * 36);// 隨機產生chars中的下標
rands[i] = chars.charAt(random);
}
return rands;
}
drawBackground方法
private void drawBackground(Graphics g) {
// 畫背景
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, WIDTH, HEIGHT);// 填充的矩形,左上方和右下角頂點座標
// 畫幹擾線
for (int i = 0; i < 120; i++) {
// 隨機產生資料,左上方座標
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
// 橢圓的顏色為隨機的
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
g.setColor(new Color(red, green, blue));
g.drawOval(x, y, 1, 0);// 隨機產生橢圓的線條,沒有被填充
}
}
LoginFormServlet部分
HttpSession session = request.getSession();
String code = request.getParameter("checkCode");
String value = session.getAttribute("checkCode").toString();
if(value.equals(code)){
out.println("驗證碼正確");
}else{
out.println("<font color=red>驗證碼不正確</font>");
}
loginForm.html部分
<h3>帶有驗證碼的登入頁面</h3>
<form method="post" action="loginFormServlet" name="form1">
使用者名稱:<input type="text" name="userName"/></br>
密 碼:<input type="password" name="password"/></br>
驗證碼:<input type="text" name="checkCode"/>
<img src="servlet/CheckCode"/></br>
<input type="submit" value="submit"/>
</form>