0

i know this question has been asked a lot of time, but i can't really understand how to get it. I made a little servlet that, after login form, set a stateful session bean (wich retrieve the entity) and redirect the user to home. The Servlet works so:

@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);
}

now, the SessionCart has a method who give back username (the method is .getNome()) and i would like to pass it trought http while the user is redirected to home. I could use the request object to redirect, but i get the homepage in the (example i have the servlet in the URL localhost:8080/web/form/login and i get the homepage in the address localhost:8080/web/form/login, but it could be in localhost:8080/web/ or the browser will not recognize images and others elements). How could i get this working?

Update: Some code about SessionCart for @developerwjk

@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
*/
}
Neo87
  • 63
  • 1
  • 11
  • i tried to set a cookie with response.addCookie(c) where c is a cookie containing username, but the browser will not send it after redirect :( – Neo87 Jan 15 '15 at 16:08
  • You have to actually put your supposed session bean in the session. Just naming it `SessionCart` doesn't put it in the session. – developerwjk Jan 15 '15 at 16:13
  • And stay focused. Don't jump all over the map. Lookup `HttpSession` and how to use it...don't switch to trying to do cookies instead because the session isn't magically appearing by giving a class a name that includes the word "Session" in it. – developerwjk Jan 15 '15 at 16:19
  • i'm new to EJB, how to put the session bean in the session? I created it with neatbeans (doing new session bean) – Neo87 Jan 15 '15 at 16:20

1 Answers1

1

You need to use HttpSession and get the session from the request object, and put the bean inside it:

private HttpSession session;
private SessionCart cart;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
{
  String email = request.getParameter("email");
  String password = request.getParameter("password");
  session = request.getSession();
  //I assume the cart was initialized somehow, maybe in the init() method
  cart.login(email, password);
  session.setAttribute("cartbean", cart);
  //there should be a redirect here to some other page
  response.sendRedirect("home");
}

Then in other pages, to retrieve the cart bean, you can do:

HttpSession session = request.getSession();
SessionCart cart = (SessionCart)session.getAttribute("cartbean");
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • shouldn't session bean be relative to sessioncontext automatically by container? Cause i'm reading a book about j2ee and i have never seen this yet – Neo87 Jan 15 '15 at 16:31
  • @Neo87, Is `SessionCart` something you wrote? Maybe you should post some of the code from there to make it more clear what it does. But basically, unless the constructor to `SessionCart` takes in an `HttpSession` you'll have to do it this way. You could pass in the session to the `SessionCart` and have it put things in the session, however. – developerwjk Jan 15 '15 at 16:33
  • @Neo87, Like `SessionCart cart = new SessionCart(request.getSession())` and then use that `HttpSession` in the cart class. But still, to have a reference to the `SessionCart` object in the next page, you'll have to put the `SessionCart` object itself in the session as I showed above. – developerwjk Jan 15 '15 at 16:39
  • edited with declarative part of SessionCart bean. Anyway thx for suggestion about session, it worked greatly :). I was not understanding why modifying the request could change data in session – Neo87 Jan 15 '15 at 16:39