最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

JSP和Struts解决用户退出问题

日期:11-22    来源:中国设计秀    作者:cnwebshow.com

HttpServletResponse response) epU中国设计秀
 throws IOException, ServletException;epU中国设计秀

 private boolean userIsLoggedIn(HttpServletRequest request) {epU中国设计秀
  if (request.getSession().getAttribute("User") == null) {epU中国设计秀
   return false;epU中国设计秀
  }epU中国设计秀

  return true;epU中国设计秀
 }epU中国设计秀
}epU中国设计秀

  清单6中的代码与清单4中的很相像,仅仅只是用ActionMapping findForward替代了RequestDispatcher forward。清单6中,如果在HttpSession中未找到username字符串,ActionMapping对象将找到名为sessionEnded的forward元素并跳转到对应的path。如果找到了,子类将执行其实现了executeAction()方法的业务逻辑。因此,在配置文件struts-web.xml中为所有子类声明个一名为sessionEnded的forward元素是必须的。清单7以secure1 action阐明了这样一个声明:epU中国设计秀

  清单7epU中国设计秀

<action path="/secure1" epU中国设计秀
type="com.kevinhle.logoutSampleStruts.Secure1Action" epU中国设计秀
scope="request">epU中国设计秀
<forward name="success" path="/WEB-INF/jsps/secure1.jsp"/>epU中国设计秀
<forward name="sessionEnded" path="/login.jsp"/>epU中国设计秀
</action>epU中国设计秀

  继承自BaseAction类的子类Secure1Action实现了executeAction()方法而不是覆盖它。Secure1Action类不执行任何退出代码,如清单8:epU中国设计秀

public class Secure1Action extends BaseAction {epU中国设计秀
 public ActionForward executeAction(ActionMapping mapping, ActionForm form,epU中国设计秀
HttpServletRequest request, HttpServletResponse response)epU中国设计秀
 throws IOException, ServletException {epU中国设计秀

  HttpSession session = request.getSession(); epU中国设计秀
  return (mapping.findForward("success"));epU中国设计秀
 }epU中国设计秀
}epU中国设计秀

  只需要定义一个基类而不需要额外的代码工作,上述解决方案是优雅而有效的。不管怎样,将通用的行为方法写成一个继承StrutsAction的基类是许多Struts项目的共同经验,值得推荐。epU中国设计秀

  结论epU中国设计秀

  本文阐述了解决退出问题的方案,尽管方案简单的令人惊讶,但却在所有情况下都能有效地工作。无论是对JSP还是Struts,所要做的不过是写一段不超过50行的代码以及一个记录用户最后登陆时间的方法。在Web应用中混合使用这些方案能够使拥护的私人数据不致泄露,同时,也能增加用户的经验。epU中国设计秀

本文引用地址:/bc/article_46721.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明