想通过将用户名和密码封装在javaBean里,然后通过javaBean实现验证用户登陆,程序代码如下:
login.jsp
<%@page contentType="text/html;charset=gb2312" language="java" %>
<html>
<head><title>use JavaBean</title>
</head>
<body>
<div align="center">用户登陆
<form name="login" action="checkLogin.jsp" method="get">
<table border="0">
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName" >
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="userPassword" >
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="提交">
</td>
<td>
<input type="reset" name="reset" value="清除">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
checkLogin.jsp:
<%@page contentType="text/html;charset=gb2312" language="java" %>
<jsp:useBean id="t" class="login.CheckLogin" scope="session" />
<%
String username=(String)request.getParameter("userName");
String password=(String)request.getParameter("userPassword");
if(t.check(username,password)==true)
{
out.println("Login Success!");
}
else
{
out.println("UserName or Password Error!");
}
%>
CheckLogin.java:
package login;
public class CheckLogin
{
String userName="abc";
String password="def";
public boolean check(String username,String password)
{
if((username==this.userName)&&(password==this.password))
return true;
else
return false;
}
}