这些日子很是受伤,也许我以前太不专心去做一些事了,做事太多,事事无成。放假回家,看到那成绩,哇,哭死一片。
事实更证明我就是一个悲剧:浩忍为悲剧而生。XX对我说,做事就一心去做,别做了一半又不做了。所以这些无聊的时间中,我要好好学习JAVA。省得又被一群出国的,考研的,修电脑,送外卖的打击一翻。哥胜是无语加无泪。
这几主要学习是JAVA WEB开发(自己方向);ASP CMS 二次开发(交给公司的);PHP WAP开发(写给朋友的,写好了去本部包吃包什么的。两人去也包,嘿嘿)今天第一课,打代码打了两天多,虽然以前学了很多JACA,JSP很少认真去写过。这几天都不出去做事,被老爸老妈骂得要上吊。验证码,拿着书打的代码。代码不全,好多都自己想点办法搞定,当然不难。我是入门者嘛,不然就去搞死作者。早知道去借光盘来,省得打代码。好,直接贴代码吧。方便以后要的同学。代码分四个文件,WEB.xml 是配置文件,PictureCheckCode.java 是主要验证生成处理文件,index.jsp 是显示 验证码的 文件,deal.jsp 是判断验证码是否正确的文件。现在直接贴了哈
PictureCheckCode.java
package com.joyleft;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
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;
import javax.servlet.http.HttpSession;
public class PictureCheckCode extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public PictureCheckCode() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
super.init();
// Put your code here
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control","No-cache");
response.setDateHeader("Expries", 0);
response.setContentType("image/jpeg");
int width=86;
int height=22;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
Graphics2D g2d=(Graphics2D)g;
Random random=new Random();
Font mFont=new Font("黑体",Font.BOLD,16);
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(mFont);
g.setColor(getRandColor(180,200));
for(int i=0;i<100;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;
BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
Line2D line=new Line2D.Double(x,y,x+x1,y+y1);
g2d.setStroke(bs);
g2d.draw(line);
}
String sRand="";
int itmp=0;
for(int i=0;i<4;i++){
String ctmp;
switch(random.nextInt(4)){
case 1:
itmp=random.nextInt(26)+25;
ctmp=String.valueOf((char)itmp);
break;
case 2:
String[] rBase={"0","1","2","3","4","5","6","7","8","9","a","b","c","f","d","e","f"};
int r1=random.nextInt(3)+11;
String str_r1=rBase[r1];
int r2;
if(r1==13)
r2=random.nextInt(7);
else
r2=random.nextInt(16);
String str_r2=rBase[r2];
int r3=random.nextInt(6)+10;
String str_r3=rBase[r3];
int r4;
if(r3==10)
r4=random.nextInt(15)+1;
else if(r3==15)
r4=random.nextInt(15);
else
r4=random.nextInt(16);
String str_r4=rBase[r4];
byte[] bytes=new byte[2];
String str_r12=str_r1+str_r2;
int tempLow=Integer.parseInt(str_r12,16);
bytes[0]=(byte)tempLow;
String str_r34=str_r3+str_r4;
int tempHigh=Integer.parseInt(str_r34,16);
bytes[1]=(byte)tempHigh;
ctmp=new String(bytes);
break;
default:
itmp=random.nextInt(10)+48;
ctmp=String.valueOf((char)itmp);
break;
}
sRand+=ctmp;
Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110));
g.setColor(color);
Graphics2D g2d_word=(Graphics2D)g;
AffineTransform trans=new AffineTransform();
trans.rotate(random.nextInt(45)*3.14/180,15*i+8,7);
float scaleSize =random.nextFloat()+0.8f;
if(scaleSize>1f)scaleSize=1f;
trans.scale(scaleSize, scaleSize);
g2d_word.setTransform(trans);
g.drawString(ctmp, 15*i+18, 14);
}
HttpSession session= request.getSession(true);
session.setAttribute("randCheckCode", sRand);
g.dispose();
ImageIO.write(image, "JPEG",response.getOutputStream());
}
public Color getRandColor(int s,int e){
Random random=new Random();
if(s>255) s=255;
if(e>255) e=255;
int r=s+random.nextInt(e-s);
int g=s+random.nextInt(e-s);
int b=s+random.nextInt(e-s);
return new Color(r,g,b);
}
}
Index.jsp
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
我不会做哦<br>
<form action="deal.jsp" method="get">
<input name="checkCode" type="text" id="checkCode" title="验证码区分大小写" size=8 maxlength="5" />
<img src="PictureCheckCode" id="createCheckCode"/><a href="#" onClick="myReload()">看不清?换一个</a><br/>
<input type="submit" name="submit" value="提交">
</form>
<script language="javascript">
function myReload()
{
document.getElementById("createCheckCode").src=document.getElementById("createCheckCode").src+"?nocache="+new Date().getTime();
}
</script>
</body>
</html>
deal.jsp
<%
String checkCode=request.getParameter("checkCode");
if("".equals(checkCode)||checkCode==null)
{
out.println("<script>alert('请输入验证码!');window.location.href='index.jsp';</script>");
}else{
if(!checkCode.equals(session.getAttribute("randCheckCode"))){
out.println("<script>alert('您输入的验证码不正确!');history.back(-1);</script>");
}
}
%>
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>PictureCheckCode</servlet-name>
<servlet-class>com.joyleft.PictureCheckCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PictureCheckCode</servlet-name>
<url-pattern>/PictureCheckCode</url-pattern>
</servlet-mapping>
</web-app>
放一个效果图吧!在打代码的过程中还是出现了很多问题,WEB.XML 配置的时候也出现问题了,找了好些时间才搞定,如果你在代码设置了断点,运行后会进入DEBUG中去。所以注意下,我也是新手,高手手下留情。
最新评论
朋友 交换链接吗
BudgetVM优惠码 2018-04-15
我也要评论,东华理工
Erchoc 2017-07-04
很强
Erchoc 2017-07-04
你是老师啊?
苟保罗 2016-07-28
写的这都是什么语言啊,看不懂呢
苟保罗 2016-07-28
包子还是一个有为青年。
纯野 2016-03-01
关于旅行,总有许多幸福和迷恋。初秋浪漫而至,期待自己的下一站快乐。
网上购买书架 2013-07-06
楼主你好,我最近也在研究安卓无线点餐,不知您的源码能否分享一下,提供一些思路,谢谢:)
jack 2013-05-22
嗯嗯,原来我以前的姿势都不对的啊
九道食品 2012-07-04
对不起,我也没搞清楚,是想下载别人的模板,都是在网上看到的,人家的“空间或者服务器主机,的文件地址咱们也不知道呀,怎么填写读入目录呀。 这样的话,根本是下不了别人网站上的模板吧。 请教详细的下载方法。最好是人家在线的网站
千刀乐 2012-03-27