JSP表单填写验证---JSP+JavaBean
来源:程序员人生 发布时间:2014-12-24 08:34:58 阅读次数:3278次
1、JavaBean
所谓JavaBean就是满足1定设计规则的Java类,需要满足的规则以下
(1)数据成员的属性为private
(2)每一个数据成员具有public属性的setXXX和getXXX方法
(3)有1个没有参数的public类型的构造函数
2、实例代码
package bean;
public class UserCheckBean{
private String name;
public UserCheckBean(){}
public UserCheckBean(String name){
this.name=name;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public boolean validate(){
return name.equals("cjc");
}
}
登录界面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF⑻">
<title>Insert title here</title>
<style>
td{font-size:15pt;font-color:red;}
</style>
</head>
<body>
<form action="loginchk.jsp" method="post">
<table>
<tr>
<td>username</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
验证JSP
<%@ page language="java" contentType="text/html; charset=ISO⑻859⑴"
pageEncoding="ISO⑻859⑴"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="bean.UserCheckBean"%>
<jsp:useBean id="user" scope="session" class="bean.UserCheckBean"/>
<jsp:setProperty name="user" property="name" value="*"/>
<%
String nm=request.getParameter("name");
user.setName(nm);
UserCheckBean uc=new UserCheckBean(nm);
if(uc.validate())
{
%>
<jsp:forward page="welcome.jsp"/>
<%
}
else{
out.println("The username does not exist!");
out.println("<a href="login.html">login again</a>");
}
%>
欢迎界面
<%@ page language="java" contentType="text/html; charset=ISO⑻859⑴"
pageEncoding="ISO⑻859⑴"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO⑻859⑴">
<title>welcome</title>
</head>
<body>
<jsp:useBean id="user" scope="session" class="bean.UserCheckBean"/>
Welcome <jsp:getProperty name="user" property="name"/>!
</body>
</html>
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠