模拟用户登陆注册的小代码,并无调用数据库,使用APPLICATION实现
用户USERBEAN代码
package bean;
public class UserBean {
private String username;
private String password;
private String type;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
登陆界面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<form name="SubForm" method="post" action="sub.jsp">
<body>
<div align="center"><h2>用户登陆</h2>
<table>
<tr><td>
用户名<input type="text" name="username"></td>
<td>密码<input type="password" name="password"></td>
</tr><br/>
<tr><td>
管理员<input type="radio" name="type" value="s">普通用户<input type="radio" name="type" value="c" checked>
</td>
</tr>
</table>
</div><br/><br/>
<div align="center"><input type="submit" value="提交">
<input type="reset" value="重置"> <a href="reg.jsp">注册</a></div>
</body>
</form>
</html>
注册界面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<body>
<div align="center"><h2>注册页面</h2>
<form name="RegForm" method="post" action="reguser.jsp">
<table>
<tr><td>
用户名<input type="text" name="username"></td><td>
密码<input type="password" name="password"></td>
</tr>
<tr><td>
管理员<input type="radio" name="type" value="s">普通用户<input type="radio" name="type" value="c" checked>
</td></tr>
</table>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</div>
</body>
</html>
注册判断JSP
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:directive.page import="bean.UserBean;"/>
<%
ArrayList userlist=(ArrayList)application.getAttribute("userlist");
String username;
String password;
String type;
if(userlist==null||userlist.size()==0){
userlist=new ArrayList();
}
UserBean user=new UserBean();
username=request.getParameter("username");
password=request.getParameter("password");
type=request.getParameter("type");
user.setUsername(username);
user.setPassword(password);
user.setType(type);
userlist.add(user);
application.setAttribute("userlist",userlist);
%>
<a href="Index.jsp">注册成功 点击跳转</a>
提交判断JSP代码段
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:directive.page import="bean.UserBean;" />
<%
String username=null;
String password=null;;
String type=null;;
String url=null;
ArrayList userlist = (ArrayList) application
.getAttribute("userlist");
if (userlist == null || userlist.size() == 0) {
url = "登陆错误!";
%>
<%=url%>
<%
return;
}
username = request.getParameter("username");
password = request.getParameter("password");
type = request.getParameter("type");
for (int i = 0; i < userlist.size(); i++) {
UserBean user = (UserBean) userlist.get(i);
if(username.equals(user.getUsername())&&password.equals(user.getPassword())){
if(type.equals("s")){
url="管理面界面";
}
else{
url="普通用户界面";
}
}
else{
url="密码错误!";
}
}
%>
<%=url%>