【发布时间】:2015-07-15 07:10:35
【问题描述】:
我有一个应用程序,我可以在其中登录,它会将我带到下一页并显示一个表格。
我已经通过实现SessionAware 接口来处理会话。
当我使用浏览器上的刷新按钮或 F5 刷新页面时,它工作正常。但是,如果我在 url 上按 enter,它会将我带到登录页面,因为用户名和密码是 null。
请在下面找到我的java代码和jsp代码以及struts.xml
JAVA 类:
public String Authentication()
{
// return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
if(j_username == null)
{
status="failure3";
return status;
}
System.out.println("get username and get password" +j_username + j_password);
// if no userName stored in the session,
// check the entered userName and password
if (j_username != null && j_password != null) {
System.out.println("inside function");
// add userName to the session
sessionMap.put("j_username", j_username);
status = otlAuthenticationController.loginAuthentication(j_username,j_password);
if(status == "success")
{
this.otlUserList= otlAuthenticationController.obtainList();
System.out.println("size is"+otlUserList.size());
}
}
return status;
}
public String logout()
{
if (sessionMap instanceof org.apache.struts2.dispatcher.SessionMap) {
try {
//((org.apache.struts2.dispatcher.SessionMap) sessionMap).invalidate();
if (sessionMap.containsKey("userName")) {
sessionMap.remove(j_username);
System.out.println("session killed");
status ="success";
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
return "success";
}
JSP 页面:
<div class="label" style="margin-top:10px;"><s:text name="login.password" /></div><input id="j_password" name="j_password" type="password" placeholder="Password" size="31" autocomplete="off"/><br><br>
<div class="left" style="padding-left:150px; horizontal-align:right;text-align:center;"><input type="submit" name="login" value=<s:text name="login"/> class="button normal normal1" onClick="validate1()"/></div>
<br> <br><s:if test="status=='failure'">
<center><p style="color:RED" text-align :"center"><b> Invalid username. Please enter a valid username</b></p></center>
</s:if>
<s:if test="status=='failure2'">
<center><p style="color:RED" text-align :"center"><b> Invalid password. Please enter a valid password</b></p></center>
</s:if>
<s:if test="status=='failure3'">
<center><p style="color:RED" text-align :"center"><b> Login to the application to continue</b></p></center>
</s:if>
</div>
struts.xml:
<action name="logout" class ="com.opentext.planning.view.OTLAuthentication" method="logout">
<result name="success">/WEB-INF/index.jsp</result>
<result name="failure">/WEB-INF/error.jsp</result>
</action>
<action name ="Otl_Homepage"
class="com.opentext.planning.view.OTLAuthentication" method="Authentication">
<result name="success">/WEB-INF/Otl_Homepage.jsp</result>
<result name="failure">/WEB-INF/index.jsp</result>
<result name="failure2">/WEB-INF/index.jsp</result>
<result name="failure3">/WEB-INF/index.jsp</result>
</action>
【问题讨论】:
-
同一个jsp返回3个不同的失败结果有什么意义?为什么不使用 camelCase 作为变量名?为什么 JSP 代码的格式如此糟糕?为什么一个动作名称是小写的而另一个是大写的?为什么一种操作方法是小写的而另一种是大写的?为什么有时
{是内联的,有时它是在一个新行中?伙计,这很重要……考虑改进它。顺便说一句,登录凭据应在自定义拦截器中检查,而不是在操作方法中检查,并且该拦截器应应用于除登录操作之外的每个操作...
标签: java jsp authentication parameters struts2