【发布时间】:2015-01-15 14:49:51
【问题描述】:
我知道这个问题已经被问了很多时间,但我真的不明白如何得到它。我做了一个小 servlet,在登录表单后,设置一个有状态会话 bean(检索实体)并将用户重定向到家。 Servlet 是这样工作的:
@EJB
private SessionCart ses;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
ses.login(email, password);
}
现在,SessionCart 有一个返回用户名的方法(该方法是 .getNome()),我想在用户被重定向到主页时通过 http 传递它。 我可以使用请求对象进行重定向,但我得到了主页(例如,我在 URL localhost:8080/web/form/login 中有 servlet,我在地址 localhost:8080/web/form/ 中得到了主页登录,但它可能在 localhost:8080/web/ 中,否则浏览器将无法识别图像和其他元素)。我怎样才能让它工作?
更新: 一些关于 @developerwjk 的 SessionCart 的代码
@Stateful
@LocalBean
public class SessionCart {
@Resource
private SessionContext context;
@PersistenceContext(unitName="ibeiPU")
private EntityManager manager;
private LinkedList<Vendita> carrello;
private LinkedList<Integer> quantita;
private Persona utente;
/*
* Business methods
*/
}
【问题讨论】:
-
我尝试使用 response.addCookie(c) 设置一个 cookie,其中 c 是一个包含用户名的 cookie,但是浏览器在重定向后不会发送它:(
-
您必须实际将您假定的会话 bean 放入会话中。仅将其命名为
SessionCart不会将其放入会话中。 -
保持专注。不要跳过整个地图。查找
HttpSession以及如何使用它...不要转而尝试做 cookie,因为通过给一个包含“会话”一词的类命名,会话不会神奇地出现。 -
我是 EJB 新手,如何将会话 bean 放入会话中?我用 netbeans 创建它(做新的会话 bean)
标签: jsp jakarta-ee servlets ejb-3.0