登錄攔截實驗報告_第1頁
登錄攔截實驗報告_第2頁
登錄攔截實驗報告_第3頁
登錄攔截實驗報告_第4頁
登錄攔截實驗報告_第5頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

實驗目的掌握攔截器的使用。實驗內(nèi)容自定義攔截器對發(fā)表留言的操作進行攔截。如果用戶已經(jīng)登錄,則能夠提交留言;否則,不能提交留言,提示用戶需要先登錄。三、實驗過程程序目錄結(jié)構(gòu):Struts配置文件:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""/dtds/struts-2.0.dtd"><struts> <constantname="struts.enable.DynamicMethodInvocation" value="true"/> <constantname="struts.devMode"value="false"/> <includefile="struts-default.xml"/> <packagename="StrutsExample"extends="struts-default"> <interceptors> <interceptorname="leaveWordInterceptor"class="erceptor.LeaveWordInterceptor"/> </interceptors><default-interceptor-refname="defaultStack"/> <actionname="word"class="com.action.WordAction"> <resultname="success">success.jsp</result> <resultname="login">login.jsp</result> <interceptor-refname="defaultStack"/> <interceptor-refname="leaveWordInterceptor"/> </action> <actionname="login"class="com.action.LoginAction"> <resultname="success">leaveword.jsp</result> </action> </package></struts>LoginAction.java程序:packagecom.action;importjava.util.Map;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.user.User;publicclassLoginActionextendsActionSupport{//處理用戶登錄 privateUseruser; publicUsergetUser(){ returnuser; } publicvoidsetUser(Useruser){ this.user=user; } @SuppressWarnings("unchecked") publicStringexecute() {ActionContextac=ActionContext.getContext(); Map<String,String>session1=(Map<String,String>)ac.getSession(); session1.put("username",user.getUserName());//把登錄用戶名保存在session中,供攔截器訪問 //省略登錄代碼 return"success"; }}WordAction.java程序:packagecom.action;importcom.word.Word;publicclassWordAction{//處理留言信息 privateWordword; publicvoidsetWord(Wordword){ this.word=word; } publicWordgetWord(){ returnword; } publicStringexecute(){ //處理留言代碼 return"success"; }}LeaveWordInterceptor.java程序:packageerceptor;importjava.util.Map;importcom.opensymphony.xwork2.Action;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionInvocation;importerceptor.AbstractInterceptor;publicclassLeaveWordInterceptorextendsAbstractInterceptor{ publicStringintercept(ActionInvocationai)throwsException{ ActionContextac=ai.getInvocationContext();//返回ActionContext的對象,供需要ActionContext時用 Map<String,String>session=(Map<String,String>)ac.getSession(); StringuserName=(String)session.get("username"); if((userName!=null)&&(userName.length()>0)){ returnai.invoke();//放行,把執(zhí)行控制權(quán)轉(zhuǎn)交給WordAction } else{ System.out.println("dksdkdk"); ac.put("sorry","對不起,請先登錄!");//保存一條信息在ac中,以便在login.jsp上顯示 returnAction.LOGIN;//替被它攔截的Action返回LOGIN } }}Leaveword.jsp程序:<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>提交留言頁面</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --></head><body><center>提交留言:<s:formaction="word"> <s:textfieldname="word.title"label="標題"/> <s:textareaname="word.message"label="內(nèi)容"/> <s:submitvalue="提交"></s:submit> </s:form></center></body></html>login.jsp程序:<%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""/TR/html4/loose.dtd"><html><head><title>用戶登錄頁</title></head><bodyvLink="#006666"link="#003366"bgColor="#E0F0F8"> <center> <b>${sorry}</b><br><br> 用戶登錄<br><br> <s:formaction="login"> <s:textfi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論