servlet驗(yàn)證碼顯示不出來(lái)
驗(yàn)證碼顯示一直是個(gè)× index.jsp <%@?page?language="java"?import="java.util.*"?pageEncoding="gbk"%> <% String?path?=?request.getContextPath(); String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"> <html> ??<head> ????<base?href="<%=basePath%>"> ???? ????<title>My?JSP?'index.jsp'?starting?page</title> <meta?http-equiv="pragma"?content="no-cache"> <meta?http-equiv="cache-control"?content="no-cache"> <meta?http-equiv="expires"?content="0">???? <meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3"> <meta?http-equiv="description"?content="This?is?my?page"> <!-- <link?rel="stylesheet"?type="text/css"?href="styles.css"> --> <script?type="text/javascript"> function?reloadCode(){ var?time?=?new?Date().getTime(); document.getElementById("imagecode").src="<%=request.getContextPath()?%>/servlet/ImageServlet?d="+time; } </script> ??</head> ?? ??<body> ??<form?action="<%=request.getContextPath()?%>/servlet/LoginServlet"?method="get"> ????驗(yàn)證碼:<input?type="text"?name="checkcode"/> ????<img?alt="驗(yàn)證碼"?id="imagecode"?src="<%=request.getContextPath()?%>/servlet/ImageServlet"/> ????<a?href="javascript:?reloadCode();">看不清楚</a><br> ????<input?type="submit"?value="提交"> ????</form> ??</body> </html>
imageservlet.java
package com.CheckCode;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet {
? ? protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ? //1.定義BufferedImage對(duì)象,參數(shù)(寬度,高度,圖片類(lèi)型)
? ? ? ? BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
? ? ? ? //2.獲得Graphics對(duì)象
? ? ? ? Graphics g = bi.getGraphics();
? ? ? ? //3.設(shè)置顏色
? ? ? ? Color c = new Color(200,150,255);
? ? ? ? //4.把顏色給g對(duì)象
? ? ? ? g.setColor(c);
? ? ? ? //5.畫(huà)框
? ? ? ? g.fillRect(0, 0, 68, 22);
? ? ? ??
? ? ? ? //6.指定驗(yàn)證碼內(nèi)容
? ? ? ? char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
? ? ? ? //7.創(chuàng)建random隨機(jī)對(duì)象獲取驗(yàn)證碼
? ? ? ? Random r = new Random();
? ? ? ? int len = ch.length; //獲取ch長(zhǎng)度
? ? ? ? int index; //創(chuàng)建一個(gè)隨機(jī)索引變量
? ? ? ? StringBuffer sb = new StringBuffer(); //用于保存驗(yàn)證碼的值
? ? ? ? //通過(guò)循環(huán)隨機(jī)取四位
? ? ? ? for (int i = 0; i < 4; i++) {
? ? ? ? ? ? index = r.nextInt(len);
? ? ? ? ? ? //設(shè)置字符隨機(jī)顏色
? ? ? ? ? ? g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));
? ? ? ? ? ? //設(shè)置字符隨機(jī)位置
? ? ? ? ? ? g.drawString(ch[index]+"", (i*15)+3, 18);
? ? ? ? ? ? //將驗(yàn)證碼添加到sb對(duì)象中
? ? ? ? ? ? sb.append(ch[index]);
? ? ? ? }
? ? ? ? //將驗(yàn)證碼保存到Session對(duì)象中,方便之后進(jìn)行驗(yàn)證
? ? ? ? request.getSession().setAttribute("piccode", sb.toString());
? ? ? ? ImageIO.write(bi, "JPG", response.getOutputStream());
? ? }
? ? protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
? ? ? ? super.doPost(req, resp);
? ? }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee ? ? ? ? ? ? ? ? ? ? ? http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
? ? <servlet-name>ImageServlet</servlet-name>
? ? <servlet-class>com.CheckCode</servlet-class>
? </servlet>
? <servlet-mapping>
? ? <servlet-name>ImageServlet</servlet-name>
? ? <url-pattern>/servlet/ImageServlet/</url-pattern>
? </servlet-mapping>
</web-app>
2018-01-17
web.xml中的<url-pattern>/servlet/ImageServlet/</url-pattern>應(yīng)該是<url-pattern>/servlet/ImageServlet</url-pattern>吧,多了個(gè)/