??xml version="1.0" encoding="utf-8" standalone="yes"?>
void include(ServeltRequest req,ServletResponse res)
可以通过两种方式得到RequestDispatcher:
1、ServletContext.getRequestDispatcher(String path);
其中q里的path必须开始ؓ"/"Q即q里的path必须相对于context的root.
2、ServeltRequest.getRequestDispatcher(String path)
q里的path可以为相对\?如果path开始ؓ"/",则也认ؓ是从context的root开始的?br />二、Redirect由HttpServletResponse.sendRedirect(String location)来支?br />差别Q?br />三个都可以对用户的requestq行转发Q但是还是有许多的不同,差别最主要集中在如下几个方面:
1、forward与include׃Request范围内的对象,而redirect则不行,卻I如果一个javabean被声明ؓrequest范围的话Q则被forward到的资源也可以访问这个javabean,而redriect则不行?br />2、forward与include基本上都是{发到context内部的资源,而redirect可以重定向到外部的资?如: req.sendRedriect("
以上来源 http://www.cn-java.com/target/news.php?news_id=3176
?br />struts-config.xml
<forward name="succ" path="/pages/dynabean2.jsp" redirect="true"/>
讄redirect="true"?转向目标面时效果和redirect一致?br />
public HashMap getData() {
return data;
}
public void setData(HashMap data) {
this.data = data;
}
public String get(String key) {
return (String) data.get(key);
}
public void set(String key, Object value) {
data.put(key, value);
}
}
struts-config.xml中写Q?br /> <form-beans>
<form-bean name="baseform" type="com.web.system.base.BaseForm"/>
</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成?
例如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部分定义了一些重要的头部信息Q如Q浏览器的种c,语言Q类型。Request Headers中还可以包括Cookie的定义。例如:
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属性都会加在URL的后面。你在浏览器的URL地址栏中会看到这些属性,cM?
http://www.somesite/login.do?user=guest&password=guest&jsessionid=1001
从理Z来说Q这3个部分(Request URLQCookie Header, Message BodyQ都可以用来存放Session ID。由于Message BodyҎ必须需要一个包含Session ID的HTML FormQ所以这U方法不通用?
一般用来实现Session的方法有两种Q?
Q?QURL重写?
Web Server在返回Response的时候,查页面中所有的URLQ包括所有的q接Q和HTML Form的Action属性,在这些URL后面加上?jsessionid=XXX”?
下一ơ,用户讉Kq个面中的URL。jsessionid׃传回到Web Server?
Q?QCookie?
如果客户端支持CookieQWeb Server在返回Response的时候,在Response的Header部分Q加入一个“set-cookie: jsessionid=XXXX”header属性,把jsessionid攑֜Cookie里传到客L?
客户端会把Cookie存放在本地文仉Q下一ơ访问Web Server的时候,再把Cookie的信息放到HTTP Request的“Cookie”header属性里面,q样jsessionid随着HTTP Requestq回lWeb Server?
我们来看Tomcat5的源代码如何支持jsessionid?
org.apache.coyote.tomcat5.CoyoteResponsecȝtoEncoded()Ҏ支持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>
<form-beans> <form-bean name="lazyForm" type="org.apache.struts.validator.LazyValidatorForm"/> </form-beans> <action-mappings> <action path="/myActionPath" type="myPackage.MyAction" name="lazyForm" validate="true"/> </action-mappings> </struts-config> |
<html:form action="/myActionPath">
<h2>Simple Property Example</h2> Customer Number: <html:text property="custNo"/> Customer Name: <html:text property="custName"/> <h2>Mapped Property Example</h2> Street: <html:text property="address(street)"/> Town: <html:text property="address(town)"/> State: <html:text property="address(state)"/> Country: <html:text property="address(country)"/> <h2>Indexed Property Example</h2> <logic:iterate id="products" property="products"> Product Code:<html:text name="products" property="code" indexed="true"/> Product Description:<html:text name="products" property="description" indexed="true"/> Product Price:<html:text name="products" property="price" indexed="true"/> </logic:iterate> </html:form> |
java代码: |
public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServeletRequest request, HttpServletResponse response)throwsException{ // Cast form to DynaBean DynaBean dynaForm = (DynaBean)form; // Use the DynaBean String custNo = (String)dynaForm.get("custNo"); // simple Map address = (Map)dynaForm.get("address"); // mapped List products = (List)dynaForm.get("products"); // indexed //... 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.tkk7.com/terry-zj/archive/2005/11/16/20012.html
2Q?d数据攑ֈ一个HashMap中而不是AccountBean之类的自定义l构?BR> 3Q?q个Ҏ的负药力还需具体试?/P>