| |
防止用户非法登录又一法 由于用户名及密码都是由a-z,a-z,0-9这几字符组成, 我参考了一下正则表达式的算法,故有已下想法: 把用户输入的用户名及密码判断一下,看是否是这几个字符组成, 如果是,进行登录验证 否则提示有非法字符 代码如下: //login.jsp <%@ page contenttype="text/html; charset=gbk" language="java" import="java.io.*" errorpage="error.jsp" %> <%! boolean regex(string str){ java.util.regex.pattern p=null; //正则表达式 java.util.regex.matcher m=null; //操作的字符串 boolean value=true; try{ p = java.util.regex.pattern.compile("[^0-9a-za-z]"); m = p.matcher(str); if(m.find()) { value=false; } }catch(exception e){} return value; } %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=gbk"> <title>无标题文档</title> </head>
<body> <table width="100%" height="100%" border="0"> <tr> <td align="center" valign="middle"> <% string action=request.getparameter("action");
if (action!=null&&action.equals("login")){ string username=request.getparameter("username"); string password=request.getparameter("password"); if(!regex(username)||!regex(password)){ out.println("<script language=´javascript´>"); out.println("alert(´1.用户名只能是a-z,a-z,0-9的字符//n2.密码只能是a-z,a-z,0-9的字符//n3.不允许空格´)"); out.println("history.go(-1)"); out.println("</script>"); out.print("用户名只能是a-z,a-z,0-9的字符"); return;//跳出程序 } out.println(action);//登录验证 out.println("用户名是"+username+"<br>"); out.println("密码"+password); } else{ %> <form name="form1" method="post" action="#"> <table width="70%" border="0"> <tr> <td>username</td> <td><input name="username" type="text" id="username"> </td> </tr> <tr> <td>password</td> <td><input name="password" type="text" id="password"></td> </tr> <tr> <td> </td> <td><input name="action" type="hidden" id="action" value="login"> 用户名及密码只能是a-z,a-z,0-9的字符</td> </tr> <tr> <td><input type="submit" name="submit" value=" o k "></td> <td><input type="reset" name="submit2" value="cancle"></td> </tr> </table> </form><% } %></td> </tr> </table> </body> </html>
|
|