亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

servlet驗證碼顯示不出來

驗證碼顯示一直是個×
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">
????驗證碼:<input?type="text"?name="checkcode"/>
????<img?alt="驗證碼"?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對象,參數(寬度,高度,圖片類型)

? ? ? ? BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);

? ? ? ? //2.獲得Graphics對象

? ? ? ? Graphics g = bi.getGraphics();

? ? ? ? //3.設置顏色

? ? ? ? Color c = new Color(200,150,255);

? ? ? ? //4.把顏色給g對象

? ? ? ? g.setColor(c);

? ? ? ? //5.畫框

? ? ? ? g.fillRect(0, 0, 68, 22);

? ? ? ??

? ? ? ? //6.指定驗證碼內容

? ? ? ? char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

? ? ? ? //7.創建random隨機對象獲取驗證碼

? ? ? ? Random r = new Random();

? ? ? ? int len = ch.length; //獲取ch長度

? ? ? ? int index; //創建一個隨機索引變量

? ? ? ? StringBuffer sb = new StringBuffer(); //用于保存驗證碼的值

? ? ? ? //通過循環隨機取四位

? ? ? ? for (int i = 0; i < 4; i++) {

? ? ? ? ? ? index = r.nextInt(len);

? ? ? ? ? ? //設置字符隨機顏色

? ? ? ? ? ? g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));

? ? ? ? ? ? //設置字符隨機位置

? ? ? ? ? ? g.drawString(ch[index]+"", (i*15)+3, 18);

? ? ? ? ? ? //將驗證碼添加到sb對象中

? ? ? ? ? ? sb.append(ch[index]);

? ? ? ? }

? ? ? ? //將驗證碼保存到Session對象中,方便之后進行驗證

? ? ? ? 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>


正在回答

1 回答

web.xml中的<url-pattern>/servlet/ImageServlet/</url-pattern>應該是<url-pattern>/servlet/ImageServlet</url-pattern>吧,多了個/

0 回復 有任何疑惑可以回復我~
#1

浮生半夏1 提問者

改了還是這樣^_^
2018-01-17 回復 有任何疑惑可以回復我~
#2

浮生半夏1 提問者 回復 浮生半夏1 提問者

問題找到了我<servlet-class>com.CheckCode</servlet-class>,寫錯了,改為com.CheckCode.ImageServlet
2018-01-17 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Java實現驗證碼制作
  • 參與學習       59932    人
  • 解答問題       132    個

本教程就會帶大家學習使用java實現各種驗證碼的方法

進入課程

servlet驗證碼顯示不出來

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號