菜鸟上路-JSP+JavaBean小程序入门
设计3个页面:
注册页面index.jsp
验证输入的信息页面check.jsp
成功显示页面success.jsp
一个Bean:
注册类Register.java(放在src目录下的TestBean包里)
index.jsp主要代码
<%@page import="Testbean.Register"%>
</head>
<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>">
<br>
年龄:<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>">
<br>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>
</head>
<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>">
<br>
年龄:<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>">
<br>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>
check.jsp主要代码
</head>
<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
<body>
<%
if(reg.isValidate()){
%>
<jsp:forward page="success.jsp"/>
<% }else{%>
<jsp:forward page="index.jsp"/>
<%} %>
</body>
<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
<body>
<%
if(reg.isValidate()){
%>
<jsp:forward page="success.jsp"/>
<% }else{%>
<jsp:forward page="index.jsp"/>
<%} %>
</body>
success.jsp
<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
<jsp:setProperty name="reg" property="*"/>
<jsp:setProperty name="reg" property="*"/>
<body>
login success!!!<br>
用户名: <jsp:getProperty name="reg" property="name"/>
</body>
login success!!!<br>
用户名: <jsp:getProperty name="reg" property="name"/>
</body>
Register.java主要代码
View Code
1 package Testbean;
3 public class Register {
4 private String name;
5 private String age;
6 private String error;
7 //注册方法
9 public Register()
10 {
11 this.name="";
12 this.age="";
13 this.error="";
14 }
16 //注册数据验证
17 public boolean isValidate()
18 {
19 boolean flag=true;
20 if(!this.name.matches("\\w{6,15}"))
21 {
22 flag=false;
23 this.name="";
24 this.error="用户名必须为6到15位字母或数字!";
25 }
26 if(!this.age.matches("\\d+"))
27 {
28 flag=false;
29 this.age="";
30 this.error="年龄只能为数字";
31 }
32 return flag;
33 }
34 public String getAge()
35 {
36 return age;
37 }
38 public void setAge(String age)
39 {
40 this.age=age;
41 }
42 public String getName()
44 {
45 return name;
46 }
47 public void setName(String name)
48 {
49 this.name=name;
50 }
51 }
3 public class Register {
4 private String name;
5 private String age;
6 private String error;
7 //注册方法
9 public Register()
10 {
11 this.name="";
12 this.age="";
13 this.error="";
14 }
16 //注册数据验证
17 public boolean isValidate()
18 {
19 boolean flag=true;
20 if(!this.name.matches("\\w{6,15}"))
21 {
22 flag=false;
23 this.name="";
24 this.error="用户名必须为6到15位字母或数字!";
25 }
26 if(!this.age.matches("\\d+"))
27 {
28 flag=false;
29 this.age="";
30 this.error="年龄只能为数字";
31 }
32 return flag;
33 }
34 public String getAge()
35 {
36 return age;
37 }
38 public void setAge(String age)
39 {
40 this.age=age;
41 }
42 public String getName()
44 {
45 return name;
46 }
47 public void setName(String name)
48 {
49 this.name=name;
50 }
51 }
涉及知识点:
1.<%@page import="Testbean.Register"%>
2.<jsp:useBean id="reg" scope="request" class="Testbean.Register"/>
3. <jsp:getProperty name="reg" property="name"/>