📄 jsp和struts解决用户退出问题 - fanqiang_com.htm
字号:
password.");<BR> rd =
request.getRequestDispatcher("login.jsp");
<BR> }<BR> //...<BR> rd.forward(request,
response);<BR>//...</TD></TR></TBODY></TABLE><BR> 为了实现上述方法,你必须记录每个用户的最后登陆时间。对于采用关系型数据库安全域来说,这点可以可以通过在某个表中加上lastLogin字段轻松实现。LDAP以及其他的安全域需要稍微动下脑筋,但很显然是可以实现的。<BR><BR> 表示最后登陆时间的方法有很多。示例logoutSampleJSP3利用了自1970年1月1日以来的毫秒数。这个方法在许多人在不同浏览器中用一个用户账号登陆时也是可行的。<BR><BR> <B>运行logoutSampleJSP3</B><BR><BR> 运行示例logoutSampleJSP3将展示如何正确处理退出问题。一旦用户退出,点击浏览器上的后退按钮在任何情况下都不会是受保护的页面在浏览器上显示出来。这个示例展示了如何正确处理退出问题而不需要额外的培训。<BR><BR> 为了使代码更简练有效,一些冗余的代码可以剔除掉。一种途径就是把清单4中的代码写到一个单独的JSP页中,通过标签<jsp:include>其他页面也可以引用。</SPAN> <BR><!--StartFragment --><SPAN
class=f14><B>Struts框架下的退出实现</B><BR><BR> 与直接使用JSP或JSP/servlets相比,另一个可选的方案是使用Struts。为一个基于Struts的Web应用添加一个处理退出问题的框架可以优雅地不费气力的实现。这部分归功于
Struts是采用MVC设计模式的因此将模型和视图清晰的分开。另外,Java是一个面向对象的语言,其支持继承,可以比JSP中的脚本更为容易地实现代码重用。在Struts中,清单4中的代码可以从JSP页面中移植到Action类的execute()方法中。<BR>此外,我们还可以定义一个继承
Struts
Action类的基本类,其execute()方法中包含了清单4中的代码。通过使用类继承机制,其他类可以继承基本类中的通用逻辑来设置HTTP头信息以及检索HttpSession对象中的username字符串。这个基本类是一个抽象类并定义了一个抽象方法executeAction()。所有继承自基类的子类都应实现exectuteAction()方法而不是覆盖它。清单6是基类的部分代码:<BR><BR> 清单6<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#d8d8d1
border=1>
<TBODY>
<TR>
<TD>public abstract class BaseAction extends Action
{<BR> public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,
HttpServletResponse response) <BR> throws IOException,
ServletException
{<BR> response.setHeader("Cache-Control","no-cache");
<BR> //Forces caches to obtain a new copy of the page from
the origin
server<BR> response.setHeader("Cache-Control","no-store");<BR> //Directs
caches not to store the page under any
circumstance<BR> response.setDateHeader("Expires", 0);
//Causes the proxy cache to see the page as
"stale"<BR> response.setHeader("Pragma","no-cache"); //HTTP
1.0 backward compatibility <BR><BR> if
(!this.userIsLoggedIn(request)) {<BR> ActionErrors errors =
new ActionErrors();<BR> errors.add("error", new
ActionError("logon.sessionEnded"));<BR> this.saveErrors(request,
errors);<BR> return
mapping.findForward("sessionEnded");<BR> }<BR> return
executeAction(mapping, form, request,
response);<BR> }<BR><BR> protected abstract ActionForward
executeAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
<BR> throws IOException, ServletException; <BR><BR> private
boolean userIsLoggedIn(HttpServletRequest request) {<BR> if
(request.getSession().getAttribute("User") == null)
{<BR> return false;<BR> }<BR><BR> return
true;<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 清单6中的代码与清单4中的很相像,仅仅只是用ActionMapping
findForward替代了RequestDispatcher
forward。清单6中,如果在HttpSession中未找到username字符串,ActionMapping对象将找到名为
sessionEnded的forward元素并跳转到对应的path。如果找到了,子类将执行其实现了executeAction()方法的业务逻辑。因此,在配置文件struts-web.xml中为所有子类声明个一名为sessionEnded的forward元素是必须的。清单7以secure1
action阐明了这样一个声明:<BR><BR> 清单7<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#d8d8d1
border=1>
<TBODY>
<TR>
<TD><action path="/secure1"
<BR>type="com.kevinhle.logoutSampleStruts.Secure1Action"
<BR>scope="request"><BR><forward name="success"
path="/WEB-INF/jsps/secure1.jsp"/><BR><forward
name="sessionEnded"
path="/login.jsp"/><BR></action></TD></TR></TBODY></TABLE><BR> 继承自BaseAction类的子类Secure1Action实现了executeAction()方法而不是覆盖它。Secure1Action类不执行任何退出代码,如清单8:<BR><BR>
<TABLE borderColor=#ffcc66 width="90%" align=center bgColor=#d8d8d1
border=1>
<TBODY>
<TR>
<TD>public class Secure1Action extends BaseAction {<BR> public
ActionForward executeAction(ActionMapping mapping, ActionForm
form,<BR>HttpServletRequest request, HttpServletResponse
response)<BR> throws IOException, ServletException
{<BR><BR> HttpSession session = request.getSession();
<BR> return
(mapping.findForward("success"));<BR> }<BR>}</TD></TR></TBODY></TABLE><BR> 只需要定义一个基类而不需要额外的代码工作,上述解决方案是优雅而有效的。不管怎样,将通用的行为方法写成一个继承StrutsAction的基类是许多Struts项目的共同经验,值得推荐。<BR><BR> <B>结论</B><BR><BR> 本文阐述了解决退出问题的方案,尽管方案简单的令人惊讶,但却在所有情况下都能有效地工作。无论是对JSP还是Struts,所要做的不过是写一段不超过50行的代码以及一个记录用户最后登陆时间的方法。在Web应用中混合使用这些方案能够使拥护的私人数据不致泄露,同时,也能增加用户的经验。</SPAN>
<!-- 正文end --><BR>(http://www.fanqiang.com)<BR></FONT><BR><FONT
color=#999999><SMALL>原文链接:<A
href="http://news.chinabyte.com/SoftChannel/72342371945283584/20041221/1891116_2.shtml"
target=_blank>http://news.chinabyte.com/SoftChannel/72342371945283584/20041221/1891116_2.shtml</A></SMALL></FONT>
<BR></FONT></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=750 border=0>
<TBODY>
<TR>
<TD align=middle width=620>
<TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
<TBODY>
<TR>
<TD width=562>
<TABLE cellSpacing=0 cellPadding=0 width=562 border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=1 cellPadding=5 width="100%" border=0>
<TBODY>
<TR>
<TD class=f14 bgColor=#666666><B><FONT color=#ffffff
size=2> <B>相关文章</B></FONT></B></TD></TR>
<TR>
<TD class=f14><A
href="http://fanqiang.chinaunix.net/program/java/2001-05-30/2068.shtml"
target=_blank>jsp入门好文章</A><FONT
color=gray> 2001-05-30 10:08:01</FONT> <BR><A
href="http://fanqiang.chinaunix.net/program/java/2001-05-30/2069.shtml"
target=_blank>JSP白皮书</A><FONT
color=gray> 2001-05-30 11:00:00</FONT> <BR><A
href="http://fanqiang.chinaunix.net/safe/program/2002-01-29/2620.shtml"
target=_blank>JSP应用的安全问题</A><FONT
color=gray> 2002-01-29 20:56:44</FONT> <BR><A
href="http://fanqiang.chinaunix.net/app/other/2001-04-21/2693.shtml"
target=_blank>gnujsp1.0.0在RedHat下基于apache
jserv的安装</A><FONT color=gray> 2001-04-21
18:08:34</FONT><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD height=10></TD></TR></TBODY></TABLE><!--结束:底部-->
<TABLE width=750 border=0>
<TBODY>
<TR>
<TD width="100%" bgColor=#d09f0d colSpan=5 height=2><IMG height=1
src="JSP和Struts解决用户退出问题 - fanqiang_com.files/c.gif" width=1></TD></TR>
<TR>
<TD vAlign=top width="100%" colSpan=5 height=40>
<P align=center><FONT
color=#ffffff>★ 感谢所有的作者为我们学习技术知识提供了一条捷径 ★
<BR>www.fanqiang.com</FONT></P></TD></TR></TBODY></TABLE>
<CENTER></CENTER></TR></TBODY></TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -