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>