我的 GWT+GAE 应用程序中有两个 servlet。一种是“MyServlet 扩展 RemoteServiceServlet”,它处理来自客户端的所有调用。另一个是“上传扩展 HTTPServlet”处理从客户端发布的文件,如下所述:https ://developers.google.com/appengine/docs/java/blobstore/overview
如何从 Upload servlet 访问在 MyServlet 中设置的对象?
//
//MyServlet extends RemoteServiceServlet
//
public User login(String username, String password) {
User user = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(User.class, "username == '" + username + "' && password == '" + password + "'");
List<User> users = (List<User>) query.execute();
if (!users.isEmpty()) {
user = new User();
User userDB = users.get(0);
user.setUsername(userDB.getUsername());
getThreadLocalRequest().getSession().setAttribute("user", user);
}
pm.close();
return user;
}
public void doSomething() {
//Never returns null. This is good.
User user = getThreadLocalRequest().getSession().getAttribute("user");
....
}
//
//Upload extends HTTPServlet
//
public void doPost(HttpServletRequest req, HttpServletResponse res) {
// Always returns null. WHY?!?
req.getSession().getAttribute("user");
....
}