jsp页面实现验证码功能

这个是根据代码改编成自己的。验证码部分的实现代码,如果有用的话,可以直接封装成类,调用就行了。

第一步建一个Servlet的类,里面实现验证码的代码

package hpu.edu.lzl.Servlet;



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ConfirmCodeSer extends HttpServlet {


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("imge/jpeg");        //验证码以图片的类型展示
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

int width= 44;  //验证码的长宽以及个数
int height = 20;
int count = 4;
String codes="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";
String codeValues = "";
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);

g.setColor(Color.blue);
g.drawRect(0, 0, width, height);
Random random = new Random();
int segment = width/count;  
for(int i=0;i<count;i++){
int red =random.nextInt(255)+1;
int blue = random.nextInt(255)+1;
int green = random.nextInt(255)+1;

Color color = new Color(red,blue,green);
g.setColor(color);
int size = random.nextInt(7)+8;
Font font = new Font("Default",Font.ITALIC,size);
g.setFont(font);
int index = random.nextInt(codes.length());
String s = codes.charAt(index)+"";
codeValues += s;
g.drawString(s, i*segment, height-4);
}
request.getSession().setAttribute("codeValues", codeValues);
// System.out.println(codeValues);
for(int i=0;i<5;i++){
int x=random.nextInt(width)+1;
int y=random.nextInt(height)+1;
g.drawRect(x, y, 1, 1);
}
g.dispose();
OutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.flush();
out.close();
}


}

第二步,在jsp页面上面,用javaScript写一个监听事件,用于调用Servlet。实现点击图片换下一个验证码的功能
在登录界面上写的:
<script type="text/javascript">
var index = 0;
function changeCode(){
var iamgeCode = document.getElementById("codeId");
iamgeCode.src = "ConfirmCodeSer?name="+index;
index++;
}
</script>
<b>  
验证码:
</b>
&nbsp;&nbsp;<input type="text" name="code" style="width:50px " />&nbsp;<img src="ConfirmCodeSer" οnclick="changeCode()" id="codeId" />
   <p></p>

第三步,在登录验证的Servlet里边,判断验证码是否正确。
最后再在登录判断验证上面判断一下就行了
String code = (String)request.getParameter("code");
List<String> info =new ArrayList<String>();
if(code==null||code.equals("")||!code.equals(codeValues)){
info.add("验证码不能为空");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值