Sessões

3 respostas
A

Gostaria de saber como posso trabalhar com sessões… estou trabalhando com servlets… gostaria que o usuário que acessar minha página fique com sua sessão aberta ate quando terminar a navegação… sem precisar ficar logando no sistema quando entrar em uma página que precise ser usuário cadastrado…

Como posso fazer isso??

Obrigado

3 Respostas

L

Obtaining a Session

The getSession method of the HttpServletRequest object returns a user’s session. When you call the method with its create argument as true, the implementation creates a session if necessary.

To properly maintain the session, you must call getSession before any output is written to the response. (If you respond using a Writer, then you must call getSession before accessing the Writer, not just before sending any response data.)

The Duke’s Bookstore example uses session tracking to keep track of the books in the user’s shopping cart. Here is an example of the CatalogServlet getting a session for a user:

public class CatalogServlet extends HttpServlet { 

        public void doGet (HttpServletRequest request,
                           HttpServletResponse response)
      throws ServletException, IOException
        {
            // Get the user's session and shopping cart
       HttpSession session = request.getSession(true);
            ...
       out = response.getWriter();
            ...
        }
    }

Dá uma olhada no tutorial da Sun para Servlets…

:wink:
[]'s

B

Basta pegar a sessão no servlet com o método getSession da request (como disse o leandrolima).

Usando o parametro true no método vc cria uma nova sessão, se não houver uma instanciada.

Aí é só trabalhar com ela.

Acho q isso resolve seu problema

Leandro_Rangel_Santo

O HttpServletRequest ja tem o metodo HttpSession getSession() que faz o mesmo que o HttpSession getSession(true)

fora isso ,seria bom você ajustar tb o timeout ,para garantir que a sessão sera mantida

Criado 12 de agosto de 2003
Ultima resposta 13 de ago. de 2003
Respostas 3
Participantes 4