我遇到了一个相对简单的问题。我正在尝试做一个简单的程序,您可以使用该会话登录和注销。 会话已创建,但我总是转发到面板servlet,然后转到NoCorrectSession页面。好像在登录页面上,服务器看到isActive属性,然后在面板页面上没有。
//class Login extends HttpServlet
private RequestDispatcher pageLogin, pagePanel, pageError; //defined in init()
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String name = request.getParameter("name");
String isBeingRequested = request.getParameter("isBeingRequested");
if (session.getAttribute("isActive") != null) {
//user has been logged in before, redirect him
pagePanel.forward(request, response);
} else if (isBeingRequested != null) {
//user has entered data into the login page and submitted it
if (name.length() == 0) {
//user has not stated his name
pageError.forward(request, response);
} else {
//otherwise access is granted and account created
session.setAttribute("isActive", "yes"); //setting session to active
pagePanel.forward(request, response);
}
} else {
//neither of these? user just entered the login screen
pageLogin.forward(request, response);
}
}
//class Panel extends HttpServlet {
private RequestDispatcher pageNoCorrectSession;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
//session has expired or never was started
if (session.getAttribute("isActive") == null) {
pageNoCorrectSession.forward(request, response);
//session valid
} else {
//logged in - do stuff
}
答案 0 :(得分:0)
在挖掘项目之后,事实证明我在这些servlet中没有犯错,但在HTML代码中导致将表单直接提交给面板servlet(而不是在过程中创建帐户对象)。我知道这很愚蠢,但这是一个很好的教训。期待意外:P