bd.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<% //接受表單提交的字符串,
/*
數據只能為a-z,A-Z,0-9的字符的正則表達式的例子.
正則表達式在做程序登陸的過程非常重要。不然用´ or ´ 等語句就可以很容易繞過您的程度判斷;
作者:高頌 QQ:932246 一個簡單的例子但是很有用,希望起到拋磚引玉的作用。
*/
String temp ="我是非法的字符";
String name = request.getParameter("name"); //應收金額
String pass = request.getParameter("pass");
if (name==null){
name=temp;
}
if (pass==null){
pass=temp;
}
%>
<%!private 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>
<title>信息輸入表單</title>
<body>
<form action = "bd.jsp" method = "get" >
<div align="center"><font color="#FF0000">數據只能為a-z,A-Z,0-9的字符的正則表達式的例子</font>
<table border = "1">
<tr>
<td bgcolor = "yellow">用戶</td>
<td><input name = "name" type = "text" id="name"></td>
</tr>
<tr>
<td width="293" bgcolor = "yellow">密碼</td>
<td width="240"> <input name = "pass" type = "text" id="input"></td>
</tr>
<tr>
<td colspan = "2" align = "center"><input name="submit" type = "submit" value = "確定">
<input name="reset" type = "reset" value = "取消">
</td>
</tr>
<tr>
<td colspan = "2" align = "center">消息提示
<%
//判斷:
if(!regex(name)){
out.print("用戶名只能是a-z,A-Z,0-9的字符");
return;//跳出程序只行
}
out.print("恭喜您的數據錄入合法了。<br>"+"您錄入的名字是:"+name+"<br>您錄入的密碼是:"+pass);
%>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>