为了账号安全,请及时绑定邮箱和手机立即绑定

验证码出不来 好心人帮看看


jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

 <%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

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

</head>

<body>

验证码:<input type="text" name="checkcode"/>

<img alt="验证码" src="<%=request.getContextPath() %>/servlet/ImageServlet>"/>

<a href="javascript:reloadCode();">看不清楚</a><br>

</body>

</html>





package 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.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


public class ImageServlet extends HttpServlet {

/**

*/

private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{

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

Graphics g=bi.getGraphics();

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

g.setColor(c);

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

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

Random r=new Random();

int len=ch.length,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.append(ch[index]);

}

request.getSession().setAttribute("piccode", sb.toString());

ImageIO.write(bi, "JPG", response.getOutputStream());

}

}


<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>checkCode</display-name>

 <servlet>

  <servlet-name>ImageServlet</servlet-name>

  <servlet-class>com.muke.ImageServlet</servlet-class>

 </servlet>

 <servlet-mapping>

  <servlet-name>ImageServlet</servlet-name>

  <url-pattern>/Servlet/ImageServlet</url-pattern>

 </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

</web-app>


正在回答

1 回答

怎么出不来?截图!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java实现验证码制作
  • 参与学习       59906    人
  • 解答问题       128    个

本教程就会带大家学习使用java实现各种验证码的方法

进入课程

验证码出不来 好心人帮看看

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信