简体   繁体   English

使用Java会话进行登录/注销

[英]Using Java sessions for login/logout

Having trouble getting session to work in my Java application. 无法使会话在我的Java应用程序中正常工作。

My login.jsp page calls this LoginAction page. 我的login.jsp页面称为该LoginAction页面。

package struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import struts.form.LoginForm;

public class LoginAction extends org.apache.struts.action.Action {
private final static String SUCCESS = "success";
private final static String FAILURE = "failure";

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginForm lf = (LoginForm) form;
        HttpSession session = request.getSession(true);
        if (lf.getUsername().equals(lf.getPassword())) {

            session.setAttribute("Username", lf.getUsername());
            System.out.println(session.getAttribute("Username"));
            return mapping.findForward(SUCCESS);

        } else {
            return mapping.findForward(FAILURE);
        }
    }
}

Corresponding LoginForm page 对应的LoginForm页面

package struts.form;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm{

    private String username;
    private String password;

    public LoginForm() {
        super();
    }
    private static final long serialVersionUID = 104092268304152302L;

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;   
}

    public String getPassword() {
        return password;
        }

        public void setPassword(String password) {
        this.password = password;   
    }
}

success.jsp, the page shown when logged in success.jsp,登录后显示的页面

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
    language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>success</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<H1>Hello: <% session.getAttribute("Username"); %></H1>

 <html:form action="/LogoutAction" >
        <html:submit value="Logout" />
 </html:form>

</body>
</html>

Logout Action page package struts.action; 注销操作页面包struts.action;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LogoutAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        HttpSession session = request.getSession(true);
        System.out.println(session.getAttribute("Username"));

        try{
            session.removeAttribute("Username");
            session.invalidate();
            return mapping.findForward(SUCCESS);
        }catch(Exception ex){
            System.out.println("Error");
        }
    return mapping.findForward(FAILURE);
    }
} 

Corresponding LogoutForm package struts.form; 对应的LogoutForm包struts.form; import org.apache.struts.action.*; 导入org.apache.struts.action。*;

public class LogoutForm extends ActionForm{

    private static final long serialVersionUID = 1L;

}

So the session is created in my Login action, and it works, as if I use the getAttribute() and print it to the console, the username comes up. 因此,该会话是在我的Login操作中创建的,并且可以正常工作,就像我使用getAttribute()并将其打印到控制台一样,出现了用户名。 However, the username won't show up on my success.jsp page. 但是,用户名不会显示在我的success.jsp页面上。

Can anyone help? 有人可以帮忙吗?

您忘了放=

<H1>Hello: <%= session.getAttribute("Username"); %></H1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM