Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Fare il logout quando la sessione è scaduta

    Ho trovato un bug del mio programma.
    Quando la sessione è scaduta e faccio un logout finisco in error.jsp.
    codice:
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error.jsp</location>
    </error-page>
    Perché accade questo e come risolvo?
    codice:
    package web1;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    @WebServlet(name = "ServletLogout", urlPatterns="/logout.jsp")
    public class ServletLogout extends HttpServlet {
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                IOException {
            HttpSession session = request.getSession(false);
            session.invalidate();
            response.sendRedirect("/index.jsp");
        }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                IOException {
    response.sendRedirect("/index.jsp");
        }
    
    }
    Più pratica in futuro...

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    Quando la sessione è scaduta e faccio un logout finisco in error.jsp.
    Quando la sessione è scaduta, un request.getSession(false) (e solo con false, attenzione!) restituisce null. Su cui chiaramente non puoi invocare nulla. Altrimenti hai un bel NullPointerException e se hai un solo <error-page> per Throwable, è abbastanza logico che ci finisci dentro.

    Ma il punto non è il <error-page> (che è giusto per trattare le eccezioni in generale) ma il fatto del null che ovviamente va considerato e controllato.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  3. #3
    Quote Originariamente inviata da andbin Visualizza il messaggio
    Quando la sessione è scaduta, un request.getSession(false) (e solo con false, attenzione!) restituisce null. Su cui chiaramente non puoi invocare nulla. Altrimenti hai un bel NullPointerException e se hai un solo <error-page> per Throwable, è abbastanza logico che ci finisci dentro.

    Ma il punto non è il <error-page> (che è giusto per trattare le eccezioni in generale) ma il fatto del null che ovviamente va considerato e controllato.
    Mille grazie, tutto chiarissimo, sono proprio un sciocco.
    Più pratica in futuro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.