使用 Java 会话进行登录/注销
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16087037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Java sessions for login/logout
提问by user2287752
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;
Logout 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; import org.apache.struts.action.*;
对应LogoutForm包struts.form;导入 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. However, the username won't show up on my success.jsp page.
所以会话是在我的登录操作中创建的,它可以工作,就像我使用 getAttribute() 并将其打印到控制台一样,用户名出现了。但是,用户名不会显示在我的 success.jsp 页面上。
Can anyone help?
任何人都可以帮忙吗?
回答by Suresh Atta
You forgot to put =
你忘了放 =
<H1>Hello: <%= session.getAttribute("Username"); %></H1>