如何检查用户是否已登录Java EE

Min*_* Oo 1 java struts2

我使用Struts2框架.在jsp中,我在项目中显示一个登录框,当用户点击登录按钮时,我设置了一个名为"loggedin"的cookie,并在Action类中将其值设置为"true".

然后返回"success"将再次加载此登录页面.

在登录页面中:

<body>
<%
Cookie cookie[] = request.getCookies();
for( c : cookie )
{
 if( c.getName().equals("loggedin") )
 {
  if( !c.getValue().equals("true") )
  {

%>
//show login form here.

<%

  }//end inner if

  else //if cookie "loggedin" value is "true"
  {

%>

//show username/profile picture/logout button here

<%
  }//end else
 }//end outer if
}/end for loop
%>
</body>
Run Code Online (Sandbox Code Playgroud)

我遇到了问题.当我单击登录表单中的登录按钮时,将设置cookie并重新加载页面.但是,在我手动重新加载页面之前,登录表单仍然显示,而不是用户名/个人资料图片.

  1. 我怎么解决这个问题?
  2. 我认为这不是检查登录的正确方法.有人可以告诉我如何以另一种方式检查这个吗?

Dee*_*ala 6

请 !请 !不要使用scriptlet.它们难以维护,每个开发人员都厌恶它们.

跟踪会话的方法有很多种.

  • 使用cookies
  • URL重写
  • 隐藏的参数

这些都在网络上有详细记录.生成会话的最快方法是执行HttpSession session = request.getSession(true);.您可以在创建此对象后继续将会话信息附加到该对象.

看一下JSTL入门并讨论为什么scriptlet不好.