JavaWeb学习笔记3——生成图片验证码,linux架构设计

这篇博客介绍了如何在JavaWeb中实现图片验证码的生成,包括随机字符串的创建、颜色处理以及在Servlet中绘制并输出验证码。此外,文章还提及了Linux架构设计的相关内容。同时,提供了HTML文件引用验证码的示例。
摘要由CSDN通过智能技术生成

for(int i=0;i<6;i++){

buffer.append(CHARS[random.nextInt(CHARS.length)]);

}

return buffer.toString();

}

//随机颜色

public static Color getRandomColor(){

return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));

}

//某颜色的反色,用于前景色

public static Color getReverseColor(Color c){

return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());

}

/**

  • Constructor of the object.

*/

public IdentityServlet() {

super();

}

/**

  • Destruction of the servlet.

*/

public void destroy() {

super.destroy(); // Just puts “destroy” string in log

// Put your code here

}

/**

  • The doGet method of the servlet.

  • 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 {

response.setContentType(“image/jpeg”);

String randomString=getRandomString(); //随机字符串

request.getSession(true).setAttribute(“randomString”, randomString);

int width=100;

int height=30;

Color color=getRandomColor();

Color reverse=getReverseColor(color); //反色,用于前景色

BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics2D g=bi.createGraphics();

g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));

g.setColor(color);

g.fillRect(0, 0, width, height);

g.setColor(reverse);

g.drawString(randomString, 18, 20); //绘制随机字符

for(int i=0, n=random.nextInt(100);i<n;i++){

//画最多100个噪音点

g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1); //噪音点在图片上随机选择位置产生

}

ServletOutputStream out=response.getOutputStream();

JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); //转成JPEG格式

encoder.encode(bi); //对图片进行编码

out.flush();

}

/**

  • The doPost method of the servlet.

  • 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(“text/html”);

PrintWriter out = response.getWriter();

out.println("");

out.println("")

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

;

out.println(" A Servlet");

out.println(" ");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}

/**

  • Initialization of the servlet.

  • @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

接下来我们建立一个HTML文件来引用这个图片验证码,创建方法是:在项目Iidentity上右键选择New—HTML(Advanced Templatets),在弹出框中设置名字为identity,然后Finish,下面是identity.html的代码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值