分布式Session
SAE Java 平台中的应用部署在分布式环境中,因此SAE为每个应用提供了分布式Session功能,以保证Session能多节点共享。Session信息使用分布式的Memcache进行存储,读写速度非常快。
使用Session
分布式Session的使用和Servlet标准一样,用户通过session.setAttribute()的方式来存储数据。分布式Session服务默认为关闭状态。
如果要使用分布式session功能,需在war包中的web.xml中增加一个元素<distributable/>
Session默认有效期为30分钟,如需调整可在应用的web.xml文件中自行设置。
<session-config>
<session-timeout>30</session-timeout>
</session-config>
注意事项
1、Session 信息使用分布式Memcache存储,因此存储到Session中的对象必须实现 java.io.Serializable 接口;
2、由于Session信息采用Cache存储,有较小的丢失概率,所以建议用户将重要信息存储MySQL中;
3、如果应用不需要使用分布式Session功能,建议在web.xml中去掉元素以提高性能。
Login的servlet
package demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sina.sae.memcached.SaeMemcache;
public class LoginAction extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 6187373924444373081L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SaeMemcache mc=new SaeMemcache();
mc.init();
request.getSession().setAttribute("user",new User("eee","t200995968@live.cn"));
response.sendRedirect("/hi");
}
}
User 类
package demo;
public class User implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -4503397959025218110L;
private String username;
private String email;
public User(String username, String email) {
super();
this.username = username;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
一直忽略了Session中的对象必须实现 java.io.Serializable 接口;
唉 还好终于成功了。。呵呵
无锡婚纱摄影 http://www.wxdior.cn