我有两个servlets
:第一个通过在会话中使用对象和持久凭据 ( & )loginServlet
对用户进行身份验证,然后重定向到第二个 servlet ,该 servlet 用于显示保存在. Gmail
userService
username
password
HomeServlet
session
我的问题是,数据第一次保存在 中session
,不能在第二次中检索(它给了我null
)。直到我进入firstServlet
.
第一个 Servlet 是:
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
// initialize user authentification service
UserService userService = UserServiceFactory.getUserService();
Boolean isConnected = (userService.getCurrentUser() != null);
if (isConnected) {
// save credentials in a session
HttpSession session = req.getSession();
session.setAttribute("username", userService.getCurrentUser().getNickname());
session.setAttribute("email", userService.getCurrentUser().getEmail());
session.setAttribute("isConnected", isConnected);
// forward to login view
resp.sendRedirect("/");
}
else
// redirect to 'gmail' login page
resp.sendRedirect(userService.createLoginURL("/"));
}
}
第二个 Servlet 是:
public class GuestbookServlet extends HttpServlet {
static {
ObjectifyService.register(Message.class);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
List<Message> messages = ofy().load().type(Message.class).list();
req.setAttribute("messages", messages);
this.getServletContext().getRequestDispatcher("/WEB-INF/index.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Message message = new Message(req.getParameter("name"), req.getParameter("message"));
ofy().save().entity(message).now();
resp.sendRedirect("/");
}
}
'/'
是第二个 servlet 的路径GuestbookServlet
。