无法让会话在我的 Java 应用程序中工作。
我的 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);
}
}
}
对应的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,登录时显示的页面
<%@ 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>
注销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);
}
}
对应LogoutForm包struts.form;导入 org.apache.struts.action.*;
public class LogoutForm extends ActionForm{
private static final long serialVersionUID = 1L;
}
所以会话是在我的登录操作中创建的,它可以工作,就像我使用 getAttribute() 并将其打印到控制台一样,用户名出现了。但是,用户名不会显示在我的 success.jsp 页面上。
任何人都可以帮忙吗?