Results 1 to 2 of 2
  1. #1
    Anis is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default How to destroy session?

    dear all...

    how to destroy session?for example,when i logout from web,and im going back to previous page,i need to relogin..
    right now..based on my code below,it still hold the username when i logout and back to previous page..tq..



    Logout.do


    package homestayPackage;

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;


    public class Logout extends HttpServlet {

    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();


    String username=session.getAttribute("CUST_NAME").toStrin g();

    session.invalidate();
    session = request.getSession(false);
    out.println("<html><script type=\"text/javascript\">alert('"+ username +" are logout');window.location='LoginPage.jsp'</script></html>");
    try {
    /* TODO output your page here
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet servletLogout</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet servletLogout at " + request.getContextPath () + "</h1>");
    out.println("</body>");
    out.println("</html>");
    */
    } finally {
    out.close();
    }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    * @return a String containing servlet description
    */
    @Override
    public String getServletInfo() {
    return "Short description";
    }// </editor-fold>

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to destroy session?

    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db

    Edit: I posted those three links in the first thread you started. Since you obviously haven't gone through those links, we can assume that you post on a forum only to waste other people's time, and never so much as revisit the thread you started to read the responses.

    IOW, unworthy of help.
    Last edited by DarrylBurke; 06-03-2012 at 09:53 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Destroy a JLabel from within its paintComponent
    By eyeBoth in forum AWT / Swing
    Replies: 4
    Last Post: 10-29-2011, 01:49 PM
  2. Destroy jtextarea and jbutton.
    By sublixt in forum New To Java
    Replies: 3
    Last Post: 08-25-2011, 08:06 AM
  3. How to destroy a Process in windows?
    By hahanizhu in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-26-2011, 11:19 AM
  4. Jbutton to destroy process
    By peterhammond in forum AWT / Swing
    Replies: 1
    Last Post: 04-19-2011, 07:58 PM
  5. Destroy object
    By anjanesh in forum New To Java
    Replies: 11
    Last Post: 01-02-2010, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •