最近写了一个用户验证密码的程序,其功能如下:
输入用户名和密码,然后提交,只有在密码位数大于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呢?

解决方案 »

  1.   

    应该是 Java代码在html里面没有起作用的导致的。
      

  2.   

    html是静态页面。jsp:java server page,在服务端运行并最终生成html格式或其他格式的页面。
      

  3.   

    那我还需要写html吗?都写成jsp呗?我不需要什么静态的,我都要动态网页不行吗?呵呵 
      

  4.   

    此程序没有任何问题<%@ 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>
    就是加了点符号
      

  5.   

    行啊,有jsp了,jsp编译运行后生成html发送给客户端,所以你不需要再另写html了,你jsp里面不就是java+html么
      

  6.   

    那你们有没有试过单独运行jsp和html
      jsp是要在一定环境下才能打开运行出来的,,比如在tomcat服务才能打开界面
      而html什么都不要用,把后缀改成html,用记事本打开随便打写东西..双击都能打开看到东西.