java--servlet重点讲解

生成随机验证码

public classidentfify extends HttpServelet{

public statcifinal char[] chars={'2','3','9','a','z'};//随机字符串

public staticRandom random=new Random();//随机数

public static StringgetRandomString()//获取6位随机数字符串

{

 StringBuffer buffer=newStringBuffer();//random.nextInt(5)返回从0-4

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

{

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

}

returnbuffer.toString();

}

 

 

public staticColor getRandomColor()//获取随机眼色

{

 return newColor(random.nextInt(255),random.next(255),random.netInt(255));

}

 

public staticColor getReverseColor(Color c)//返回某颜色的反色

{

  return newColor(255-c.getRed(),255-c.getGreen(),255-c.getBlue());//红 绿 蓝

}

 

public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws ServletException,IOException

{

response.setContentType("image/jpeg");//设置输出类型

StringrandomString=getRandomString();//调用方法获取随机字符串

request.getSession(true).setAttribute("randomString",randomString);

 

intwidth=100;int height=30;

Colorcolor=getRandomColor();

Colorreverse=getReverseColor();//反色,用于前景色

BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_REG);//创建一个不带透明色的图片

Graphics2D g=bi.createGraphics();//创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中

g.setFont(newFont(Font.SANS_SERIF,Font.Bold,16));//设置字体

g.setColor(color);

g.filRect(0,0,width,height);//起始坐标,大小,绘制了一个背景色

g.setColor(reverse);

g.drawString(randomString,18,20);//使用当前颜色,当前字体在18,20处的位置编写一个字符串

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

{

 g.drawRect(random.nextInt(width),random.nextInt(height),1,1);//在随机位置上画一个1,1的正方形

}

//转换成图片并输出

ServletOutPutStreamout=response.getOutPutStream();

JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);

encoder.encode(bi);

out.flush();

}

}

html代码:

<html>

<head>

<scripttype="text/javascript">

functionreload(){

document.getElementById('imga').src='yanzheng?date='+newDate().toString();

}

</script></head>

<body>

<imgsrc="yanzheng" id="imga"/> <button id="a"οnclick="reload()" >换一张</button>

</body>

</html>

 

 

扩展:

getSubimage方法是进行图片裁剪。

举例:

public staticvoid main(String[] args) {

try {

//从特定文件载入

BufferedImage bi= ImageIO.read(new File("c:\\test.jpg"));

bi.getSubimage(0,0, 10, 10);//前两个值是坐标位置X、Y,后两个是长和宽

} catch(IOException e) {

e.printStackTrace();

}

}

 

ImageIO.write(BufferedImageimage,String format,OutputStream out);方法可以很好的解决问题;

参数image表示获得的BufferedImage;

参数format表示图片的格式,比如“gif”等;

参数out表示输出流,如果要转成Byte数组,则输出流为ByteArrayOutputStream即可;

举例:

BufferedImageimage = ImageIO.read(new File("1.gif"));

      ByteArrayOutputStream out = newByteArrayOutputStream();

      boolean flag = ImageIO.write(image,"gif", out);

      byte[] b = out.toByteArray();

将字符串转化为日期

dateformatformat = new SimpleDateFormat("yyyy-mm-dd");

date b=format.parse(request.getParameter("time"));

 

J2SE 提供的最后一个批注是 @SuppressWarnings。该批注的作用是给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默。

Javaweb文件结构

 

 

 

 

注解preconstruct

Httprensponse状态码

 

Page属性


Include属性


Plugin属性

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值