版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、项目结构如下:图1package com.checkcode.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Random;import java.awt.*;import java.awt.image.Buf
2、feredImage;import javax.imageio.ImageIO;import javax.servlet.http.HttpSession;public class AuthImage extends HttpServlet private static final String CONTENT_TYPE="text/html;charset=gb2312" private Font mFont=new Font("Times new Roman",Font.PLAIN,17);/Color getRandColor(int fc,int
3、 bc)Random random=new Random();if(fc>255) fc=255;if(bc>255) bc=255;int r=fc+random.nextInt(bc-fc);int g=fc+random.nextInt(bc-fc);int b=fc+random.nextInt(bc-fc);return new Color(r,g,b);/private String getRandChar()int rand=(int)Math.round(Math.random()*2);long itmp=0; char ctmp='u0000'
4、switch(rand) case 1: itmp=Math.round(Math.random()*25+65); ctmp=(char)itmp; return String.valueOf(ctmp); case 2: itmp=Math.round(Math.random()*25+97); ctmp=(char)itmp; return String.valueOf(ctmp); default: itmp=Math.round(Math.random()*9); ctmp=(char)itmp; return String.valueOf(ctmp); /* * Construct
5、or of the object. */public AuthImage() super();/* * Destruction of the servlet. <br> */public void destroy() super.destroy(); / Just puts "destroy" string in log/ Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOE
6、xception response.setHeader("pragma","No-cache");response.setHeader("Cache-Control", "No-cashe");response.setHeader("Expires","0");/response.setContentType("image/jpeg");int width=80;int height=18;BufferedImage image=new BufferedI
7、mage(width,height,BufferedImage.TYPE_INT_RGB);Graphics g=image.getGraphics();Random random=new Random();g.setColor(getRandColor(200,500);g.fillRect(1, 1, width-1, height-1);g.setColor(new Color(102,102,102);g.drawRect(0, 0, width-1, height-1);g.setFont(mFont);/g.setColor(getRandColor(160,200);for(in
8、t i=0;i<155;i+)int x=random.nextInt(width-1);int y=random.nextInt(height-1);int x1=random.nextInt(6)+1;int y1=random.nextInt(12)+1;g.drawLine(x, y, x+x1, y+y1);/for(int i=0;i<70;i+)int x=random.nextInt(width-1);int y=random.nextInt(height-1);int x1=random.nextInt(12)+1;int y1=random.nextInt(6)
9、+1;g.drawLine(x, y, x-x1, y-y1);/String sRand =""for(int i=0;i<4;i+)String tmp=getRandChar();sRand+=tmp;/g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110);g.drawString(String.valueOf(tmp), 15*i+16, 16);HttpSession session=request.getSession(true);
10、session.setAttribute("ValidateCode", sRand);g.dispose();ImageIO.write(image, "JPEG", response.getOutputStream();public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doGet(request,response);public void init() throws Servl
11、etException / Put your code herepackage com.checkcode.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CheckLogin extends HttpServlet
12、/* * Constructor of the object. */public CheckLogin() super();/* * Destruction of the servlet. <br> */public void destroy() super.destroy(); / Just puts "destroy" string in log/ Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws Servle
13、tException, IOException String validatecode1=(String)request.getSession().getAttribute("ValidateCode");/String validatecode2=request.getParameter("validatecode");/if(!validatecode1.equals(validatecode2)/request.setAttribute("message", "验证错误");elserequest.setAt
14、tribute("message", "验证正确");/request.getRequestDispatcher("/login.jsp").forward(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doGet(request,response); public void init() throws ServletE
15、xception / Put your code here配置文件:<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>AuthImage</servlet-name> <servlet-class>com.che
16、ckcode.servlet.AuthImage</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>CheckLogin</servlet-name&g
17、t; <servlet-class>com.checkcode.servlet.CheckLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>AuthImage</servlet-name> <url-pattern>/servlet/AuthImage</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Ch
18、eckLogin</servlet-name> <url-pattern>/servlet/CheckLogin</url-pattern> </servlet-mapping>登陆页面:<% page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme
19、()+":/"+request.getServerName()+":"+request.getServerPort()+path+"/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html> <head> <base href="<%=basePath%>"> <title>Login</title> </head>
20、; <body> <form name="logform" method="post" action="./servlet/CheckLogin"> <table align="center"> <tr> <td> 用户名:</td> <td> <input type="text" name="username" size="15"></td> </tr> <tr> <td> 密码:</td> <td>
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026四川九洲物业服务有限公司招聘物业案场经理3人模拟试卷附完整答案详解【名校卷】
- 2026年延安市宝塔区第二期就业见习报名(150人)备考题库(名师系列)附答案详解
- 2026年铜川市王益区招募大学生政府机关见习通知(20人)备考题库(原创题)附答案详解
- 水库除草服务方案范本
- 公司展厅布置方案范本
- 灯具建造方案范本及
- 2026四川宜宾市屏山县劳动人事争议仲裁院招聘就业见习人员2人备考题库及完整答案详解【考点梳理】
- 2026四川华丰科技股份有限公司招聘绩效专员岗位1人笔试题库及1套参考答案详解
- 2026年度中国农业科学院西部农业研究中心(科技援疆指挥部)高层次人才引进笔试题库附答案详解【满分必刷】
- 2026四川内江市隆昌市龙市镇招聘公益性岗位6人参考题库及参考答案详解【巩固】
- 国企贸易业务管理办法
- NBT11402-2023火力发电厂安全设施设计专篇编制导则
- 公司质量财务管理制度
- GB/T 3091-2025低压流体输送用焊接钢管
- 2025年招商局集团招聘【100人】高频重点提升(共500题)附带答案详解
- 5G-NR数字蜂窝移动通信网无线操作维护中心(OMC-R)测量报告技术要求
- JJF(陕) 112-2024 高频电刀分析仪校准规范
- QC/T 822-2024汽车用压力传感器
- 2024届新高考语文高中古诗文必背72篇 【原文+注音+翻译】
- 国家开放大学《管理信息系统》大作业参考答案
- NB-T25013-2013核电厂发电机组首次并网试验要求
评论
0/150
提交评论