一,密码的复杂度
public static int getString(String str){
int isNumber=0;
int isLetter=0;
int isOther =0;
int len = str.length();
if (len<6) return 1;
for (int i=0;i<len;i++){
char ch = str.charAt(i);
if (ch>='0' && ch<='9')
isNumber=1;
else if (ch>='A' && ch<='Z' || ch>='a' && ch<='z')
isLetter=1;
else
isOther=1;
}
int sum = isNumber+isLetter+isOther;
return sum;
}
二,验证码
//产生四个随机数
public char[] getRandoms()
{
String sourceString = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijkmnpqrstuvwxyz";
Random random = new Random();
char[] chs = new char[NUM];
for (int i = 0; i < NUM; i++) {
chs[i] = sourceString.charAt(random.nextInt(56));
}
return chs;
}
//产生验证码
public void doImage(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
char[] chs = getRandoms();
String codeString = new String(chs);
request.getSession().setAttribute("codeString", codeString);
BufferedImage bImage = new BufferedImage(80,25,BufferedImage.TYPE_INT_RGB);
Graphics graphics = bImage.getGraphics();
graphics.setColor(Color.gray);
graphics.fillRect(0, 0, 80, 25);
graphics.setColor(Color.red);
graphics.setFont(new Font("宋体",Font.BOLD,28));
graphics.drawString(chs[0]+"", 0, 21);
graphics.drawString(chs[1]+"", 20, 18);
graphics.drawString(chs[2]+"", 45, 19);
graphics.drawString(chs[3]+"", 60, 24);
//也可以划线
//四根线
Random r = new Random();
g.setColor(Color.GREEN);
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
g.drawLine(r.nextInt(80), r.nextInt(20),r.nextInt(80), r.nextInt(20));
ImageIO.write(bImage, "jpeg", response.getOutputStream());
}
·在jsp页面中的用法
验证码:<input type="text" id="code" name="codename" size=3/>
<img src="${pageContext.request.contextPath}/login.do?method=doimage" id="codeImg"/>
三,购物车的实现
javascript:
//只能输入数字
function check(e)
{
var e = e?e:window.event;
var c = e.charCode || e.keyCode ;
if(!(c >=48 && c<=57 || c == 8 || c==37 || c==39))
{
c = 0 ;
return false;
}
}
function checkMoney(e)
{
var e = e?e:window.event;
var c = e.charCode || e.keyCode ;
if(!(c >=48 && c<=57 || c == 8 || c==37 || c==39 || c == 46))
{
c = 0 ;
return false;
}
}