本文目录一览:
jsp中显示验证码的代码怎么写?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/*生成验证码图片
*/
public class MakeCertPic {
//验证码图片中可以出现的字符集,可以根据需要修改
private char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'
};
/* 功能:生成彩色验证码图片
参数wedth为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
public String getCertPic(int width,int height,OutputStream os){
if(width=0)
width=60;
if(height=0)
height=20;
BufferedImage image= new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//设定背景颜色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0,0,width,height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//随机产生的验证码
String strEnsure = "";
//4代表4为验证码,如果要产生更多位的验证码,则加大数值
for(int i = 0;i4;++i){
strEnsure += mapTable[(int) (mapTable.length*Math.random())];
}
//将认证码显示到图像中,如果要生成更多位的验证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str, 20, 15);
str = strEnsure.substring(2,3);
g.drawString(str, 35, 18);
str = strEnsure.substring(3,4);
g.drawString(str, 45, 15);
//随机产生15个干扰点
Random rand = new Random();
for(int i=0; i10; i++){
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//释放图形上下文
g.dispose();
try{
//输出图形到页面
ImageIO.write(image, "JPEG", os);
}catch (IOException e){
return "";
}
return strEnsure;
}
}
makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:
%@page contentType="image/jpeg" %%@page language="java" pageEncoding="utf-8"%jsp:useBean id="image" scope="page" class="securityCode.pic.MakeCertPic"/%
String str = image.getCertPic(0,0,response.getOutputStream());
//将验证码存入session中
session.setAttribute("certCode",str);
%
下边是登录页面:
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
title验证码测试登录页面/title
script type="text/javascript"
function changeimg()
{
var myimg = document.getElementById("code");
now = new Date();
myimg.src="makeCertPic.jsp?code="+now.getTime();
}
/script
/head
body
center
form action="loginCheck.jsp" method="post" /
用户名:input type="text" name="username" /br
密nbsp;nbsp;码:input type="password" name="password"/br
nbsp;验证码:input type="text" name="certCode"/
img id="code" src="makeCertPic.jsp"a href="javascript:changeimg()"看不清,换一张 /abr
input type="submit" value="登录"/
/form
/center
/body
/html
JSP中点击新闻标题显示显示新闻内容要怎么?急~~跪求..开发高手们
你的新闻标题和内容应该在数据库中,你应该写一个类,里面有根据新闻ID或者新闻标题获取这条新闻的方法,取出来,放到request中,然后再shownews.jsp文件中拿出新闻的标题和内容.
JSP验证码
package com.shown.netshop.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ValidateCodeActionServlet extends HttpServlet {
private static int width = 80;
private static int heigth = 25;
private static int font_size = 20;
private static Random random = new Random();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
String content = this.getRandomStr();
request.getSession().setAttribute("correctCode", content);
ServletOutputStream out = response.getOutputStream();
//构建缓冲图像
BufferedImage bi = new BufferedImage(80,25,BufferedImage.TYPE_INT_BGR);
//设置绘图上下文
Graphics2D g = bi.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, heigth);
//设置字体
g.setFont(new Font("宋体",Font.BOLD,font_size));
g.setColor(Color.black);
for(int i = 0 ; i 30 ; i ++){
int x1 = random.nextInt(80);
int y1 = random.nextInt(25);
int x2 = random.nextInt(80);
int y2 = random.nextInt(25);
g.setColor(this.getRandomColor());
g.drawLine(x1, y1, x2, y2);
}
for(int i = 0 ; i 500 ; i ++){
g.setColor(this.getRandomColor());
g.drawOval(random.nextInt(80), random.nextInt(25), 0, 0);
}
g.drawString(content, 10, 18);//----------------------------------------
JPEGImageEncoder coder = JPEGCodec.createJPEGEncoder(out);
coder.encode(bi);
out.flush();
out.close();
}
/**
* get four numbers random
* @return String
*/
private String getRandomStr() {
String str = "";
for(int i = 0 ; i 4 ; i ++){
int temp = random.nextInt(10);
str += temp;
}
return str;
}
/**
* get a random color
* @return Color
*/
private Color getRandomColor(){
int R = random.nextInt(255);
int G = random.nextInt(255);
int B = random.nextInt(255);
Color color = new Color(R,G,B);
return color;
}
}
求JSP编写验证码程序的代码注释
你的实在看不太清,见谅...我给你一个,有注释的!这个功能通常一共3个页面:index.jsp是用来登录用的,在其中显示验证码,即img src="image.jsp"/
image.jsp是用来生成验证码的,有注释,很详细,如再不行,你复制到百度就有详解了。
result.jsp 是用来判断输入是否正确的。
相信你一定能看明白...别忘了采纳哦,谢谢。。。
【1.index.jsp】
%@ page language="java" import="java.util.*" pageEncoding="GBK"%
htmlbody
form method=post action="result.jsp"
input type=text name=input maxlength=4
img border=0 src="image.jsp"
input type="submit" value="submit"
/form/body/html
【2.image.jsp】
%@ page contentType="image/JPEG"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="GBK"%
%!Color getRandColor(int fc, int 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);
}%
%
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 将认证码存入SESSION
session.setAttribute("code", sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%
【3.result.jsp】
%@ page language="java" import="java.util.*" pageEncoding="GBK"%
htmlbody
%
String input=request.getParameter("input");
String code=(String)session.getAttribute("code");
if(input.equals(code)){
out.println("验证成功!");
}else{
out.println("验证失败!");
}
%
bodyhtml
jsp页面该如何刷新验证码
(1)jsp代码:
img id = "img_authcode" src="${ctx}/account/authcode" /a href="javascript:;" onclick="javascript:document.getElementById('img_authcode').setAttribute('src', '${ctx}/account/authcode?' + Math.random())"换一换/a
(2)java代码(该代码为我自己框架代码,跟servlet写法不一样的我都给你注释了):
public View authcode() throws IOException {
HttpServletResponse response = PuffContext.getResponse();//获取response
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String authCode = AuthCodeUtil.getRandom(4); //获取验证码,代码在下面(3)
System.out.println("生成随机码:" + authCode);
PuffContext.getSession().setAttribute("session_authcode", authCode);//把该验证码存储在session
ServletOutputStream output = response.getOutputStream();
AuthCodeUtil.draw(output, authCode);
output.flush();
output.close();
return ViewFactory.nullView();//返回null
}
(3)///////////////////////////下面为生成验证码类////////////////////////////////////
public class AuthCodeUtil {
private final static Random random = new Random();
// 随机字体样式
private final static int[] fontStyle = { Font.HANGING_BASELINE, Font.ITALIC, Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_NO_LIMIT_CONTEXT,
Font.LAYOUT_NO_START_CONTEXT, Font.LAYOUT_RIGHT_TO_LEFT };
/**
* 画随机码图
*
* @param out
* @param width
* @param height
* @throws IOException
*/
public static void draw(OutputStream out, String value) throws IOException {
int width = 80, height = 30;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.drawRect(1, 1, width - 2, height - 2);
for (int i = 0; i 10; i++) {
g.setColor(randColor(150, 250));
g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
}
int n = (int) (Math.random() * 6);
Font mFont = new Font("Arial", fontStyle[n], 23);
g.setFont(mFont);
g.setColor(randColor(10, 240));
g.drawString(value, 10, 21);// 随机数,水平距离,垂直距离
ImageIO.write(bi, "png", out);
}
private static Color randColor(int fc, int bc) {// 给定范围获得随机颜色
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);
}
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("d:\\aa.png");
draw(out, getRandom(4));
}
public static String getRandom(int size) {// 随机字符串
char[] c = { '1', '3', '5', '6', '7', '8', '9' };
StringBuffer sb = new StringBuffer();
for (int i = 0; i size; i++)
sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
}
}