最近写了一个用户验证密码的程序,其功能如下:
输入用户名和密码,然后提交,只有在密码位数大于8位切有数字的时候,才输出Thank you Your password meets the security policy 否则输出Sorry Your password does not meet the security policy 可是我的不知道怎么了,两个竟然同时输出了!各位大虾帮帮忙啊!谢啦!
login.html文件如下:
<html>
<head><title>A Siple Login Page</title></head>
<body>
<p>Please Login:
<form action=login.jsp method=post>
<p>User Name:<input type=text name=usernameField>
<p>Password:<input type=text name=passwordField>
<p><input type=SUBMIT value=Submit>
</form>
</body>
</html>
login.jsp文件如下:
<html>
<head>
<title>A Simple Jsp That Verifies Password Policy</title>
</head>
<body>
<%! final static int MIN_PSWD_LEN=8;
static boolean verifyPasswordLength(String password){
if(password.length()<MIN_PSWD_LEN)return false;
return true;
}
static boolean verifyPasswordHasDigit(String password){
for(int i=0;i<password.length();i++)
if(Character.isDigit(password.charAt(i))) return true;
return false;
}
static boolean verifyPasswordPolicy(String password){
if(verifyPasswordLength(password)&&verifyPasswordHasDigit(password))
return true;
else
return false;
}
%>
<%String password=request.getParameter(passwordField);
if(verifyPasswordPolicy(password)){%>
<p>Thank you
<p>Your password meets the security policy
<%}else{%>
<p>Sorry
<p>Your password does not meet the security policy
<p><a href="login.html">Please Try Again</a>
<%}%>
</body>
</html>
在众多好心人的帮助和启发下,我无意中改了一下文件类型,将.html改成.jsp , 就好用了!!我想问一下,这个和我的编译环境有关吗?我用的是Eclipse3.2--jdk1.5--jre1.5 。 还是另有其因?如果这样可以的话,为什么不把所有的html都换成jsp呢?
------解决方案--------------------
此程序没有任何问题
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>A Simple Jsp That Verifies Password Policy</title>
</head>
<body>
<%! final static int MIN_PSWD_LEN=8;
static boolean verifyPasswordLength(String password){
if(password.length()<MIN_PSWD_LEN)return false;
return true;
}
static boolean verifyPasswordHasDigit(String password){
for(int i=0;i<password.length();i++)
if(Character.isDigit(password.charAt(i))) return true;
return false;
}
static boolean verifyPasswordPolicy(String password){
if(verifyPasswordLength(password)&&verifyPasswordHasDigit(password))
return true;
else
return false;
}
%>
<%
String password=request.getParameter("passwordField");
if(verifyPasswordPolicy(password)){
out.print(password);
%>
<p>Thank you
<p>Your password meets the security policy)
<%
}else{
%>
<p>Sorry
<p>Your password does not meet the security policy
<p><a href="login.html">Please Try Again</a>
<%
}
%>
</body>
</html>
就是加了点符号
------解决方案--------------------