首先创建一个这样的类.. 设置用户 bean 属性此示例为简单起见假设一个用户
public class Users implements Serializable {
private String user;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
然后像这样登录检查
String userName = request.getRequest().getParameter("txtid");
String password = request.getRequest().getParameter("txtPassword");
Vector params = new Vector();
params.add(null);//return result from procedure
params.add(Name);
params.add(password);
Users mainUser = new Users ();
Vector tempResults = callingDaoProcedure.executeGetPasswordogin(params);//finds the password in DB
if (!tempResults.isEmpty()) {
mainUser = (MainUsers) tempResults.get(0);
if (mainUser.getPassword().trim().equals(password.trim())) {
HttpSession session = request.getSession(false);
if (request.getSession().getAttribute("userLogin") == null) {
// Session not created yet. So we do it now.
session = request.getSession();
requestgetRequest().setAttribute("status", "NO_ERRORS");
request.getSession().setAttribute("userLogin", mainUser);
response.sendRedirect("home.jsp");
// I redirect them to their profile home pages
}
else {
// Session is already created.
response.sendRedirect("home.jsp");
// So i again redirect them to their home page
}
}
else
{
request.getRequest().setAttribute("status", "WRONG_PASSWORD");
System.out.println("wrong password");
}
}
上面的代码不是没有错误的,但是由于您提供了缺少的代码,因此这与我遇到的情况一样接近,您可以像这样在每个 jsp 上访问此变量
<input type="hidden" name="user" id="user" value="${sessionScope['userLogin'].user}">
我希望它有所帮助,最重要的是你从中学到了一些东西