我使用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并重新加载页面.但是,在我手动重新加载页面之前,登录表单仍然显示,而不是用户名/个人资料图片.
请 !请 !不要使用scriptlet.它们难以维护,每个开发人员都厌恶它们.
跟踪会话的方法有很多种.
这些都在网络上有详细记录.生成会话的最快方法是执行HttpSession session = request.getSession(true);.您可以在创建此对象后继续将会话信息附加到该对象.
看一下JSTL入门并讨论为什么scriptlet不好.
| 归档时间: |
|
| 查看次数: |
1390 次 |
| 最近记录: |