Forum Sementara Putera.com

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Forum Sementara Putera.com

Bersama kita perkemaskan forum ini sementara forum asal dalam pemulihan.

Forum putera dah kembali. Masalah sudah berjaya diselesaikan. Sila lawati http://forum.putera.com/tanya


    Problem ngan JSP session

    avatar
    parangjiwa
    Ahli Baharu
    Ahli Baharu


    Number of posts : 34
    Registration date : 10/07/2009

    Problem ngan JSP session Empty Problem ngan JSP session

    Post by parangjiwa Mon Jul 13, 2009 12:42 pm

    Salam...hamba nak bertanya..kalau silap umah..sorry ekk..

    hamba wat satu login/logout simple nag JSP guna session...mmg jadi..tetapi setelah logout, masih bleh masuk ke page sebelumnya bila kita click "back" kat browser..

    camner ek...ahmab dh try solution2 dari pakcik Google dan Yahoo..tp still tak dpt...code2 hamba sprti di bawah:

    sessionForm.jsp
    Code:


    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
    <%
     String usernameFinal=(String) session.getAttribute("usernameSession");
      if(usernameFinal==null)
        {
          out.println("you are log out!!Session id is: "+session.getId());
        }
      else
        {
        out.println("you are still logged in.."+session.getId());
        }
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <form method="post" action="setSession.jsp">
    Enter your name: <input type="text" name="userName">
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>


    setSession.jsp
    Code:


    <%
    response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
    response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
    response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
    response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
    %>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>

    <%
    String usernameInput=request.getParameter("userName"); // get userName value from form and store in variable...

    out.println(usernameInput);

    if((usernameInput==null)||(usernameInput==""))
     {
      http://out.println("Name required!");
      %><jsp:forward page="sessionForm.jsp"/><%
     }
    else
     {
     out.println("Session available but not yet set..Now setting session attributes");
     }

    session.setAttribute("usernameSession", usernameInput); //create the session object with the name usernameSession
    out.println("ok...done setting the session...the session id is still da same: "+session.getId());
    %>

    <body>
    <p><a href="showSessionValue.jsp">Next to show session values</a><p>
    </body>
    </html>


    showSessionValue.jsp
    Code:


    <%
    response.setHeader("Cache-Control","no-cache"); //Forces caches to obtain a new copy of the page from the origin server
    response.setHeader("Cache-Control","no-store"); //Directs caches not to store the page under any circumstance
    response.setDateHeader("Expires", 0); //Causes the proxy cache to see the page as "stale"
    response.setHeader("Pragma","no-cache"); //HTTP 1.0 backward compatibility
    %>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
    <%
    String usernameFinalZ=(String) session.getAttribute("usernameSession");
    request.getSession();
    out.println(usernameFinalZ);
    if(usernameFinalZ==null)
    //if(request.getAttribute("usernameSession")==null)
      {
      %><jsp:forward page="home.jsp"/><%
      }
    else
      {
      out.println("HOI! Session still set. the session id is: "+session.getId());
      }
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <input type="hidden" id="refreshed" value="no">
    <%
    String usernameFinal=(String) session.getAttribute("usernameSession");
    //if(usernameFinal==null)
    //{
    //usernameFinal="";
    //}
    %>

     <p>Welcome: <%=usernameFinal%></p>
     <p>Session id is: <%=session.getId()%></p>
     <a href="sessionLogout.jsp">Logout</a>
    </body>
    </html>


    sessionLogout.jsp
    Code:


    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <%
      //String usernameFinal=(String) session.getAttribute("usernameSession");

      //ada 2 cara untuk logout!cara 1 dan cara 2. tp cara 1 lebih clear dan specific!


      //cara1
      http://session.removeAttribute("usernameSession");
      //%><jsp:forward page="home.jsp"/><%
           

      //cara2
      session.invalidate();
      %><jsp:forward page="home.jsp"/>   
    </body>
    </html>


    ok..harap dapat bantu..trimas y'all.
    avatar
    parangjiwa
    Ahli Baharu
    Ahli Baharu


    Number of posts : 34
    Registration date : 10/07/2009

    Problem ngan JSP session Empty Re: Problem ngan JSP session

    Post by parangjiwa Fri Aug 14, 2009 1:05 pm

    ok..since takder reply. Topik ditutup. SEKIAN!!!

      Current date/time is Fri May 17, 2024 4:25 pm