1、JavaBean
所谓JavaBean就是满足一定设计规则的Java类,需要满足的规则如下
(1)数据成员的属性为private
(2)每个数据成员拥有public属性的setXXX和getXXX方法
(3)有一个没有参数的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-8"> <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-8859-1" pageEncoding="ISO-8859-1"%> <!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-8859-1" pageEncoding="ISO-8859-1"%> <!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-8859-1"> <title>welcome</title> </head> <body> <jsp:useBean id="user" scope="session" class="bean.UserCheckBean"/> Welcome <jsp:getProperty name="user" property="name"/>! </body> </html>
时间: 2024-10-08 21:23:08