??xml version="1.0" encoding="utf-8" standalone="yes"?>
void include(ServeltRequest req,ServletResponse res)
可以通过两种方式得到RequestDispatcher:
1、ServletContext.getRequestDispatcher(String path);
其中q里的path必须开始ؓ(f)"/"Q即q里的path必须相对于context的root.
2、ServeltRequest.getRequestDispatcher(String path)
q里的path可以为相对\?如果path开始ؓ(f)"/",则也认ؓ(f)是从context的root开始的?br />二、Redirect由HttpServletResponse.sendRedirect(String location)来支?br />差别Q?br />三个都可以对用户的requestq行转发Q但是还是有许多的不同,差别最主要集中在如下几个方面:(x)
1、forward与include׃Request范围内的对象,而redirect则不行,卻I(x)如果一个javabean被声明ؓ(f)request范围的话Q则被forward到的资源也可以访问这个javabean,而redriect则不行?br />2、forward与include基本上都是{发到context内部的资源,而redirect可以重定向到外部的资?如:(x) req.sendRedriect("
以上来源 http://www.cn-java.com/target/news.php?news_id=3176
?br />struts-config.xml
<forward name="succ" (tng) path="/pages/dynabean2.jsp" redirect="true"/>
讄redirect="true"?转向目标面时效果和redirect一致?br />
(tng) (tng) (tng) public HashMap getData() {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) return data;
(tng) (tng) (tng) }
(tng) (tng) (tng) public void setData(HashMap data) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) this.data = data;
(tng) (tng) (tng) }
(tng) (tng) (tng) public String get(String key) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) return (String) data.get(key);
(tng) (tng) (tng) }
(tng) (tng) (tng) public void set(String key, Object value) {
(tng) (tng) (tng) (tng) (tng) (tng) (tng) data.put(key, value);
(tng) (tng) (tng) }
}
struts-config.xml中写Q?br /> (tng)<form-beans>
(tng) (tng) (tng) <form-bean (tng) (tng) (tng) (tng) (tng) (tng) name="baseform" (tng) (tng) (tng) (tng) type="com.web.system.base.BaseForm"/>
(tng) </form-beans>
我们看到Q号码牌QSession IDQ必d含在HTTP Request里面。关于HTTP Request的具体格式,请参见HTTP协议Q?A >http://www.w3.org/Protocols/Q。这里只做一个简单的介绍?
在Java Web ServerQ即Servlet/JSP ServerQ中QSession ID用jsessionid表示Q请参见Servlet规范Q?
HTTP Request一般由3部分l成Q?
Q?QRequest Line
q一行由HTTP MethodQ如GET或POSTQ、URL、和HTTP版本L(fng)成?
例如QGET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1
GET http://www.google.com/search?q=Tomcat HTTP/1.1
POST http://www.google.com/search HTTP/1.1
GET http://www.somsite.com/menu.do;jsessionid=1001 HTTP/1.1
Q?QRequest Headers
q部分定义了(jin)一些重要的头部信息Q如Q浏览器的种c,语言Q类型。Request Headers中还可以包括Cookie的定义。例如:(x)
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Accept-Language: en-us
Cookie: jsessionid=1001
Q?QMessage Body
如果HTTP Method是GETQ那么Message Body为空?
如果HTTP Method是POSTQ说明这个HTTP Request是submit一个HTML Form的结果,
那么Message Body为HTML Form里面定义的Input属性。例如,
user=guest
password=guest
jsessionid=1001
LQ如果把HTML Form元素的Method属性改为GET。那么,Message Body为空Q所有的Input属性都?x)加在URL的后面。你在浏览器的URL地址栏中?x)看到这些属性,cM?
http://www.somesite/login.do?user=guest&password=guest&jsessionid=1001
从理Z来说Q这3个部分(Request URLQCookie Header, Message BodyQ都可以用来存放Session ID。由于Message BodyҎ(gu)必须需要一个包含Session ID的HTML FormQ所以这U方法不通用?
一般用来实现Session的方法有两种Q?
Q?QURL重写?
Web Server在返回Response的时候,(g)查页面中所有的URLQ包括所有的q接Q和HTML Form的Action属性,在这些URL后面加上?jsessionid=XXX”?
下一ơ,用户讉Kq个面中的URL。jsessionid׃(x)传回到Web Server?
Q?QCookie?
如果客户端支持CookieQW(xu)eb Server在返回Response的时候,在Response的Header部分Q加入一个“set-cookie: jsessionid=XXXX”header属性,把jsessionid攑֜Cookie里传到客L(fng)?
客户端会(x)把Cookie存放在本地文仉Q下一ơ访问Web Server的时候,再把Cookie的信息放到HTTP Request的“Cookie”header属性里面,q样jsessionid随着HTTP Requestq回lWeb Server?
我们来看Tomcat5的源代码如何支持jsessionid?
org.apache.coyote.tomcat5.CoyoteResponsecȝtoEncoded()Ҏ(gu)支持URL重写?
String toEncoded(String url, String sessionId) {
?
StringBuffer sb = new StringBuffer(path);
if( sb.length() > 0 ) { // jsessionid can't be first.
sb.append(";jsessionid=");
sb.append(sessionId);
}
sb.append(anchor);
sb.append(query);
return (sb.toString());
}
我们来看org.apache.coyote.tomcat5.CoyoteRequest的两个方法configureSessionCookie()
doGetSession()用Cookie支持jsessionid.
/**
* Configures the given JSESSIONID cookie.
*
* @param cookie The JSESSIONID cookie to be configured
*/
protected void configureSessionCookie(Cookie cookie) {
?
}
HttpSession doGetSession(boolean create){
?
// Creating a new session cookie based on that session
if ((session != null) && (getContext() != null)
&& getContext().getCookies()) {
Cookie cookie = new Cookie(Globals.SESSION_COOKIE_NAME,
session.getId());
configureSessionCookie(cookie);
((HttpServletResponse) response).addCookie(cookie);
}
?
}
Session的典型应用是存放用户的Login信息Q如用户名,密码Q权限角色等信息Q应用程序(如Email服务、网上银行等pȝQ根据这些信息进行n份验证和权限验证
<struts-config> (tng)
(tng) <form-beans> (tng) (tng) (tng) (tng) (tng) (tng)<form-bean name="lazyForm" type="org.apache.struts.validator.LazyValidatorForm"/> (tng) (tng) (tng)</form-beans> (tng) (tng) (tng)<action-mappings> (tng) (tng) (tng) (tng)<action path="/myActionPath" type="myPackage.MyAction" name="lazyForm" validate="true"/> (tng) (tng) (tng)</action-mappings> </struts-config> |
<html:form action="/myActionPath"> (tng)
(tng) <h2>Simple Property Example</h2> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) Customer Number: <html:text property="custNo"/> (tng) (tng) (tng) (tng) (tng) Customer Name: (tng) (tng)<html:text property="custName"/> (tng) (tng) <h2>Mapped Property Example</h2> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) Street: (tng) <html:text property="address(street)"/> (tng) (tng) (tng) (tng) (tng) Town: (tng) (tng) <html:text property="address(town)"/> (tng) (tng) (tng) (tng) (tng) State: (tng) (tng)<html:text property="address(state)"/> (tng) (tng) (tng) (tng) (tng) Country: <html:text property="address(country)"/> (tng) (tng) <h2>Indexed Property Example</h2> (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) <logic:iterate id="products" property="products"> (tng) (tng) (tng) (tng) (tng)Product Code:<html:text name="products" property="code" indexed="true"/> (tng) (tng) (tng) (tng) (tng)Product Description:<html:text name="products" property="description" indexed="true"/> (tng) (tng) (tng) (tng) (tng)Product Price:<html:text name="products" property="price" (tng)indexed="true"/> (tng) (tng) </logic:iterate> </html:form> |
java代码: (tng) |
public ActionForward execute(ActionMapping mapping,
(tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)ActionForm form, (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)HttpServeletRequest request, (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng) (tng)HttpServletResponse response)throwsException{ (tng) (tng) (tng) (tng) (tng)// Cast form to DynaBean (tng) (tng) (tng) (tng) (tng)DynaBean dynaForm = (DynaBean)form; (tng) (tng) (tng) (tng) (tng)// Use the DynaBean (tng) (tng) (tng) (tng) (tng)String custNo = (String)dynaForm.get("custNo"); (tng) (tng)// simple (tng) (tng) (tng) (tng) (tng)Map address (tng) (tng)= (Map)dynaForm.get("address"); (tng) (tng) (tng)// mapped (tng) (tng) (tng) (tng) (tng)List products = (List)dynaForm.get("products"); (tng) (tng)// indexed (tng) (tng) (tng) (tng) (tng) (tng)//... etc } |
4 实现:1Q?app1d时把d数据攑ֈ一个HashMapl定到initialContext?BR> 2Q?q移链接http://app2ServerAddress/app2/TopEntry.do?sessid=q里写sessionId?BR> 3Q?app2斚w过request.getAttribute("sessid")得到sessionId再到initialContext中lookup出登录数据?/P>
附注:1Q?nbsp; weblogic中JNDI的用可参考本Blog相关文章?A HREF="/terry-zj/archive/2005/11/16/20012.html">http://www.aygfsteel.com/terry-zj/archive/2005/11/16/20012.html
2Q?d数据攑ֈ一个HashMap中而不是AccountBean之类的自定义l构?BR> 3Q?q个Ҏ(gu)的负药力还需具体试?/P>